// Curate-on-import: what `/people/import` decides about this phone's own // address book, PURE — which cards are already in People, the selection, the // in-import search — and the one tool the import calls. Kept with the import // screen when People's own screens became its table's list and page. import CottonKit import Foundation /// The import's decisions and its tool. public enum ContactImport { /// The insert half of `applyContactSync`, and THE identity spine: every row /// it writes carries `device_contact_id`, so a second run skips rather than /// duplicates and a rename in Cotton stays harmless. static let syncDeviceContacts = "sync_device_contacts" } /// One card of this phone's own address book, as the import screen reads it. /// /// It is NOT a boundary shape and cannot be: no read answers the device's /// contacts — the core has never seen them, and saying so is the point of the /// permission gate. public struct DeviceContact: Equatable, Identifiable, Sendable { public let id: String public let fullName: String public let phones: [String] public let emails: [String] public let org: String? public let roleTitle: String? /// The first postal address that names one — `create_person` takes a city /// and the address book is where this app learns it. public let city: String? public let birthMonth: Int? public let birthDay: Int? public let birthYear: Int? public init( id: String, fullName: String, phones: [String] = [], emails: [String] = [], org: String? = nil, roleTitle: String? = nil, city: String? = nil, birthMonth: Int? = nil, birthDay: Int? = nil, birthYear: Int? = nil ) { self.id = id self.fullName = fullName self.phones = phones self.emails = emails self.org = org self.roleTitle = roleTitle self.city = city self.birthMonth = birthMonth self.birthDay = birthDay self.birthYear = birthYear } } extension ContactImport { /// The set of device cards this workspace already holds — what "Already in /// People" is made of, and the guard that keeps a second import from /// duplicating somebody. /// /// An ARCHIVED person is absent from `read.peopleImport`'s answer on /// purpose: they are out of the directory, and offering their card as taken /// would make them unreachable. /// A person with no card contributes NO id: `text(_:)` turns a blank /// `deviceContactId` into nothing, and an empty string in this set would /// swallow every card the phone could not identify. public static func alreadyLinked(_ read: JSONValue) -> Set { Set(read.list("linked").compactMap { $0.text("deviceContactId") }) } /// Split device cards by whether their id is already imported. Both halves /// are NAME-ORDERED, which is the order the list draws. public static func partitionContacts( _ contacts: [DeviceContact], alreadyLinked linked: Set ) -> (eligible: [DeviceContact], alreadyImported: [DeviceContact]) { var eligible: [DeviceContact] = [] var imported: [DeviceContact] = [] for contact in contacts { if linked.contains(contact.id) { imported.append(contact) } else { eligible.append(contact) } } let byName: (DeviceContact, DeviceContact) -> Bool = { $0.fullName.localizedCaseInsensitiveCompare($1.fullName) == .orderedAscending } return (eligible.stableSorted(by: byName), imported.stableSorted(by: byName)) } /// Toggle one card in or out of the selection. public static func toggleSelected(_ selected: Set, _ id: String) -> Set { var next = selected if next.contains(id) { next.remove(id) } else { next.insert(id) } return next } /// Case-insensitive name / org / phone filter — the in-import search field. /// Selection is keyed by id, so filtering never loses one. public static func filterContacts(_ contacts: [DeviceContact], query: String) -> [DeviceContact] { let needle = query.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() guard !needle.isEmpty else { return contacts } return contacts.filter { contact in if contact.fullName.lowercased().contains(needle) { return true } if let org = contact.org, org.lowercased().contains(needle) { return true } return contact.phones.contains { $0.filter { !$0.isWhitespace }.contains(needle) } } } /// The cards the import will actually send — selected ∩ eligible. public static func selectedContacts( _ eligible: [DeviceContact], selected: Set ) -> [DeviceContact] { eligible.filter { selected.contains($0.id) } } }