"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.removePidFile = exports.writePidToFile = exports.getPidFromFile = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const config_loader_facade_1 = require("../config/config-loader-facade");
function getCursorDir() {
    return path.join((0, config_loader_facade_1.getCcsDir)(), 'cursor');
}
function getPidFilePath() {
    return path.join(getCursorDir(), 'daemon.pid');
}
function getPidFromFile() {
    const pidFile = getPidFilePath();
    try {
        if (fs.existsSync(pidFile)) {
            const content = fs.readFileSync(pidFile, 'utf8').trim();
            const pid = parseInt(content, 10);
            return isNaN(pid) ? null : pid;
        }
    }
    catch {
        // Ignore errors
    }
    return null;
}
exports.getPidFromFile = getPidFromFile;
function writePidToFile(pid) {
    const pidFile = getPidFilePath();
    try {
        const dir = path.dirname(pidFile);
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
        }
        fs.writeFileSync(pidFile, pid.toString(), { mode: 0o600 });
    }
    catch {
        // Ignore errors
    }
}
exports.writePidToFile = writePidToFile;
function removePidFile() {
    const pidFile = getPidFilePath();
    try {
        if (fs.existsSync(pidFile)) {
            fs.unlinkSync(pidFile);
        }
    }
    catch {
        // Ignore errors
    }
}
exports.removePidFile = removePidFile;
//# sourceMappingURL=cursor-daemon-pid.js.map