// Groups (Spec 6, note 8c089dd6): a table whose records the owner gathers// into named, hand-picked groups says so with `groups`, naming the existing// face its stacks are piled from — a tool's groups are a row of small piles// above its list, photographs for People and paper slips for Todos.//// A group is a record of the `group` table and lives in its own tool: one// "Family", read by the People list, Home and anything else that names// it. A record may be in many groups. Every group is hand-picked; there is no// other kind.//// The reading and the verbs live here, in one place, so the surfaces only// draw. The core answers them (`groups.list`, `groups.create`,// `groups.update`, `groups.delete`): a table's groups with the first three// records of each, a group made from a name (and the records to start it// with), ONE change per write — a rename, records put in, records taken out —// each a single change with its receipt and its Undo, and a delete that// leaves the records where they are. A group's records are the table's own// list, narrowed by `group` (`records.list`).importCottonKitimportCottonValuesimportFoundation/// A table's `groups`: its records can be gathered into named groups.publictypealiasTableGroups = DefinitionGroupsextensionDefinitionGroups {publictypealiasStack = DefinitionStackFacepublictypealiasKeptIn = DefinitionGroupsKeptInpublicinit(stack: Stack, keptIn: KeptIn? = nil) {self.init(_generated: (), stack: stack)self.keptIn = keptIn }}extensionDefinitionGroupsKeptIn {publicinit(table: String, items: String) { self.init(_generated: (), table: table, items: items) }}/// One group, as the core answers it.publicstructRecordGroup: Identifiable, Hashable, Sendable, Decodable {publicvar id: Stringpublicvar name: String/// The table whose records it gathers.publicvar of: String/// How many records it holds.publicvar count: Int?/// The table the group is a record of, when its groups are kept in one/// (`TableGroups.keptIn`): an album, whose page opens it.publicvar keptIn: String?/// The word that record's slip wears, drawn under its stack in red: an/// album Cotton suggested says "suggested".publicvar byline: String?/// A filter group's `where` — the Email list's Junk stack is/// `{sender_kind: junk}`, and offers Empty Junk now on its page.publicvar filter: [String: RecordValue]?enumCodingKeys: String, CodingKey {case id, name, count, bylinecase of = "table"case keptIn = "kept_in"case filter = "where" }publicinit( id: String, name: String, of: String, count: Int? = nil, keptIn: String? = nil, byline: String? = nil, filter: [String: RecordValue]? = nil ) {self.id = idself.name = nameself.of = ofself.count = countself.keptIn = keptInself.byline = bylineself.filter = filter }}/// A group as a stack: the group, the first few of its records as the/// table's own records, and — when the read named a record — whether the/// group holds it.publicstructGroupStack: Identifiable, Hashable, Sendable {publicvar group: RecordGrouppublicvar top: [TableRecord]publicvar holds: Bool?publicvar id: String { group.id }publicinit(group: RecordGroup, top: [TableRecord] = [], holds: Bool? = nil) {self.group = groupself.top = topself.holds = holds }}/// The ONE thing a write changes about a group.publicenumGroupChange: Hashable, Sendable {case name(String)/// Records put in, after the last.case add([String])/// Records taken out; the records themselves are untouched.case remove([String])/// Members in their new order, the rest after them as they stood — a/// drag on the group's own page, which never touches the table's order.case order([String])/// The write's request: the group's id and exactly one of the keys.publicfunc request(id: String) -> [String: RecordValue] {switchself {case .name(let name): return ["id": .text(id), "name": .text(name)]case .add(let ids): return ["id": .text(id), "add": .list(ids.map(RecordValue.text))]case .remove(let ids): return ["id": .text(id), "remove": .list(ids.map(RecordValue.text))]case .order(let ids): return ["id": .text(id), "order": .list(ids.map(RecordValue.text))] } }}/// A store that keeps groups (the core's). A store that does not — the/// fixtures, Settings' own tables — draws no stacks and offers no group act.publicprotocolGroupingRecords: AnyObject, Sendable {/// The groups of `table`, in the order they were made; with `record`,/// each says whether it holds that record.func groups(of table: String, holding record: String?) asyncthrows -> [GroupStack]/// Makes a group, holding `records` in order when given.func createGroup(named name: String, of table: String, records: [String]) asyncthrows -> (change: ChangeRecord, group: RecordGroup?)func updateGroup(_ id: String, _ change: GroupChange) asyncthrows -> ChangeRecord/// Deletes the group; its records stay where they are.func deleteGroup(_ id: String) asyncthrows -> ChangeRecord}/// What the surfaces ask of groups, over whichever store is in the environment.publicenumGroupRecords {/// The `where` key that narrows a table's list to one group's records.publicstaticlet memberKey = "group"/// What narrows a table's list to `group`'s records, in the group's order.publicstaticfunc members(of group: RecordGroup) -> [String: RecordValue] { [memberKey: .text(group.id)] }/// The store's groups half, or nil when it keeps none.publicstaticfunc grouping(_ store: (any Records)?) -> (any GroupingRecords)? { store as? any GroupingRecords }/// A group's name as typed: trimmed, nil when nothing is left.publicstaticfunc name(typed: String) -> String? {let name = typed.trimmingCharacters(in: .whitespacesAndNewlines)return name.isEmpty ? nil : name }}