import Foundation

final class NavigationSettings: ObservableObject, LocalizedItem {

    /// The items to show in the navigation bar
    @Published
    var navigationItems: [Item]

    @Published
    var german: LocalizedNavigationSettings

    @Published
    var english: LocalizedNavigationSettings

    init(navigationItems: [Item],
         german: LocalizedNavigationSettings,
         english: LocalizedNavigationSettings) {
        self.navigationItems = navigationItems
        self.german = german
        self.english = english
    }
}

// MARK: Storage

extension NavigationSettings {

    convenience init(context: LoadingContext, data: NavigationSettings.Data) {
        self.init(
            navigationItems: data.navigationItems.compactMap(context.item),
            german: LocalizedNavigationSettings(data: data.german),
            english: LocalizedNavigationSettings(data: data.english))
    }

    struct Data: Codable, Equatable {
        let navigationItems: [ItemId]
        let german: LocalizedNavigationSettings.Data
        let english: LocalizedNavigationSettings.Data
    }

    var data: Data {
        .init(
            navigationItems: navigationItems.map { $0.itemId },
            german: german.data,
            english: english.data)
    }
}

extension NavigationSettings {

    static var `default`: NavigationSettings {
        .init(navigationItems: [],
              german: .default,
              english: .default)
    }
}
