import { spawn } from 'node:child_process';
import { existsSync } from 'node:fs';
import path from 'node:path';
function appDir() {
    return process.env.NTERMINAL_APP_DIR ? path.resolve(process.env.NTERMINAL_APP_DIR) : process.cwd();
}
export function runRuntimeUninstall(cwd = appDir()) {
    const controlScript = path.join(cwd, 'scripts', 'nterminalctl');
    if (!existsSync(controlScript)) {
        return {
            ok: false,
            message: 'nterminalctl not found; reinstall nterminal or run nterminal uninstall --force on the secondary.',
            disconnecting: false
        };
    }
    if (process.env.NTERMINAL_UNINSTALL_DRY_RUN === '1') {
        return {
            ok: true,
            message: 'Clean uninstall scheduled.',
            disconnecting: true
        };
    }
    try {
        const child = spawn('sh', ['-c', 'sleep 0.2; exec bash "$1" uninstall --force', 'nterminal-uninstall', controlScript], {
            cwd,
            detached: true,
            env: process.env,
            stdio: 'ignore'
        });
        child.unref();
        return {
            ok: true,
            message: 'Clean uninstall scheduled.',
            disconnecting: true
        };
    }
    catch (error) {
        return {
            ok: false,
            message: error instanceof Error ? error.message : 'uninstall_schedule_failed',
            disconnecting: false
        };
    }
}
//# sourceMappingURL=runtimeUninstall.js.map