// The paper chat's rules, as pure functions — which paper a note is written on,// when a message becomes a letter, and how several photographs are laid down.//// The approved references are `ref-chat-1` (a one-to-one thread),// `ref-chat-2-longmessage` (the letter) and `ref-chat-3-group` (a group). What// the screen draws is in `MessageThreadScreen.swift`; what it DECIDES is here,// so a test holds every decision without a screen.importFoundationenumChatPaper {// ── Which paper ───────────────────────────────────────────────────────/// The dyes a group member's notes can be written on, in rotation order./// BLUE IS NOT AMONG THEM: blue is the owner's own paper, and a member on/// blue would read as something the owner wrote.staticlet memberDyes: [PaperDye] = [.cream, .sage, .straw, .peach, .pink]/// The paper one note is written on.////// Mine is always blue and theirs, in a one-to-one thread, always cream./// In a group each member gets a dye of their own: `assigned` is the/// thread's table from `groupDyes(_:)`, and a sender it does not name/// falls back to their preferred dye.staticfunc dye( mine: Bool, sender: String?, isGroup: Bool, assigned: [String: PaperDye] = [:] ) -> PaperDye {if mine { return .blue }guard isGroup else { return .cream }guardlet who = memberKey(sender) else { return .cream }return assigned[who] ?? memberDyes[preferredSlot(who)] }/// Every sender in a group thread, given a dye of their own.////// Each member PREFERS the dye a stable hash of who they are points at, so/// the same person is the same paper on every open and every device, and a/// newly loaded older message never shuffles everybody the way a/// first-seen order would. Two members preferring one dye would read as/// one person, though, so a contested dye goes to the alphabetically first/// of them and the others take the next free dye along the rotation. Only/// past five members does a dye repeat.staticfunc groupDyes(_ senders: [String?]) -> [String: PaperDye] {let members = Set(senders.compactMap(memberKey)).sorted()var out: [String: PaperDye] = [:]var taken = Set<Int>()var losers: [String] = []for who in members {let slot = preferredSlot(who)if taken.insert(slot).inserted { out[who] = memberDyes[slot] } else { losers.append(who) } }for who in losers {let start = preferredSlot(who)let free = (0..<memberDyes.count) .map { (start + $0) % memberDyes.count } .first { !taken.contains($0) }let slot = free ?? start taken.insert(slot) out[who] = memberDyes[slot] }return out }/// Case and surrounding space do not make a second person; a blank name/// is nobody.staticfunc memberKey(_ sender: String?) -> String? {let who = (sender ?? "").trimmingCharacters(in: .whitespacesAndNewlines).lowercased()return who.isEmpty ? nil : who }privatestaticfunc preferredSlot(_ key: String) -> Int {Int(CapColour.fnv1a(key) % UInt64(memberDyes.count)) }// ── When a message is a letter ────────────────────────────────────────/// A message this long is written as a letter: the same paper, page wide,/// growing with the words. Below it a note hugs its words.staticlet letterCharacters = 320/// …or one broken into this many lines, which reads as a letter however/// short the lines are.staticlet letterLines = 8/// Whether `body` is a letter. Never a page count: a letter is one sheet/// that grows, however long it is.staticfunc isLetter(_ body: String?) -> Bool {guardlet body else { returnfalse }let trimmed = body.trimmingCharacters(in: .whitespacesAndNewlines)if trimmed.count >= letterCharacters { returntrue }return trimmed.split(separator: "\n", omittingEmptySubsequences: false).count >= letterLines }// ── Several photographs ───────────────────────────────────────────────staticlet clusterMax = 4}