// 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.importCottonKitimportFoundation/// The import's decisions and its tool.publicenumContactImport {/// 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.staticlet 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.publicstructDeviceContact: Equatable, Identifiable, Sendable {publiclet id: Stringpubliclet fullName: Stringpubliclet phones: [String]publiclet emails: [String]publiclet org: String?publiclet roleTitle: String?/// The first postal address that names one — `create_person` takes a city/// and the address book is where this app learns it.publiclet city: String?publiclet birthMonth: Int?publiclet birthDay: Int?publiclet birthYear: Int?publicinit( 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 = idself.fullName = fullNameself.phones = phonesself.emails = emailsself.org = orgself.roleTitle = roleTitleself.city = cityself.birthMonth = birthMonthself.birthDay = birthDayself.birthYear = birthYear }}extensionContactImport {/// 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.publicstaticfunc alreadyLinked(_ read: JSONValue) -> Set<String> {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.publicstaticfunc partitionContacts( _ contacts: [DeviceContact], alreadyLinked linked: Set<String> ) -> (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.publicstaticfunc toggleSelected(_ selected: Set<String>, _ id: String) -> Set<String> {var next = selectedif 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.publicstaticfunc filterContacts(_ contacts: [DeviceContact], query: String) -> [DeviceContact] {let needle = query.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()guard !needle.isEmpty else { return contacts }return contacts.filter { contact inif contact.fullName.lowercased().contains(needle) { returntrue }iflet org = contact.org, org.lowercased().contains(needle) { returntrue }return contact.phones.contains { $0.filter { !$0.isWhitespace }.contains(needle) } } }/// The cards the import will actually send — selected ∩ eligible.publicstaticfunc selectedContacts( _ eligible: [DeviceContact], selected: Set<String> ) -> [DeviceContact] { eligible.filter { selected.contains($0.id) } }}