// What a card drawn off a record (Home's, a search's, a customer's) reads // beyond the core's words: the pages its card turns to, its places under // their labels, a rating, the values stored under its places, a delivery's // states, the ways a person is reached, its file. A deck's card is answered // whole by the core (`shell.deck`, `DeckCard.answered`) and is never filled // here; on any card the phone reads only what the person sees once it is // held (its related lists' counts). import CottonFaces import CottonKit import CottonPaper import CottonValues import SwiftUI enum DeckCardFill { enum RelatedCountFailure: Error { case missingTotal, noCore } /// The card with what its record and its table say beyond the core's /// words. `definition` is the table the deck's card target names; with /// none, the card turns to no pages and its places stand under no label. static func filled(_ card: DeckCard, record: TableRecord, definition: TableDefinition?) -> DeckCard { var filled = card // An absent picture must stay absent all the way to the face. if case .text(let key)? = filled.fields["photo"], key.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { filled.fields.removeValue(forKey: "photo") } filled.record = record.id filled.pages = pages(definition, record: record) filled.labels = labels(definition) filled.stars = stars(definition, record: record) filled.stored = stored(definition, record: record) filled.reach = reach(record) filled.file = AttachmentValue.read(record["storage_path"])?.ref if DefinitionCardKind(rawValue: card.face) == .nancyDelivery { filled.delivery = KitchenDeliveryState.of(record) } return filled } /// The columns a card turns to (`card.pages`) that hold words, in order, /// each under its column's label. static func pages(_ definition: TableDefinition?, record: TableRecord) -> [CardPage] { guard let definition, let names = definition.card?.pages else { return [] } return names.compactMap { name in guard let text = record[name].text?.trimmingCharacters(in: .whitespacesAndNewlines), !text.isEmpty else { return nil } return CardPage(label: definition.column(name)?.label ?? name, text: text, markdown: definition.setsMarkdown(name)) } } /// The labels a card's places stand under, read off its table: each /// slot under its explicit card label or the first column filling it, /// every column under its own. static func labels(_ definition: TableDefinition?) -> CardLabels { guard let definition else { return CardLabels() } var labels = CardLabels() for field in definition.card?.fields ?? [] { let label = field.label.map { ShellLines.line("cards", $0) } ?? field.column.flatMap { definition.column($0)?.label } guard let label else { continue } labels.bySlot[field.slot.rawValue] = labels.bySlot[field.slot.rawValue] ?? label } for column in definition.columns { labels.byColumn[column.key] = column.label } return labels } /// The rating its facts name in the rating column, out of five. static func stars(_ definition: TableDefinition?, record: TableRecord) -> Double? { guard let facts = definition?.card?.fields.first(where: { $0.slot == .facts }) else { return nil } let columns = [facts.column].compactMap { $0 } + facts.also guard columns.contains(KitchenColumns.rating), let value = record[KitchenColumns.rating].number, (0...Self.most).contains(value) else { return nil } return value } /// The most stars a rating holds. static let most: Double = 5 /// The value stored in the column that fills each slot, as kept (a /// state's "running", a dish's kind), by slot: the first column filling it. /// The state place's set yes/no flag (`also`) stands first, by its key: a /// film converting on the Mac stores `needs_mac`, whatever its status. static func stored(_ definition: TableDefinition?, record: TableRecord) -> [String: String] { var stored: [String: String] = [:] for field in definition?.card?.fields ?? [] { if field.slot == .state, let flag = field.also.first(where: { record[$0].bool == true || record[$0].number == 1 }) { stored[field.slot.rawValue] = flag continue } guard stored[field.slot.rawValue] == nil, let column = field.column, let text = record[column].text else { continue } stored[field.slot.rawValue] = text } return stored } /// The numbers and addresses a person's record holds, and their /// thread's channel. static func reach(_ record: TableRecord) -> CardReach { func list(_ key: String) -> [String] { switch record[key] { case .list(let items): return items.compactMap { $0.text } case .text(let text): return [text] default: return [] } } let phones = list("phones") let emails = list("emails").filter { $0.contains("@") } let channel = record["thread_channel"].text return CardReach(phones: phones, emails: emails, channel: channel, call: ContactHandoff.call(phones: phones) != nil, message: ContactHandoff.message(phones: phones, channel: channel) != nil, email: emails.first.flatMap { ContactHandoff.link(.mail, ContactTarget(value: $0, kind: .email)) } != nil) } /// The definition's related lists counted over their whole filtered read, /// each said by the core (`count.words`) in its table's line /// (`cards.opened.related.