import Foundation

extension Content {

    private static let disallowedCharactersInIds = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-")).inverted

    private static let disallowedCharactersInFileIds = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-.")).inverted

    func isNewIdForTag(_ id: String) -> Bool {
        tagOverview?.identifier != id && !tags.contains { $0.identifier == id }
    }

    func isNewIdForPage(_ id: String) -> Bool {
        !pages.contains { $0.identifier == id }
    }

    func isNewIdForPost(_ id: String) -> Bool {
        !posts.contains { $0.identifier == id }
    }

    func isNewIdForFile(_ id: String) -> Bool {
        !files.contains { $0.identifier == id }
    }

    func isValidIdForTagOrPageOrPost(_ id: String) -> Bool {
        !id.isEmpty &&
        id.rangeOfCharacter(from: Content.disallowedCharactersInIds) == nil
    }

    func isValidIdForFile(_ id: String) -> Bool {
        !id.isEmpty &&
        id.rangeOfCharacter(from: Content.disallowedCharactersInFileIds) == nil
    }

    func containsPage(withUrlComponent urlComponent: String) -> Bool {
        pages.contains {
            $0.german.urlString == urlComponent ||
            $0.english.urlString == urlComponent
        }
    }

    func containsTag(withUrlComponent urlComponent: String) -> Bool {
        (tagOverview?.contains(urlComponent: urlComponent) ?? false) ||
        tags.contains { $0.contains(urlComponent: urlComponent) }
    }
}
