import Foundation

final class ContentLabel: ObservableObject {

    @Published
    var icon: PageIcon

    @Published
    var value: String

    init(icon: PageIcon, value: String) {
        self.icon = icon
        self.value = value
    }
}

extension ContentLabel: Equatable {

    static func == (lhs: ContentLabel, rhs: ContentLabel) -> Bool {
        lhs.icon == rhs.icon && lhs.value == rhs.value
    }
}

extension ContentLabel: Identifiable {

    var id: String {
        icon.rawValue + value
    }
}

extension ContentLabel {

    var data: Data {
        .init(icon: icon.rawValue, value: value)
    }

    convenience init?(context: LoadingContext, data: Data) {
        guard let icon = PageIcon(rawValue: data.icon) else {
            context.error("Unknown label icon '\(data.icon)'")
            return nil
        }
        self.init(icon: icon, value: data.value)
    }

    struct Data: Codable {
        let icon: String
        let value: String
    }
}
