import Foundation // Initial placement splits the newest column; native minimums can shift its divider. enum LanguageRestartOracle { static func nextLanguage(current: String, languages: [String]) throws -> String { guard let choice = ["fr", "ja"].first(where: { $0 != current && languages.contains($0) }) else { throw RealityError("No alternate shipped language is available for the restart check") } return choice } static func requireSavedChoice( _ choice: String, state: LanguagePreferenceReadback, saved: LanguagePreferenceSnapshot, needsRestart: Bool ) throws { guard state.accepted, state.languages.contains(choice), state.selection == choice, state.needsRestart == needsRestart, !state.isRestarting, saved.languages == [choice] else { throw RealityError("Language selection, saved preference, and restart state do not match") } } static func columns(visible: ScreenRect, folderMinimumWidth: Double, folderFirst: Bool = true) throws -> [ScreenRect] { try columns( visible: visible, folderMinimumWidth: folderMinimumWidth, order: folderFirst ? [0, 1, 2] : [0, 2, 1]) } private static func columns(visible: ScreenRect, folderMinimumWidth: Double, order: [Int]) throws -> [ScreenRect] { let usable = visible.inset(16) let primaryWidth = ((usable.width - 12) / 2).rounded() let secondarySpace = usable.width - primaryWidth - 24 guard visible.isValid, folderMinimumWidth.isFinite, folderMinimumWidth > 0, let folderColumn = order.firstIndex(of: 1) else { throw RealityError("Native minimum widths do not fit the language-restart columns") } let firstSecondary = (secondarySpace / 2).rounded() var widths = [primaryWidth, firstSecondary, secondarySpace - firstSecondary] if folderColumn == 0 { guard primaryWidth >= folderMinimumWidth else { throw RealityError("Native minimum widths do not fit the language-restart columns") } } else { widths[folderColumn] = max(widths[folderColumn], folderMinimumWidth) widths[3 - folderColumn] = secondarySpace - widths[folderColumn] } guard widths.allSatisfy({ $0 > 0 }) else { throw RealityError("Native minimum widths do not fit the language-restart columns") } var result = Array(repeating: usable, count: 3) var x = usable.x for (column, index) in order.enumerated() { result[index] = ScreenRect(x: x, y: usable.y, width: widths[column], height: usable.height) x += widths[column] + 12 } return result } static func arrangements(visible: ScreenRect, folderMinimumWidth: Double) throws -> [[ScreenRect]] { let orders = [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]] return try orders.map { try columns(visible: visible, folderMinimumWidth: folderMinimumWidth, order: $0) } } static func matches(_ frames: [ScreenRect], columns: [ScreenRect], terminalIncrement: ResizeIncrement) -> Bool { guard frames.count == 3, columns.count == 3 else { return false } return frames[0].matches(columns[0], tolerance: 1) && columns[0].contains(frames[0], tolerance: 0) && frames[1].matches(columns[1], tolerance: 1) && columns[1].contains(frames[1], tolerance: 0) && FrameOracle.incrementFit(frames[2], tile: columns[2], increment: terminalIncrement) } }