import Foundation

final class LocalizedPostSettings: ObservableObject {

    var linkPreview: LinkPreview

    /// The path to the feed in the final website, appended with the page number
    @Published
    var feedUrlPrefix: String

    /**
     The text to display when linking to a page

     Each post may define a custom text.
     */
    @Published
    var defaultPageLinkText: String

    init(feedUrlPrefix: String, defaultPageLinkText: String, linkPreview: LinkPreview) {
        self.feedUrlPrefix = feedUrlPrefix
        self.defaultPageLinkText = defaultPageLinkText
        self.linkPreview = linkPreview
    }
}

// MARK: Storage

extension LocalizedPostSettings {

    convenience init(context: LoadingContext, data: Data) {
        self.init(
            feedUrlPrefix: data.feedUrlPrefix,
            defaultPageLinkText: data.defaultPageLinkText,
            linkPreview: .init(context: context, data: data.linkPreview))
    }

    var data: Data {
        .init(
            feedUrlPrefix: feedUrlPrefix,
            defaultPageLinkText: defaultPageLinkText,
            linkPreview: linkPreview.data)
    }

    struct Data: Codable, Equatable {
        let feedUrlPrefix: String
        let defaultPageLinkText: String
        let linkPreview: LinkPreview.Data
    }
}
