"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SERVICE_MANAGER_KINDS = exports.SERVICE_MANAGER_COMMANDS = void 0;
exports.detectServiceManager = detectServiceManager;
exports.getKnownServiceDefinitionPaths = getKnownServiceDefinitionPaths;
const node_os_1 = __importDefault(require("node:os"));
const catalog_1 = require("../i18n/catalog");
const command_1 = require("./command");
const branding_1 = require("./branding");
const paths_1 = require("./paths");
exports.SERVICE_MANAGER_COMMANDS = {
    launchctl: "launchctl",
    systemctl: "systemctl",
};
exports.SERVICE_MANAGER_KINDS = {
    launchd: "launchd",
    systemdUser: "systemd-user",
    unsupported: "unsupported",
};
async function detectServiceManager(options = {}) {
    const platform = options.platform ?? process.platform;
    const homeDirectory = options.homeDirectory ?? node_os_1.default.homedir();
    const locale = options.locale ?? catalog_1.DEFAULT_APP_LOCALE;
    const hasCommand = options.commandExists ?? command_1.commandExists;
    const run = options.runCommand ?? command_1.runCommand;
    if (platform === "darwin") {
        if (!(await hasCommand(exports.SERVICE_MANAGER_COMMANDS.launchctl))) {
            return unsupportedServiceManager(platform, (0, catalog_1.translate)(locale, "serviceManager.reason.launchctlMissing"), locale);
        }
        const uid = options.uid ?? process.getuid?.();
        if (uid === undefined) {
            return unsupportedServiceManager(platform, (0, catalog_1.translate)(locale, "serviceManager.reason.launchctlUidMissing"), locale);
        }
        const domain = `gui/${uid}`;
        return {
            kind: exports.SERVICE_MANAGER_KINDS.launchd,
            supported: true,
            platform,
            description: (0, catalog_1.translate)(locale, "serviceManager.description.launchd"),
            definitionPath: (0, paths_1.getMacOsLaunchAgentPath)(homeDirectory),
            launchctlDomain: domain,
            launchctlTarget: `${domain}/${branding_1.SERVICE_LABEL}`,
        };
    }
    if (platform === "linux") {
        if (!(await hasCommand(exports.SERVICE_MANAGER_COMMANDS.systemctl))) {
            return unsupportedLinuxServiceManager(options, locale);
        }
        try {
            await run({
                command: exports.SERVICE_MANAGER_COMMANDS.systemctl,
                args: ["--user", "show-environment"],
                timeoutMs: 10_000,
            });
        }
        catch {
            return unsupportedLinuxServiceManager(options, locale);
        }
        return {
            kind: exports.SERVICE_MANAGER_KINDS.systemdUser,
            supported: true,
            platform,
            description: (0, catalog_1.translate)(locale, "serviceManager.description.systemdUser"),
            definitionPath: (0, paths_1.getLinuxSystemdUserServicePath)(homeDirectory),
        };
    }
    return unsupportedServiceManager(platform, (0, catalog_1.translate)(locale, "serviceManager.reason.platformUnsupported", {
        platform,
    }), locale);
}
function getKnownServiceDefinitionPaths(homeDirectory = node_os_1.default.homedir()) {
    return [
        (0, paths_1.getMacOsLaunchAgentPath)(homeDirectory),
        (0, paths_1.getLinuxSystemdUserServicePath)(homeDirectory),
    ];
}
function unsupportedLinuxServiceManager(options, locale) {
    const release = options.release ?? node_os_1.default.release();
    const env = options.env ?? process.env;
    const { WSL_DISTRO_NAME: wslDistroName } = env;
    const isWsl = Boolean(wslDistroName) || release.toLowerCase().includes("microsoft");
    return unsupportedServiceManager(options.platform ?? process.platform, isWsl
        ? (0, catalog_1.translate)(locale, "serviceManager.reason.wslRequiresSystemctlUser")
        : (0, catalog_1.translate)(locale, "serviceManager.reason.linuxRequiresSystemctlUser"), locale);
}
function unsupportedServiceManager(platform, reason, locale) {
    return {
        kind: exports.SERVICE_MANAGER_KINDS.unsupported,
        supported: false,
        platform,
        description: (0, catalog_1.translate)(locale, "serviceManager.description.unsupported"),
        reason,
    };
}
