"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyDaemonOwnership = exports.verifyProcessOwnership = void 0;
const child_process_1 = require("child_process");
const fs = __importStar(require("fs"));
function sleepSync(milliseconds) {
    Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds);
}
function getProcessCommandLine(pid) {
    if (process.platform === 'linux') {
        for (let attempt = 0; attempt < 3; attempt++) {
            try {
                // /proc cmdline uses null separators between arguments.
                const commandLine = fs
                    .readFileSync(`/proc/${pid}/cmdline`, 'utf8')
                    .replace(/\0/g, ' ')
                    .trim();
                if (commandLine) {
                    return commandLine;
                }
            }
            catch {
                return null;
            }
            sleepSync(25);
        }
        return null;
    }
    if (process.platform === 'darwin') {
        try {
            const result = (0, child_process_1.spawnSync)('ps', ['-p', String(pid), '-o', 'command='], {
                encoding: 'utf8',
            });
            if (result.error || result.status !== 0) {
                return null;
            }
            return result.stdout.trim();
        }
        catch {
            return null;
        }
    }
    if (process.platform === 'win32') {
        const command = `(Get-CimInstance Win32_Process -Filter "ProcessId = ${pid}" | Select-Object -ExpandProperty CommandLine)`;
        const shells = ['powershell.exe', 'powershell', 'pwsh.exe', 'pwsh'];
        for (const shell of shells) {
            try {
                const result = (0, child_process_1.spawnSync)(shell, ['-NoProfile', '-Command', command], {
                    encoding: 'utf8',
                });
                if (result.error) {
                    continue;
                }
                if (result.status !== 0) {
                    return null;
                }
                return result.stdout.trim();
            }
            catch {
                // Try next shell candidate
            }
        }
        return null;
    }
    return null;
}
function verifyProcessOwnership(pid, ownershipMatcher) {
    try {
        process.kill(pid, 0);
    }
    catch (err) {
        const error = err;
        if (error.code === 'ESRCH') {
            return 'not-running';
        }
        return 'unknown';
    }
    const commandLine = getProcessCommandLine(pid);
    if (!commandLine) {
        return 'unknown';
    }
    return ownershipMatcher(commandLine) ? 'owned' : 'not-owned';
}
exports.verifyProcessOwnership = verifyProcessOwnership;
function verifyDaemonOwnership(pid) {
    return verifyProcessOwnership(pid, (commandLine) => commandLine.includes('--ccs-daemon') && commandLine.includes('cursor-daemon-entry'));
}
exports.verifyDaemonOwnership = verifyDaemonOwnership;
//# sourceMappingURL=daemon-process-ownership.js.map