#!/usr/bin/env node
import { program } from 'commander';
import withProgressBar from './handlers/hofs/withProgressBar.js';
import init from './handlers/init.js';
import systemList from './handlers/systemList.js';
import systemInstall from './handlers/systemInstall.js';
import componentList from './handlers/componentList.js';
import componentInstall from './handlers/componentInstall.js';
import componentCreate from './handlers/componentCreate.js';
import CliError from './lib/CliError.js';
import log from './lib/log.js';
import { createRequire } from 'module';
import { cyan, green } from 'colorette';
import boxen from 'boxen';
const packageInfo = createRequire(import.meta.url)('../package.json');
program
    .enablePositionalOptions()
    .option('-c --checkout <commit/branch/tag>', 'Commit, branch or tag of the base repository that should be checked out');
program
    .command('init [name] [path]', {
    isDefault: true,
})
    .description('Initialize an Emulsify project')
    .option('-m --machineName <machineName>', 'Machine-friendly name of the project you are initializing. If not provided, this will be automatically generated.')
    .option('-s --starter <repository>', 'Git repository of the Emulsify starter you would like to use, such as the Emulsify Drupal theme: https://github.com/emulsify-ds/emulsify-starter')
    .option('-c --checkout <commit/branch/tag>', 'Commit, branch or tag of the base repository that should be checked out')
    .option('-p --platform <drupal/wordpress/etc>', 'Name of the platform Emulsify is being within. In some cases, Emulsify is able to automatically detect this. If it is not, Emulsify will prompt you to specify.')
    .option('-y --yes', 'Accept default init values for any missing options without prompting.')
    .action(withProgressBar(init));
const system = program
    .command('system')
    .description('Parent command that contains sub-commands pertaining to systems');
system
    .command('list')
    .description('Lists all of the available systems that Emulsify supports out-of-the-box')
    .alias('ls')
    .action(systemList);
system
    .command('install [name]')
    .description('Install a system within an Emulsify project. You must specify either the name of an out-of-the-box system (such as compound), or a link to a git repository containing the system you want to install')
    .option('-r --repository <repository>', 'Git repository containing the system you would like to install')
    .option('-c --checkout <commit/branch/tag>', 'Commit, branch or tag of the base repository that should be checked out. MUST be provided if you are passing along a repository (-r or --repository). Tags or commit hashes are strongly preferable, because you want to ensure that you are using the same version of the system every time you install components, etc')
    .option('-a --all', 'Use this to install all available components within the specified system. Without this flag, only the required system components will be installed.')
    .action(systemInstall);
const component = program
    .command('component')
    .description('Parent command that contains sub-commands pertaining to components');
component
    .command('list')
    .description('Lists all of the components that are available for installation within your project based on the system and variant you selected')
    .alias('ls')
    .action(componentList);
component
    .command('install [name]')
    .description("Install a component from within the current project's system and variant")
    .option('-f --force', 'Use this to overwrite a component that is already installed')
    .option('-a --all', 'Use this to install all available components, rather than specifying a single component to install')
    .option('--dry-run', 'Preview component installs without copying or removing files.')
    .alias('i')
    .action(componentInstall);
component
    .command('create [name]')
    .option('-d --directory <directory>', 'Used to set the directory where the new component is to be created')
    .option('-f --format <format>', 'Component format to generate. Supported values: default, sdc.')
    .option('-y --yes', 'Skip overwrite confirmation prompts and replace existing components.')
    .option('--dry-run', 'Preview generated component files without writing or removing files.')
    .alias('c')
    .description("Create a component from within the current project's system and variant")
    .action(componentCreate);
const title = cyan(packageInfo.productName);
const message = `Version: ${green(packageInfo.version)}`;
const boxedMessage = boxen(message, {
    title: title,
    backgroundColor: 'black',
    borderStyle: 'round',
    borderColor: 'blue',
    padding: 1,
    margin: 1,
});
program.version(boxedMessage);
try {
    await program.parseAsync(process.argv);
}
catch (err) {
    if (err instanceof CliError) {
        log('error', err.message);
        process.exitCode = err.exitCode;
    }
    else {
        const message = err instanceof Error ? err.message : String(err);
        log('error', message);
        process.exitCode = 1;
    }
}
