import Foundation /// The task page's card belongs to its creator. Unknown card kinds and /// options stay on the web until the native client understands their meaning. struct TaskDecision { var card: ThinCard? var choices: [DecisionView.Choice] = [] init() {} init(task: ThinTask, cards: [ThinCard], mine: Bool, owner: Bool) { guard mine, owner, !["done", "cancelled", "merging"].contains(task.stage), task.moving != true, task.stopping != true, task.copyHold != true, task.copyCanSkip != true else { return } let openCards = cards.filter { $0.taskId == task.id && $0.isOpen } .sorted { ($0.createdAt, $0.id) < ($1.createdAt, $1.id) } card = openCards.last { ["merge", "direction", "held"].contains($0.kind) } ?? openCards.last { $0.kind == "question" } guard let card else { return } if card.kind == "merge", task.kind == "explore" { self.card = nil; return } // Questions carry model-written options, rendered by LiveQuestionCard. guard card.kind != "question" else { return } if let decision = Feed.decision(card, task: task), decision.open { choices = decision.choices; return } let allowed: Set switch card.kind { case "direction": allowed = ["build", "again", "drop"] case "held": allowed = ["run_now", "wait"] default: return } var result: [DecisionView.Choice] = [] for value in card.options ?? [] { guard allowed.contains(value), let label = Feed.webText("chat.card." + value) else { return } result.append(.init(value: value, label: label, primary: value == "build" || value == "run_now")) } choices = result } func canAnswer(_ choice: String, note: String) -> Bool { guard choices.contains(where: { $0.value == choice }), note.count <= 1000 else { return false } return card?.kind != "direction" || choice != "again" || !note.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } }