/**
 * @license
 * Copyright 2025 HopCode Team
 * SPDX-License-Identifier: Apache-2.0
 */
import { ExtensionManager, parseInstallSource } from '@hoptrendy/hopcode-core';
import { getErrorMessage } from '../../utils/errors.js';
import { writeStdoutLine, writeStderrLine } from '../../utils/stdioHelpers.js';
import { isWorkspaceTrusted } from '../../config/trustedFolders.js';
import { loadSettings } from '../../config/settings.js';
import { requestConsentOrFail, requestConsentNonInteractive, requestChoicePluginNonInteractive, } from './consent.js';
import { t } from '../../i18n/index.js';
export async function handleInstall(args) {
    try {
        const installMetadata = await parseInstallSource(args.source);
        if (installMetadata.type !== 'git' &&
            installMetadata.type !== 'github-release' &&
            installMetadata.type !== 'npm') {
            if (args.ref || args.autoUpdate) {
                throw new Error(t('--ref and --auto-update are not applicable for marketplace extensions.'));
            }
        }
        if (installMetadata.type === 'npm' && args.ref) {
            throw new Error(t('--ref is not applicable for npm extensions. Use @version suffix instead (e.g. @scope/package@1.2.0).'));
        }
        if (installMetadata.type !== 'npm' && args.registry) {
            throw new Error(t('--registry is only applicable for npm extensions.'));
        }
        if (installMetadata.type === 'npm' && args.registry) {
            installMetadata.registryUrl = args.registry;
        }
        const requestConsent = args.consent
            ? () => Promise.resolve()
            : requestConsentOrFail.bind(null, requestConsentNonInteractive);
        const workspaceDir = process.cwd();
        const extensionManager = new ExtensionManager({
            workspaceDir,
            isWorkspaceTrusted: !!isWorkspaceTrusted(loadSettings(workspaceDir).merged),
            requestConsent,
            requestChoicePlugin: requestChoicePluginNonInteractive,
        });
        await extensionManager.refreshCache();
        const extension = await extensionManager.installExtension({
            ...installMetadata,
            ref: args.ref,
            autoUpdate: args.autoUpdate,
            allowPreRelease: args.allowPreRelease,
        }, requestConsent);
        writeStdoutLine(t('Extension "{{name}}" installed successfully and enabled.', {
            name: extension.name,
        }));
    }
    catch (error) {
        writeStderrLine(getErrorMessage(error));
        process.exit(1);
    }
}
export const installCommand = {
    command: 'install <source>',
    describe: t('Installs an extension from a git repository URL, local path, scoped npm package (@scope/name), or claude marketplace (marketplace-url:plugin-name).'),
    builder: (yargs) => yargs
        .positional('source', {
        describe: t('The github URL, local path, or marketplace source (marketplace-url:plugin-name) of the extension to install.'),
        type: 'string',
        demandOption: true,
    })
        .option('ref', {
        describe: t('The git ref to install from.'),
        type: 'string',
    })
        .option('auto-update', {
        describe: t('Enable auto-update for this extension.'),
        type: 'boolean',
    })
        .option('pre-release', {
        describe: t('Enable pre-release versions for this extension.'),
        type: 'boolean',
    })
        .option('registry', {
        describe: t('Custom npm registry URL (only for npm extensions).'),
        type: 'string',
    })
        .option('consent', {
        describe: t('Acknowledge the security risks of installing an extension and skip the confirmation prompt.'),
        type: 'boolean',
        default: false,
    })
        .check((argv) => {
        if (!argv.source) {
            throw new Error(t('The source argument must be provided.'));
        }
        return true;
    }),
    handler: async (argv) => {
        await handleInstall({
            source: argv['source'],
            ref: argv['ref'],
            autoUpdate: argv['auto-update'],
            allowPreRelease: argv['pre-release'],
            consent: argv['consent'],
            registry: argv['registry'],
        });
    },
};
//# sourceMappingURL=install.js.map