import Foundation struct NotationMove: Equatable, Identifiable { let id: Int let label: String } struct NotationMoveRow: Equatable, Identifiable { let id: String let number: UInt32 let white: NotationMove? let black: NotationMove? let indent: Int } struct NotationComment: Equatable, Identifiable { let id: String let text: String let indent: Int } enum NotationToken: Equatable, Identifiable { case move(NotationMove) case comment(id: String, text: String) case punctuation(id: String, text: String) var id: String { switch self { case .move(let move): return "move-\(move.id)" case .comment(let id, _), .punctuation(let id, _): return id } } } struct NotationVariation: Equatable, Identifiable { let id: String let startNodeID: Int let tokens: [NotationToken] let indent: Int } struct NotationResult: Equatable, Identifiable { let id: String let text: String let indent: Int } enum NotationEntry: Equatable, Identifiable { case comment(NotationComment) case moves(NotationMoveRow) case variation(NotationVariation) case result(NotationResult) var id: String { switch self { case .comment(let entry): return entry.id case .moves(let entry): return entry.id case .variation(let entry): return entry.id case .result(let entry): return entry.id } } var indent: Int { switch self { case .comment(let entry): return entry.indent case .moves(let entry): return entry.indent case .variation(let entry): return entry.indent case .result(let entry): return entry.indent } } } /// Complete PGN presentation: a two-column main line and every alternative subtree as /// clickable tokens. IDs are MoveNode.id, never visible row offsets. struct NotationModel { let entries: [NotationEntry] let entryByNode: [Int: String] var moveIDs: [Int] { entries.flatMap { entry -> [Int] in switch entry { case .moves(let row): return [row.white?.id, row.black?.id].compactMap { $0 } case .variation(let variation): return variation.tokens.compactMap { if case .move(let move) = $0 { return move.id } return nil } case .comment, .result: return [] } } } init(game: GameData) { var entries: [NotationEntry] = [] var entryByNode: [Int: String] = [:] var visited: Set = [0] if let root = game.nodes.first, !root.commentAfter.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { entries.append(.comment(NotationComment( id: "comment-root-after", text: root.commentAfter, indent: 0 ))) } var pendingRow: PendingRow? func flushRow() { guard let row = pendingRow else { return } let entry = NotationMoveRow( id: row.id, number: row.number, white: row.white.map { NotationMove(id: $0, label: game.nodes[$0].san + game.nodes[$0].nagSymbols) }, black: row.black.map { NotationMove(id: $0, label: game.nodes[$0].san + game.nodes[$0].nagSymbols) }, indent: 0 ) entries.append(.moves(entry)) if let white = row.white { entryByNode[white] = row.id } if let black = row.black { entryByNode[black] = row.id } pendingRow = nil } var parent = 0 for _ in 0.., entryByNode: inout [Int: String] ) -> NotationVariation? { var tokens: [NotationToken] = [] func appendComment(_ text: String, suffix: String) { guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return } tokens.append(.comment(id: "\(entryID)-comment-\(suffix)-\(tokens.count)", text: text)) } func appendPunctuation(_ text: String) { tokens.append(.punctuation(id: "\(entryID)-punctuation-\(tokens.count)", text: text)) } func line(first: Int) { var id = first var showNumber = true for _ in 0..