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", .snap). 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 (only used when `appliedFields` is nil). public var clearEmptyFields: Bool /// PM-style checkboxes: only these fields are applied (an empty checked field clears the target). /// nil = apply every non-empty field. public var appliedFields: Set? /// PM "Apply date: photo capture date" – Date Created comes from EXIF instead of the template value. public var dateFromCapture: Bool /// XMP properties from an imported PM template we do not model; written back on export only. public var extras: [String: String] public var modified: Date /// PM field codes from an imported .snap that Lichtkasten could not map (shown as hint in the editor). public var unmappedPMCodes: [String] = [] public init(id: UUID = UUID(), name: String, values: IPTCValues = IPTCValues(), listMergeMode: ListMergeMode = .append, clearEmptyFields: Bool = false, appliedFields: Set? = nil, dateFromCapture: Bool = false, extras: [String: String] = [:], modified: Date = Date()) { self.id = id self.name = name self.values = values self.listMergeMode = listMergeMode self.clearEmptyFields = clearEmptyFields self.appliedFields = appliedFields self.dateFromCapture = dateFromCapture self.extras = extras self.modified = modified } enum CodingKeys: String, CodingKey { case id, name, values, listMergeMode, clearEmptyFields, appliedFields, dateFromCapture, extras, modified, unmappedPMCodes } public init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) id = try c.decodeIfPresent(UUID.self, forKey: .id) ?? UUID() name = try c.decodeIfPresent(String.self, forKey: .name) ?? "Vorlage" values = try c.decodeIfPresent(IPTCValues.self, forKey: .values) ?? IPTCValues() listMergeMode = try c.decodeIfPresent(ListMergeMode.self, forKey: .listMergeMode) ?? .append clearEmptyFields = try c.decodeIfPresent(Bool.self, forKey: .clearEmptyFields) ?? false appliedFields = try c.decodeIfPresent(Set.self, forKey: .appliedFields) dateFromCapture = try c.decodeIfPresent(Bool.self, forKey: .dateFromCapture) ?? false extras = try c.decodeIfPresent([String: String].self, forKey: .extras) ?? [:] modified = try c.decodeIfPresent(Date.self, forKey: .modified) ?? Date() unmappedPMCodes = try c.decodeIfPresent([String].self, forKey: .unmappedPMCodes) ?? [] } /// Fields this template writes. public var fieldsToWrite: [IPTCField] { IPTCField.all.filter { f in if let applied = appliedFields { return applied.contains(f.id) } if f.id == "dateCreated" && dateFromCapture { return true } return clearEmptyFields || !values.isEmpty(f) } } /// 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 fieldsToWrite { if field.kind == .list { let tpl = values.list(field) if tpl.isEmpty { 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] ?? "" result.text[field.id] = tpl.isEmpty ? nil : tpl } } return result } }