"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.installDashboardCliproxyVersion = void 0;
const binary_manager_1 = require("../../cliproxy/binary-manager");
const port_manager_1 = require("../../cliproxy/config/port-manager");
const service_manager_1 = require("../../cliproxy/service-manager");
const session_tracker_1 = require("../../cliproxy/session-tracker");
const stats_fetcher_1 = require("../../cliproxy/services/stats-fetcher");
const supervisord_lifecycle_1 = require("../../docker/supervisord-lifecycle");
const defaultDeps = {
    getProxyStatus: () => (0, session_tracker_1.getProxyStatus)((0, port_manager_1.resolveLifecyclePort)()),
    isCliproxyRunning: () => (0, stats_fetcher_1.isCliproxyRunning)((0, port_manager_1.resolveLifecyclePort)()),
    installCliproxyVersion: binary_manager_1.installCliproxyVersion,
    ensureCliproxyService: () => (0, service_manager_1.ensureCliproxyService)((0, port_manager_1.resolveLifecyclePort)()),
};
async function wasProxyRunning(deps) {
    const status = deps.getProxyStatus();
    if (status.running) {
        return true;
    }
    return deps.isCliproxyRunning();
}
async function installDashboardCliproxyVersion(version, backend, deps = defaultDeps) {
    const effectiveBackend = (0, binary_manager_1.resolveLocalBackend)(backend, { notifyOnPlus: true });
    const backendLabel = effectiveBackend === 'plus' ? 'CLIProxy Plus' : 'CLIProxy';
    const shouldRestoreService = await wasProxyRunning(deps);
    // The installer owns the stop-and-replace lifecycle, including best-effort
    // shutdown for tracked and untracked proxies before swapping the binary.
    await deps.installCliproxyVersion(version, true, effectiveBackend);
    if (!shouldRestoreService) {
        return {
            success: true,
            restarted: false,
            message: `Successfully installed ${backendLabel} v${version}`,
        };
    }
    // In Docker, supervisord owns process lifecycle — delegate restart to it
    if ((0, supervisord_lifecycle_1.isRunningUnderSupervisord)()) {
        const result = (0, supervisord_lifecycle_1.restartCliproxyViaSupervisord)();
        return {
            success: result.success,
            restarted: result.success,
            port: result.port,
            error: result.error,
            message: result.success
                ? `Successfully installed ${backendLabel} v${version} and restarted it on port ${result.port}`
                : `Installed ${backendLabel} v${version}, but restart failed`,
        };
    }
    const startResult = await deps.ensureCliproxyService();
    if (!startResult.started && !startResult.alreadyRunning) {
        return {
            success: false,
            restarted: false,
            error: startResult.error || `Installed ${backendLabel} v${version}, but restart failed`,
            message: `Installed ${backendLabel} v${version}, but failed to restart it`,
        };
    }
    return {
        success: true,
        restarted: true,
        port: startResult.port,
        message: `Successfully installed ${backendLabel} v${version} and restarted it on port ${startResult.port}`,
    };
}
exports.installDashboardCliproxyVersion = installDashboardCliproxyVersion;
//# sourceMappingURL=cliproxy-dashboard-install-service.js.map