import Foundation /// IPTC values keyed by `IPTCField.id`. Text fields and list fields are stored separately /// so list semantics (keywords, persons …) survive round trips exactly. public struct IPTCValues: Codable, Hashable, Sendable { public var text: [String: String] = [:] public var lists: [String: [String]] = [:] public init(text: [String: String] = [:], lists: [String: [String]] = [:]) { self.text = text self.lists = lists } public subscript(field: IPTCField) -> String { get { if field.kind == .list { return (lists[field.id] ?? []).joined(separator: ", ") } return text[field.id] ?? "" } set { if field.kind == .list { let items = IPTCValues.splitList(newValue) lists[field.id] = items.isEmpty ? nil : items } else { text[field.id] = newValue.isEmpty ? nil : newValue } } } public func list(_ field: IPTCField) -> [String] { lists[field.id] ?? [] } public func isEmpty(_ field: IPTCField) -> Bool { field.kind == .list ? (lists[field.id]?.isEmpty ?? true) : (text[field.id]?.isEmpty ?? true) } public var isCompletelyEmpty: Bool { text.isEmpty && lists.isEmpty } /// Keywords are separated by comma or semicolon (PM accepts both). public static func splitList(_ s: String) -> [String] { s.split(whereSeparator: { $0 == "," || $0 == ";" || $0 == "\n" }) .map { $0.trimmingCharacters(in: .whitespaces) } .filter { !$0.isEmpty } } } public enum ListMergeMode: String, Codable, CaseIterable, Sendable { case append = "Anhängen" case replace = "Ersetzen" } /// A metadata template (PM: "IPTC Stationery Pad" / "IPTC Template"). public struct IPTCTemplate: Codable, Identifiable, Hashable, Sendable { public var id: UUID public var name: String public var values: IPTCValues public var listMergeMode: ListMergeMode /// If true, empty template fields clear the target field (PM: "Replace all"). public var clearEmptyFields: Bool /// XMP properties from an imported PM template we do not model; written back on export. public var extras: [String: String] public var modified: Date public init(id: UUID = UUID(), name: String, values: IPTCValues = IPTCValues(), listMergeMode: ListMergeMode = .append, clearEmptyFields: Bool = false, extras: [String: String] = [:], modified: Date = Date()) { self.id = id self.name = name self.values = values self.listMergeMode = listMergeMode self.clearEmptyFields = clearEmptyFields self.extras = extras self.modified = modified } /// Merges the template into existing image values. Variables / codes are NOT expanded here, /// that happens per image in `MetadataWriter`. public func apply(to existing: IPTCValues) -> IPTCValues { var result = existing for field in IPTCField.all { if field.kind == .list { let tpl = values.list(field) if tpl.isEmpty { if clearEmptyFields { result.lists[field.id] = nil } continue } switch listMergeMode { case .replace: result.lists[field.id] = tpl case .append: var merged = existing.list(field) for k in tpl where !merged.contains(k) { merged.append(k) } result.lists[field.id] = merged } } else { let tpl = values.text[field.id] ?? "" if tpl.isEmpty { if clearEmptyFields { result.text[field.id] = nil } continue } result.text[field.id] = tpl } } return result } }