import { RemoteApprovalController } from './approval-controller.js';
import { createRemoteAwareApprovalGate } from './approval-gate.js';
import { createSessionBackedRemoteAutoApproveSettings } from './auto-approve-settings.js';
import { PhoneControlBrokerClient } from './broker/client.js';
import { DesktopRemoteControlRuntime } from './desktop-runtime.js';
import { TEAM_WHATSAPP_DISABLED_MESSAGE } from './policy.js';
import { ShortyPhoneManager } from './shorty-phone-manager.js';
const SHORTY_CONNECTED_INFO = 'ShortcutXL is connected to your phone via WhatsApp. Message yourself to use Shorty from your phone.';
const SHORTY_RELINK_INFO = 'ShortcutXL phone control needs relink. Run /shorty.';
export class RemoteControlBootstrap {
    settingsManager;
    autoApproveState;
    agentDir;
    log;
    approvalController;
    teamAccount;
    session;
    runtime;
    brokerClient;
    phoneManager;
    constructor(options) {
        this.settingsManager = options.settingsManager;
        this.autoApproveState = options.autoApproveState;
        this.agentDir = options.agentDir;
        this.log = options.log ?? (() => { });
        this.teamAccount = options.teamAccount === true;
        this.approvalController = new RemoteApprovalController({
            autoApproveSettings: createSessionBackedRemoteAutoApproveSettings(this.autoApproveState)
        });
    }
    createApprovalGate(fallback) {
        return createRemoteAwareApprovalGate({
            remote: this.approvalController,
            fallback,
            shouldUseRemote: () => this.runtime?.shouldRouteApprovalsToRemote() ?? false
        });
    }
    attachSession(session) {
        if (this.runtime || this.phoneManager) {
            throw new Error('Remote control bootstrap already attached to a session.');
        }
        this.runtime = new DesktopRemoteControlRuntime({
            session,
            approvalController: this.approvalController
        });
        this.session = session;
        this.brokerClient = new PhoneControlBrokerClient({
            agentDir: this.agentDir,
            runtime: this.runtime,
            session,
            log: this.log
        });
        this.phoneManager = new ShortyPhoneManager({
            brokerClient: this.brokerClient,
            teamAccount: this.teamAccount
        });
    }
    getPhoneManager() {
        if (!this.phoneManager) {
            throw new Error('Remote control bootstrap has not been attached to a session.');
        }
        return this.phoneManager;
    }
    async initializeInteractive() {
        if (!this.phoneManager || !this.settingsManager.getRemoteControlEnabled()) {
            return {
                startupInfos: [],
                startupWarnings: []
            };
        }
        if (this.teamAccount && this.settingsManager.getRemoteControlProvider() === 'whatsapp') {
            return {
                startupInfos: [],
                startupWarnings: [TEAM_WHATSAPP_DISABLED_MESSAGE]
            };
        }
        const startupInfos = [];
        const startupWarnings = [];
        const status = await this.phoneManager.autoStartConfiguredProvider();
        if (status.lastError) {
            startupWarnings.push(`ShortcutXL phone control unavailable: ${status.lastError}. Run /shorty to retry.`);
            return {
                startupInfos,
                startupWarnings
            };
        }
        if (this.settingsManager.getRemoteControlProvider() === 'whatsapp') {
            startupInfos.push(status.enabled && status.provider === 'whatsapp'
                ? { message: SHORTY_CONNECTED_INFO, tone: 'success' }
                : SHORTY_RELINK_INFO);
        }
        return {
            startupInfos,
            startupWarnings
        };
    }
    async close() {
        this.runtime?.close();
        this.runtime = undefined;
        this.session = undefined;
        if (this.phoneManager) {
            await this.phoneManager.close();
            this.phoneManager = undefined;
        }
        this.brokerClient = undefined;
    }
    async setTeamAccount(teamAccount) {
        this.teamAccount = teamAccount;
        await this.phoneManager?.setTeamAccount(teamAccount);
    }
}
//# sourceMappingURL=bootstrap.js.map