"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SERVICE_REPO_STATES = exports.SERVICE_REPO_RESULTS = exports.SERVICE_PUBLIC_COMMAND_ACTIONS = exports.SERVICE_PROCESS_STATUSES = exports.SERVICE_EVENT_TYPES = exports.SERVICE_COMMAND_USAGE = exports.SERVICE_COMMAND_ACTIONS = exports.runServiceLoop = exports.isServiceCommandAction = exports.isPublicServiceCommandAction = void 0;
exports.runServiceCommand = runServiceCommand;
const locale_1 = require("../locale");
const log_inspection_1 = require("../log-inspection");
const install_1 = require("./install");
const service_runtime_1 = require("../service-runtime");
Object.defineProperty(exports, "runServiceLoop", { enumerable: true, get: function () { return service_runtime_1.runServiceLoop; } });
const shared_1 = require("./shared");
Object.defineProperty(exports, "isPublicServiceCommandAction", { enumerable: true, get: function () { return shared_1.isPublicServiceCommandAction; } });
Object.defineProperty(exports, "isServiceCommandAction", { enumerable: true, get: function () { return shared_1.isServiceCommandAction; } });
Object.defineProperty(exports, "SERVICE_COMMAND_ACTIONS", { enumerable: true, get: function () { return shared_1.SERVICE_COMMAND_ACTIONS; } });
Object.defineProperty(exports, "SERVICE_COMMAND_USAGE", { enumerable: true, get: function () { return shared_1.SERVICE_COMMAND_USAGE; } });
Object.defineProperty(exports, "SERVICE_EVENT_TYPES", { enumerable: true, get: function () { return shared_1.SERVICE_EVENT_TYPES; } });
Object.defineProperty(exports, "SERVICE_PROCESS_STATUSES", { enumerable: true, get: function () { return shared_1.SERVICE_PROCESS_STATUSES; } });
Object.defineProperty(exports, "SERVICE_PUBLIC_COMMAND_ACTIONS", { enumerable: true, get: function () { return shared_1.SERVICE_PUBLIC_COMMAND_ACTIONS; } });
Object.defineProperty(exports, "SERVICE_REPO_RESULTS", { enumerable: true, get: function () { return shared_1.SERVICE_REPO_RESULTS; } });
Object.defineProperty(exports, "SERVICE_REPO_STATES", { enumerable: true, get: function () { return shared_1.SERVICE_REPO_STATES; } });
async function runServiceCommand(options, deps = {}) {
    switch (options.action) {
        case shared_1.SERVICE_COMMAND_ACTIONS.enable:
            return (0, install_1.runServiceEnableCommand)(options.homeDirectory, deps);
        case shared_1.SERVICE_COMMAND_ACTIONS.disable:
            return (0, install_1.runServiceDisableCommand)(options.homeDirectory, deps, {
                force: options.force === true,
            });
        case shared_1.SERVICE_COMMAND_ACTIONS.restart:
            return (0, install_1.runServiceRestartCommand)(options.homeDirectory, deps, {
                force: options.force === true,
            });
        case shared_1.SERVICE_COMMAND_ACTIONS.status:
            return (0, service_runtime_1.runServiceStatusCommand)(options.homeDirectory);
        case shared_1.SERVICE_COMMAND_ACTIONS.logShow:
            return runServiceLogShowCommand(options.homeDirectory, options.tail);
        case shared_1.SERVICE_COMMAND_ACTIONS.run:
            return (0, service_runtime_1.runServiceLoop)(options.homeDirectory ? { homeDirectory: options.homeDirectory } : {}, deps);
    }
}
async function runServiceLogShowCommand(homeDirectory, tail) {
    const locale = await (0, locale_1.loadCommandLocale)(process.cwd());
    const text = await (0, log_inspection_1.readServiceLogText)(homeDirectory);
    if (text === null) {
        console.log((0, log_inspection_1.renderEmptyServiceLog)(locale));
        return 0;
    }
    process.stdout.write((0, log_inspection_1.sliceTextTail)(text, tail));
    return 0;
}
