// Find's own private state: the searches that OPENED something.//// DEVICE-LOCAL by design, like the page's visit ledger: what you searched your// own database for is exactly the kind of record the memory product promises// never leaves the phone uninvited. It does not survive an uninstall, and that// is the right cost.//// **The store is a seam, and only because of the cap.** There is no// AsyncStorage here and no `createPersistedAtom`; `UserDefaults` is the whole// mechanism. It is behind a protocol so the cap, the dedupe and the ordering are// tested with no device, no container and no suite to clean up — the same reason// `SecretStore` has an in-memory twin. **Nothing else is persisted here**: the// query being typed, the expanded sections and the hits are all per-visit, and// an expansion is an answer to THIS answer rather than a setting.importFoundation/// How many recent searches the zero state offers back.publiclet searchRecentsCap = 8/// Where the recents live between launches.publicprotocolSearchRecentsStore: Sendable {func load() -> [String]func save(_ recents: [String])}/// The real one./// `@unchecked Sendable` because `UserDefaults` is thread-safe by contract and/// carries no annotation saying so; the struct holds nothing else.publicstructUserDefaultsSearchRecents: SearchRecentsStore, @unchecked Sendable {/// The previous app's own key, kept. Nothing reads that older store from/// Swift — the two runtimes share no container for it — so this is a NEW/// list on the first launch either way; the key carries across so a person/// reading the defaults finds one name for one thing.publicstaticlet key = "@cotton/find-recents"privatelet defaults: UserDefaultspublicinit(defaults: UserDefaults = .standard) {self.defaults = defaults }publicfunc load() -> [String] {// `stringArray(forKey:)` answers nil for an absent key AND for a value// that is not an array of strings, which is the same "no recents" to a// screen that has never been used. defaults.stringArray(forKey: UserDefaultsSearchRecents.key) ?? [] }publicfunc save(_ recents: [String]) { defaults.set(recents, forKey: UserDefaultsSearchRecents.key) }}/// The double every test drives.publicfinalclassInMemorySearchRecents: SearchRecentsStore, @unchecked Sendable {privatelet lock = NSLock()privatevar recents: [String]publicinit(_ recents: [String] = []) {self.recents = recents }publicfunc load() -> [String] { lock.lock()defer { lock.unlock() }return recents }publicfunc save(_ recents: [String]) { lock.lock()defer { lock.unlock() }self.recents = recents }}/// A search earns a place here only when it OPENED something — recording every/// keystroke would fill the list with the seven prefixes of one thought.////// Case-insensitive dedupe keeps "Ravi" and "ravi" one memory; the newer casing/// wins the slot.publicfunc rememberRecent(_ recents: [String], _ query: String) -> [String] {let trimmed = query.trimmingCharacters(in: .whitespacesAndNewlines)if trimmed.isEmpty { return recents }let kept = recents.filter { $0.lowercased() != trimmed.lowercased() }returnArray(([trimmed] + kept).prefix(searchRecentsCap))}