/**
 * @license
 * Copyright 2025 HopCode Team
 * SPDX-License-Identifier: Apache-2.0
 */
import { getErrorMessage } from '../../utils/errors.js';
import { writeStdoutLine, writeStderrLine } from '../../utils/stdioHelpers.js';
import { ExtensionManager } from '@hoptrendy/hopcode-core';
import { requestConsentNonInteractive, requestConsentOrFail, } from './consent.js';
import { isWorkspaceTrusted } from '../../config/trustedFolders.js';
import { loadSettings } from '../../config/settings.js';
import { t } from '../../i18n/index.js';
export async function handleUninstall(args) {
    try {
        const workspaceDir = process.cwd();
        const extensionManager = new ExtensionManager({
            workspaceDir,
            requestConsent: requestConsentOrFail.bind(null, requestConsentNonInteractive),
            isWorkspaceTrusted: !!isWorkspaceTrusted(loadSettings(workspaceDir).merged),
        });
        await extensionManager.refreshCache();
        await extensionManager.uninstallExtension(args.name, false);
        writeStdoutLine(t('Extension "{{name}}" successfully uninstalled.', { name: args.name }));
    }
    catch (error) {
        writeStderrLine(getErrorMessage(error));
        process.exit(1);
    }
}
export const uninstallCommand = {
    command: 'uninstall <name>',
    describe: t('Uninstalls an extension.'),
    builder: (yargs) => yargs
        .positional('name', {
        describe: t('The name or source path of the extension to uninstall.'),
        type: 'string',
    })
        .check((argv) => {
        if (!argv.name) {
            throw new Error(t('Please include the name of the extension to uninstall as a positional argument.'));
        }
        return true;
    }),
    handler: async (argv) => {
        await handleUninstall({
            name: argv['name'],
        });
    },
};
//# sourceMappingURL=uninstall.js.map