"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CELL_AUTOSTART_TASK = void 0;
exports.getCellCliPath = getCellCliPath;
exports.getAutostartLauncherPath = getAutostartLauncherPath;
exports.getWindowsStartupPath = getWindowsStartupPath;
exports.getAutostartCommand = getAutostartCommand;
exports.registerAutostart = registerAutostart;
exports.unregisterAutostart = unregisterAutostart;
exports.checkAutostart = checkAutostart;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const child_process_1 = require("child_process");
exports.CELL_AUTOSTART_TASK = 'FivoCellDaemon';
function getCellCliPath() {
    return path.resolve(__dirname, '..', '..', '..', '..', 'bin', 'cell.js');
}
function getAutostartLauncherPath() {
    return path.join(process.env.USERPROFILE || os.homedir(), '.fivo', 'cell', 'autostart.cmd');
}
function getWindowsStartupPath() {
    return path.join(process.env.APPDATA || path.join(process.env.USERPROFILE || os.homedir(), 'AppData', 'Roaming'), 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup', `${exports.CELL_AUTOSTART_TASK}.cmd`);
}
function getAutostartCommand(opts = {}) {
    const nodePath = opts.nodePath || process.execPath;
    const cliPath = opts.cliPath || getCellCliPath();
    return `"${nodePath}" "${cliPath}" start --from-autostart`;
}
function writeAutostartLauncher(opts = {}) {
    const launcherPath = getAutostartLauncherPath();
    const logPath = path.join(path.dirname(launcherPath), 'autostart.log');
    const command = getAutostartCommand(opts);
    fs.mkdirSync(path.dirname(launcherPath), { recursive: true });
    fs.writeFileSync(launcherPath, `@echo off\r\n${command} >> "${logPath}" 2>&1\r\n`, 'utf8');
    return launcherPath;
}
function registerAutostart(opts = {}) {
    const platform = opts.platform || process.platform;
    const command = platform === 'win32' ? `"${getWindowsStartupPath()}"` : getAutostartCommand(opts);
    if (platform !== 'win32') {
        return { ok: true, skipped: true, message: 'Autostart is only configured automatically on Windows.', command };
    }
    if (opts.dryRun) {
        return { ok: true, skipped: true, message: `Would register Windows startup launcher: ${exports.CELL_AUTOSTART_TASK}`, command };
    }
    try {
        const launcherPath = writeAutostartLauncher(opts);
        const startupPath = getWindowsStartupPath();
        fs.mkdirSync(path.dirname(startupPath), { recursive: true });
        fs.writeFileSync(startupPath, `@echo off\r\ncall "${launcherPath}"\r\n`, 'utf8');
        return { ok: true, skipped: false, message: `Windows autostart enabled (${exports.CELL_AUTOSTART_TASK}).`, command: `"${startupPath}"` };
    }
    catch (e) {
        const msg = e instanceof Error ? e.message : String(e);
        return { ok: false, skipped: false, message: `Windows autostart failed: ${msg}`, command };
    }
}
function unregisterAutostart(opts = {}) {
    const platform = opts.platform || process.platform;
    const command = getAutostartCommand(opts);
    if (platform !== 'win32') {
        return { ok: true, skipped: true, message: 'Autostart is only configured automatically on Windows.', command };
    }
    if (opts.dryRun) {
        return { ok: true, skipped: true, message: `Would remove Windows startup launcher: ${exports.CELL_AUTOSTART_TASK}`, command };
    }
    try {
        fs.rmSync(getWindowsStartupPath(), { force: true });
        try {
            (0, child_process_1.execFileSync)('schtasks', ['/Delete', '/TN', exports.CELL_AUTOSTART_TASK, '/F'], {
                stdio: 'ignore',
                windowsHide: true,
            });
        }
        catch { }
        return { ok: true, skipped: false, message: `Windows autostart disabled (${exports.CELL_AUTOSTART_TASK}).`, command };
    }
    catch {
        return { ok: true, skipped: false, message: 'Windows autostart was not registered.', command };
    }
}
function hasLegacyScheduledTask() {
    try {
        (0, child_process_1.execFileSync)('schtasks', ['/Query', '/TN', exports.CELL_AUTOSTART_TASK], {
            stdio: 'ignore',
            windowsHide: true,
        });
        return true;
    }
    catch {
        return false;
    }
}
function checkAutostart(opts = {}) {
    const platform = opts.platform || process.platform;
    const command = getAutostartCommand(opts);
    if (platform !== 'win32') {
        return { ok: true, skipped: true, message: 'Autostart is only configured automatically on Windows.', command };
    }
    try {
        if (fs.existsSync(getWindowsStartupPath()) || hasLegacyScheduledTask()) {
            return { ok: true, skipped: false, message: `Windows autostart enabled (${exports.CELL_AUTOSTART_TASK}).`, command };
        }
        return { ok: false, skipped: false, message: 'Windows autostart is not registered.', command };
    }
    catch {
        return { ok: false, skipped: false, message: 'Windows autostart is not registered.', command };
    }
}
//# sourceMappingURL=autostart.js.map