// A whole keeps its parts on the next fan tier; the hub closes one level.importCottonValuesimportFoundation/// One whole the fan stands inside: its record, and its title as its card/// said it when it was opened.publicstructWholeLevel: Hashable, Sendable {publiclet table: Stringpubliclet id: Stringpubliclet title: Stringpublicinit(table: String, id: String, title: String) {self.table = tableself.id = idself.title = title }}/// One part as the fan deals it: the part's own record (the one its deck/// holds), and the record it stands for — itself, or with `through` the/// record its link names (a playlist's entry stands for its track).publicstructWholePart: Hashable, Sendable {/// The part's own id, in the parts' table.publiclet part: String/// The record the fan shows for it.publiclet table: Stringpubliclet id: Stringpublicvar title: String}/// One word of the crumb line and where its tap goes: the depth of wholes/// left standing (0 is the tool's own deck), nil for where the fan stands.publicstructWholeCrumb: Hashable, Sendable {publiclet word: Stringpubliclet depth: Int?}enumWholePath {// MARK: The parts/// The narrowing a whole's parts are read with: the parts whose `by`/// link names the whole.staticfunc filter(_ opens: DefinitionCardOpens, whole id: String) -> [String: RecordValue] { [opens.by: .text(id)] }/// The parts in the order `order` deals them: by its first column, a tie/// by the next, and so on; a part with nothing in a column comes after/// one with something; a full tie keeps the order they were read in.staticfunc ordered(_ parts: [TableRecord], by order: [String]) -> [TableRecord] {guard !order.isEmpty else { return parts }return parts.enumerated().sorted { a, b infor key in order {let (x, y) = (a.element[key], b.element[key])switch (RecordValue.isBlank(x), RecordValue.isBlank(y)) {case (false, true): returntruecase (true, false): returnfalsecase (true, true): continuecase (false, false):ifTableDefinition.precedes(x, y) { returntrue }ifTableDefinition.precedes(y, x) { returnfalse } } }return a.offset < b.offset }.map(\.element) }/// The order a deck of the parts is drawn in, as a list sort holds it:/// the first `order` column, a tie by the second. Nil with no order.staticfunc sort(_ order: [String]) -> ListSort? {guardlet first = order.first else { returnnil }var sort = ListSort(_generated: (), column: first) sort.tie = order.dropFirst().firstreturn sort }/// The parts' table as the page deals them: in the whole's order, which/// stands over the table's own (a drag order included).staticfunc dealt(_ parts: TableDefinition, by opens: DefinitionCardOpens) -> TableDefinition {guardlet sort = sort(opens.order) else { return parts }var dealt = parts dealt.order = nil dealt.sort = sortreturn dealt }/// What each part stands for, in the parts' order: itself, or with/// `through` the record its link names in the link's table. A part whose/// link names nothing is left out — the fan never deals a blank card.staticfunc standing(_ parts: [TableRecord], opens: DefinitionCardOpens, in definition: TableDefinition?) -> [WholePart] {guardlet through = opens.through else {return parts.map { WholePart(part: $0.id, table: opens.table, id: $0.id, title: title(of: $0, in: definition)) } }guardlet linked = definition?.column(through)?.linkTo else { return [] }return parts.compactMap { part inguardlet id = FaceText.linkIDs(part[through]).first else { returnnil }// Its own title until the linked record is read.returnWholePart(part: part.id, table: linked, id: id, title: title(of: part, in: definition)) } }/// Where a part's card on the fan goes: into it, when the record it/// stands for is a whole of its own (its card `opens`); else that part is/// brought to the front of the deck. The definitions decide, never whether/// the part's own parts have been read yet.staticfunc entered(_ part: WholePart, in definitions: TableDefinitions) -> WholeLevel? {guard definitions[part.table]?.card?.opens != nilelse { returnnil }returnWholeLevel(table: part.table, id: part.id, title: part.title) }/// A record's title as its card says it, else its title column.staticfunc title(of record: TableRecord, in definition: TableDefinition?) -> String {DeckCard(record)?.text("title") ?? record[definition?.title].text ?? "" }// MARK: The path/// The path with `whole` opened inside where it stands. A whole already/// on the path is gone back to, never opened twice.staticfunc entering(_ whole: WholeLevel, _ path: [WholeLevel]) -> [WholeLevel] {iflet at = path.firstIndex(where: { $0.table == whole.table && $0.id == whole.id }) {returnArray(path.prefix(at + 1)) }return path + [whole] }/// A step out to `depth` wholes: what stands, and the whole just left,/// whose card the deck under it opens at.structLeaving: Equatable {let path: [WholeLevel]let opensAt: WholeLevel }/// Out to `depth` wholes (0: the tool's own deck); nil where the fan/// already stands there or deeper is asked.staticfunc leaving(_ path: [WholeLevel], to depth: Int) -> Leaving? {guard depth >= 0, depth < path.count else { returnnil }returnLeaving(path: Array(path.prefix(depth)), opensAt: path[depth]) }/// The back card's step: one level up; from the top level, out of the/// whole to the tool's own deck.staticfunc back(_ path: [WholeLevel]) -> Leaving? { leaving(path, to: path.count - 1) }/// The crumb line: the tool, the first whole's table, then each whole/// by its title, the last where the fan stands. The tool and the table/// both go back to the tool's own deck; one word the same as the one/// before it is said once ("Books › Dune", never twice).staticfunc crumbs(tool: String, path: [WholeLevel], plural: (String) -> String?) -> [WholeCrumb] {guardlet first = path.first else { return [] }var crumbs = [WholeCrumb(word: tool, depth: 0)]iflet table = plural(first.table), table != tool { crumbs.append(WholeCrumb(word: table, depth: 0)) }for (at, whole) in path.enumerated() { crumbs.append(WholeCrumb(word: whole.title, depth: at + 1 == path.count ? nil : at + 1)) }return crumbs }}