import Foundation

/// Represents a contact field that has been added
struct AddedContactField: Identifiable, Equatable {
    let id: UUID
    let fieldType: ContactFieldType
    var value: String
    var title: String

    init(id: UUID = UUID(), fieldType: ContactFieldType, value: String = "", title: String = "") {
        self.id = id
        self.fieldType = fieldType
        self.value = value
        self.title = title
    }

    static func == (lhs: AddedContactField, rhs: AddedContactField) -> Bool {
        lhs.id == rhs.id && lhs.value == rhs.value && lhs.title == rhs.title
    }

    /// Returns the display value for this field (formatted for addresses, raw for others)
    var displayValue: String {
        fieldType.formattedDisplayValue(value)
    }

    /// Returns a short display value suitable for single-line display in lists
    var shortDisplayValue: String {
        if fieldType.id == "address" {
            if let address = PostalAddress.decode(from: value), address.hasValue {
                return address.singleLineString
            }
        }
        return fieldType.formattedDisplayValue(value)
    }
}
