// Keys a page section reads so that a page can ask the owner something and// say a thing by hand, each drawn once for any table that names it (Photos was// the first: an album's "Cotton put these together — keep the album?" with// Keep and Not this while it is open, "You said not this one." in red with// Bring it back once dismissed, its days and pictures as one handwritten line,// and a photo's review with its Keep and Delete)://// * `when` — the section is drawn only while the record holds a value;// * `note` with its `ink` — one sentence by hand at the section's head;// * `hand` — one handwritten line of some columns, or a sentence naming// them ("Suggested by {review_by} on {review_at}");// * `answers` — the slip's answers (`SlipAnswers`), under the section;// * `actions` — typed verbs that are not one choice: clear some columns, or// delete the record after its question.//// The reading lives here, pure, so the words are settled by tests; what// draws a record only draws them.importCottonKitimportCottonValuesimportFoundation/// The values a record must hold for a section to be drawn: one of `values`/// in `column`, or — `present` — anything at all (true) or nothing (false).publictypealiasSectionWhen = DefinitionSectionWhenextensionDefinitionSectionWhen {publicinit(column: String, values: [String]? = nil, present: Bool? = nil) {self.init(_generated: (), column: column)self.values = valuesself.present = present }}/// The ink a hand line is drawn in. Red is a word the owner should not miss;/// never a tint that carries meaning on its own.publictypealiasSectionInk = DefinitionInk/// One handwritten line: some columns' words joined by the page's separator,/// those holding nothing left out — an entry may be words naming a column/// ("Away from {home_label}"), left out with it; or one sentence whose/// `{column}` tokens are those columns' words, drawn only when every token/// has words.publictypealiasPageHand = DefinitionPageHandextensionDefinitionPageHand {publicvar keys: [String] {switchself {case .columns(let columns): return columns.flatMap { $0.contains("{") ? PageHand.tokens(in: $0) : [$0] }case .sentence(let sentence): returnPageHand.tokens(in: sentence) } }staticfunc tokens(in sentence: String) -> [String] {var out: [String] = []var rest = Substring(sentence)whileletopen = rest.firstIndex(of: "{"), let close = rest[open...].firstIndex(of: "}") {let key = String(rest[rest.index(after: open)..<close])if !key.isEmpty { out.append(key) } rest = rest[rest.index(after: close)...] }return out }}/// A typed verb under a page section that is not one choice: `sets` writes/// those columns at once ("Keep" clears a review note), `delete` deletes the/// record after the page's own Delete question.publictypealiasSectionAction = DefinitionSectionActionextensionDefinitionSectionAction {publicinit(label: String, sets: [String: RecordValue] = [:], delete: Bool = false, object: String? = nil, does: CommandDoes? = nil) {self.init(_generated: (), label: label)self.sets = setsself.delete = deleteself.object = objectself.does = does }}extensionTableDefinition {/// Whether a section's `when` holds for this record; a section with no/// `when` always does.publicfunc holds(_ section: PageSection, for record: TableRecord) -> Bool { holds(section.when, for: record) }/// Whether a `when` holds for the record as read; nil always holds.publicfunc holds(_ when: SectionWhen?, for record: TableRecord) -> Bool {guardlet when else { returntrue }let value = record[when.column]iflet present = when.present, present == RecordValue.isBlank(value) { returnfalse }iflet values = when.values {guardlet text = value.text else { returnfalse }return values.contains(text) }returntrue }/// Words with their `{column}` tokens filled, or nil when one says nothing.privatefunc filled(_ words: String, of record: TableRecord, links: [String: String], now: Date) -> String? {var line = wordsfor key inPageHand.tokens(in: words) {guardlet value = handWords(key, of: record, links: links, now: now) else { returnnil } line = line.replacingOccurrences(of: "{\(key)}", with: value) }return line }/// A column's words in a hand line: a date as a hand writes a day ("12/// September 2026", or its range with `until`), anything else as a page/// writes it.privatefunc handWords(_ key: String, of record: TableRecord, links: [String: String], now: Date) -> String? {guardlet column = column(key) else { returnnil }if column.type == .date, column.until == nil, record[key].text != nil {return record.dayParts(key)?.dayLong }return words(key, of: record, links: links, now: now) }}// MARK: - Answers as drawn/// One answer as drawn: the value it writes, its words, and whether it is/// the agreeing one (`primary`, drawn filled).publicstructAnswerVerb: Hashable, Sendable {publicvar value: Stringpublicvar words: Stringpublicvar primary: Bool}extensionPageSection {/// The list the section draws. A page's `main` list is the page's own/// list, not a second one under it: it pages on as a list does, so a/// `limit` never caps it (a statement's every line, under it).publicvar drawnList: SectionList? {guard role == .main, var list else { return list } list.limit = nilreturn list }}extensionSlipAnswers {/// Whether the record still waits on these answers: its column holds/// nothing, or one of `when` — the rule `isAnswered` reads, for answers a/// page section carries apart from the slip's.publicfunc isOpen(_ record: TableRecord) -> Bool {let value = record[column]ifRecordValue.isBlank(value) { returntrue }guardlet text = value.text else { returnfalse }return when.contains(text) }/// The verbs drawn for the record in `answerVerbs`' order — the agreeing/// one first, then the column's choices — none once it is answered.publicfunc verbs(for record: TableRecord, in definition: TableDefinition) -> [AnswerVerb] {guard isOpen(record) else { return [] }var probe = definition probe.slip.answers = selfreturn probe.answerVerbs(record).map { AnswerVerb(value: $0.value, words: $0.words, primary: $0.value == primary) } }}extensionTableDefinition {/// The slip's answers for the record as drawn, the agreeing one marked;/// none when it asks nothing or is answered.publicfunc drawnAnswers(_ record: TableRecord) -> [AnswerVerb] { slip.answers?.verbs(for: record, in: self) ?? [] }}