#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.main = main;
const configuration_file_1 = require("../apps/configuration-file");
const manifest_1 = require("../apps/manifest");
const workspace_app_state_1 = require("../apps/workspace-app-state");
const rtws_1 = require("../rtws");
const apps_cli_hints_1 = require("./apps-cli-hints");
function printHelp() {
    console.log(`Usage:
  dominds enable <appId>

Notes:
  - enable only removes the app from .apps/configuration.yaml.disabledApps and refreshes derived state.
  - ${(0, apps_cli_hints_1.formatMutationBoundaryNote)({
        commandName: 'enable',
        layerDescription: '.apps/configuration.yaml.disabledApps',
    })}
`);
}
function parseArgs(argv) {
    const positional = [];
    for (const arg of argv) {
        if (arg === '--help' || arg === '-h') {
            printHelp();
            process.exit(0);
        }
        if (arg.startsWith('-'))
            throw new Error(`Unknown option: ${arg}`);
        positional.push(arg);
    }
    if (positional.length !== 1)
        throw new Error('enable requires exactly one <appId>');
    return { appId: positional[0] };
}
async function main(argv = process.argv.slice(2)) {
    let args;
    try {
        args = parseArgs(argv);
    }
    catch (err) {
        console.error(err instanceof Error ? err.message : String(err));
        printHelp();
        process.exit(1);
        return;
    }
    const rtwsRootAbs = (0, rtws_1.domindsRtwsRootAbs)();
    if (!(await (0, manifest_1.hasRtwsDeclaredAppDependency)({ rtwsRootAbs, appId: args.appId }))) {
        console.error(`Error: app '${args.appId}' is not declared in .minds/app.yaml dependencies`);
        console.error((0, apps_cli_hints_1.formatDoctorGuidance)({ appId: args.appId }));
        process.exit(1);
        return;
    }
    const loadedConfig = await (0, configuration_file_1.loadAppsConfigurationFile)({ rtwsRootAbs });
    if (loadedConfig.kind === 'error') {
        console.error(`Error: failed to read .apps/configuration.yaml: ${loadedConfig.errorText}`);
        console.error((0, apps_cli_hints_1.formatDoctorGuidance)({ appId: args.appId }));
        process.exit(1);
        return;
    }
    const nextConfig = (0, configuration_file_1.setAppDisabledInConfiguration)({
        existing: loadedConfig.file,
        appId: args.appId,
        disabled: false,
    });
    await (0, configuration_file_1.writeAppsConfigurationFileIfChanged)({ rtwsRootAbs, file: nextConfig });
    await (0, workspace_app_state_1.refreshAppsDerivedState)({ rtwsRootAbs });
    console.log(`Enabled app '${args.appId}' in .apps/configuration.yaml and refreshed derived state.`);
    console.log((0, apps_cli_hints_1.formatMutationBoundaryNote)({
        commandName: 'enable',
        layerDescription: '.apps/configuration.yaml.disabledApps',
        appId: args.appId,
    }));
}
if (require.main === module) {
    main().catch((err) => {
        console.error('Unhandled error:', err);
        process.exit(1);
    });
}
