// A link arriving in a window: its database first, then its route.//// Every way in — the phone's `onOpenURL`, the Mac's `DeepLinkInbox`, a// notification tap — reaches `PendingTaps` as a `DatabaseLink`, and// `RootView` hands each one here before the route is opened. The rule is// `DatabaseLinkArrival.decide` (`CottonKit/DatabaseLink.swift`); this is what// the window does with its answer.importCottonKitimportFoundation/// Places links in ONE window: the window's own `DatabaseScene`, the app's/// held list, and the app's toasts.@MainActorstructDatabaseLinkPlacing {let model: DatabasesModellet scene: DatabaseScenelet toasts: ToastCenter/// The core a link that names no database asks each held database/// through, whether it holds the link's record; nil where there is none/// (a gallery specimen), when such a link opens where the window is.var core: (any CottonCalling)? = nil/// Show the link's database in this window — which also makes it the last/// one used — and answer the route to open. Nil when nothing opens.func place(_ link: DatabaseLink) async -> Route? {// A cold-start link can land before the window has read the list.if model.held.isEmpty { await model.refresh() }switchDatabaseLinkArrival.decide(link: link, held: model.held, shown: scene.shownId) {case .open(let database, let route):await scene.show(database, in: model)return routecase .notHeld: toasts.show(ShellLines.line("databases", "that_link_opens_a"), tone: .error)returnnilcase .ask(let route, let among):// No database named and several held: each is asked whether it// holds the record, and the only one that does is shown. With// none or several — or a route that names no record — it opens// in the database this window shows.let holding = await holders(of: route, among: among)ifcase .open(let database, _) = DatabaseLinkArrival.settle(route: route, among: among, holding: holding) {await scene.show(database, in: model) }return routecase .here(let route):return route } }/// The held databases among `among` whose copy holds `route`'s record:/// one `read.record` each, the personal database through the core as it/// is and every other named. A refusal is "not here".privatefunc holders(of route: Route, among: [String]) async -> [String] {guardlet core, let record = DatabaseLinkArrival.record(of: route) else { return [] }var holding: [String] = []for database in among {let personal = model.held.first { $0.databaseId == database }?.isPersonal ?? falselet caller: any CottonCalling = personal ? core : DatabaseScopedCore(underlying: core, database: database)let answer = try? await caller.callJSON(CottonMethod.readRecord, RecordReadRequest(kind: record.kind.rawValue, id: record.id))iflet answer, answer != .null { holding.append(database) } }return holding }}