import Foundation
import SwiftUI

/**
 A localized page contains the page content of a single language,
 including the title, url path and required resources

 */
final class LocalizedPage: ObservableObject {

    unowned let content: Content

    /**
     The string to use when creating the url for the page.

     Defaults to ``id`` if unset.
     */
    @Published
    var urlString: String

    /**
     The headline to use when showing the entry on it's own page
     */
    @Published
    var title: String

    @Published
    var lastModified: Date?

    /**
     The url used on the old version of the website.

     Needed to redirect links to their new locations.
     */
    let originalUrl: String?

    @Published
    var linkPreviewImage: FileResource?

    @Published
    var linkPreviewTitle: String?

    @Published
    var linkPreviewDescription: String?

    @Published
    var hideTitle: Bool

    init(content: Content,
         urlString: String,
         title: String,
         lastModified: Date? = nil,
         originalUrl: String? = nil,
         linkPreviewImage: FileResource? = nil,
         linkPreviewTitle: String? = nil,
         linkPreviewDescription: String? = nil,
         hideTitle: Bool = false) {
        self.content = content
        self.urlString = urlString
        self.title = title
        self.lastModified = lastModified
        self.originalUrl = originalUrl
        self.linkPreviewImage = linkPreviewImage
        self.linkPreviewTitle = linkPreviewTitle
        self.linkPreviewDescription = linkPreviewDescription
        self.hideTitle = hideTitle
    }
    
    func isValid(urlComponent: String) -> Bool {
        content.isValidIdForTagOrPageOrPost(urlComponent) &&
        !content.containsPage(withUrlComponent: urlComponent)
    }
}

extension LocalizedPage: LinkPreviewItem {

}
