// How far a write to a record that repeats reaches.//// One day of a repeating event is not a row of its own: a change or a delete// of it could mean that day or every day. The core never guesses (architect,// 5 Oct: one day's new time moved a whole series) — it refuses such a write// without a `scope` — and it marks the records that ask (`scope_columns`), so// the app asks "Only this one" or "Every one" before it writes, on any table// whose engine says so, never by knowing which table repeats.importCottonValuesimportFoundation/// The reach of a write to a record that repeats.publicenumWriteScope: String, Sendable {/// Only this one: the day becomes its own record; the rest are untouched.case this/// Every one: the whole series.case all}/// A store that cannot say how far a write reaches was handed one that must.publicstructScopeNotCarried: LocalizedError, Equatable {publicvar errorDescription: String? { ShellLines.line("definitions", "scope.not_carried") }}extensionTableRecord {/// The columns whose change asks how far it reaches, as the core lists/// them on a record that repeats. Not a column of any definition; absent/// on a record that does not repeat.publicstaticlet scopeColumnsKey = "scope_columns"/// Whether this record repeats: a delete of it asks how far it reaches.publicvar repeats: Bool { !scopeColumns.isEmpty }var scopeColumns: [String] {guardcase .list(let keys) = self[Self.scopeColumnsKey] else { return [] }return keys.compactMap(\.text) }}/// The question asked before a write to a record that repeats: two answers,/// "Only this one" and "Every one" — or "Every one" alone where the change/// belongs to all of them (how it repeats).publicstructScopeQuestion: Equatable, Sendable {publicstaticvar onlyThis: String { ShellLines.line("definitions", "scope.only_this") }publicstaticvar everyOne: String { ShellLines.line("definitions", "scope.every_one") }publiclet title: Stringpubliclet message: String/// False where only the whole series can take the change.publiclet offersThis: Bool/// It removes something (red ink).publiclet destructive: Bool/// Before a change to `column` of a record that repeats.publicstaticfunc change(_ record: TableRecord, column: ColumnDefinition, in definition: TableDefinition) -> ScopeQuestion {let name = record.said?.slip.name ?? ShellLines.line("definitions", "scope.this", ["name": definition.name.lowercased()])// How it repeats is every one's: the core takes that change for all only.let whole = column.format == .repeatRulereturnScopeQuestion( title: ShellLines.line("definitions", "scope.repeats", ["name": name]), message: ShellLines.line("definitions", whole ? "scope.change_whole" : "scope.change_question"), offersThis: !whole, destructive: false) }/// Before a delete of a record that repeats; `bin` is the table's own/// sentence for where a deleted record goes.publicstaticfunc delete(_ record: TableRecord, in definition: TableDefinition, bin: String) -> ScopeQuestion {ScopeQuestion( title: ShellLines.line("definitions", "scope.repeats", ["name": record.said?.slip.name ?? ShellLines.line("definitions", "scope.this", ["name": definition.name.lowercased()])]), message: bin.isEmpty ? ShellLines.line("definitions", "scope.delete_question") : ShellLines.line("definitions", "scope.delete_question_bin", ["bin": bin]), offersThis: true, destructive: true) }}