#if targetEnvironment(macCatalyst)
import Combine

// Scene lifetime is independent of focused commands: a connected window may
// still be authenticating, inactive, or minimized. The native Quick Question
// panel is not a workspace scene and does not count as an open window here.
@MainActor
public final class SybilMacWindowCommands: ObservableObject {
    public static let shared = SybilMacWindowCommands()

    @Published private var connectedWindowIDs: Set<String> = []

    init() {}

    var hasOpenWindows: Bool { !connectedWindowIDs.isEmpty }

    public func windowConnected(sessionID: String) {
        connectedWindowIDs.insert(sessionID)
    }

    public func windowDisconnected(sessionID: String) {
        connectedWindowIDs.remove(sessionID)
    }

    func canStartNewChat(hasFocusedActions: Bool) -> Bool {
        !hasOpenWindows || hasFocusedActions
    }

    func newChatOrWindow(newChat: (() -> Void)?, openWindow: () -> Void) {
        if hasOpenWindows {
            newChat?()
        } else {
            openWindow()
        }
    }
}
#endif
