// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var InitCommand_1;
import { BaseCommand } from '../base.js';
import fs from 'node:fs';
import * as constants from '../../core/constants.js';
import { SoloError } from '../../core/errors/solo-error.js';
import { Flags as flags } from '../flags.js';
import chalk from 'chalk';
import { PathEx } from '../../business/utils/path-ex.js';
import { injectable } from 'tsyringe-neo';
/**
 * Defines the core functionalities of 'init' command
 */
let InitCommand = class InitCommand extends BaseCommand {
    static { InitCommand_1 = this; }
    static COMMAND_NAME = 'init';
    static INIT_COMMAND_NAME = InitCommand_1.COMMAND_NAME;
    // Although empty, tsyringe requires the constructor to be present
    constructor() {
        super();
    }
    /** Executes the init CLI command */
    async init(argv) {
        const self = this;
        let cacheDirectory = this.configManager.getFlag(flags.cacheDir);
        if (!cacheDirectory) {
            cacheDirectory = constants.SOLO_CACHE_DIR;
        }
        const tasks = this.taskList.newTaskList([
            {
                title: 'Setup home directory and cache',
                task: async (context_, task) => {
                    self.configManager.update(argv);
                    context_.dirs = this.setupHomeDirectory();
                    let username = self.configManager.getFlag(flags.username);
                    if (username && !flags.username.validate(username)) {
                        username = await flags.username.prompt(task, username);
                    }
                    context_.config = { username };
                },
            },
            {
                title: 'Check dependencies',
                task: (_, task) => {
                    const deps = [constants.HELM, constants.KUBECTL];
                    const subTasks = self.depManager.taskCheckDependencies(deps);
                    // set up the sub-tasks
                    return task.newListr(subTasks, {
                        concurrent: true,
                        rendererOptions: {
                            collapseSubtasks: false,
                        },
                    });
                },
            },
            {
                title: 'Create local configuration',
                skip: () => this.localConfig.configFileExists(),
                task: async () => {
                    await this.localConfig.load();
                },
            },
            {
                title: 'Setup chart manager',
                task: async (context_) => {
                    context_.repoURLs = await this.chartManager.setup();
                },
            },
            {
                title: `Copy templates in '${cacheDirectory}'`,
                task: context_ => {
                    const resources = ['templates', 'profiles'];
                    for (const directoryName of resources) {
                        const sourceDirectory = PathEx.safeJoinWithBaseDirConfinement(constants.RESOURCES_DIR, constants.RESOURCES_DIR, directoryName);
                        if (!fs.existsSync(sourceDirectory)) {
                            continue;
                        }
                        const destinationDirectory = PathEx.join(cacheDirectory, directoryName);
                        if (!fs.existsSync(destinationDirectory)) {
                            fs.mkdirSync(destinationDirectory, { recursive: true });
                        }
                        fs.cpSync(sourceDirectory, destinationDirectory, { recursive: true });
                    }
                    if (argv.dev) {
                        self.logger.showList('Home Directories', context_.dirs);
                        self.logger.showList('Chart Repository', context_.repoURLs);
                    }
                    self.logger.showUser(chalk.grey('\n***************************************************************************************'));
                    self.logger.showUser(chalk.grey(`Note: solo stores various artifacts (config, logs, keys etc.) in its home directory: ${constants.SOLO_HOME_DIR}\n` +
                        "If a full reset is needed, delete the directory or relevant sub-directories before running 'solo init'."));
                    self.logger.showUser(chalk.grey('***************************************************************************************'));
                },
            },
        ], {
            concurrent: false,
            rendererOptions: constants.LISTR_DEFAULT_RENDERER_OPTION,
        }, undefined, InitCommand_1.INIT_COMMAND_NAME);
        if (tasks.isRoot()) {
            try {
                await tasks.run();
            }
            catch (error) {
                throw new SoloError('Error running init', error);
            }
        }
        return true;
    }
    /**
     * Return Yargs command definition for 'init' command
     * @returns A object representing the Yargs command definition
     */
    getCommandDefinition() {
        const self = this;
        return {
            command: InitCommand_1.COMMAND_NAME,
            desc: 'Initialize local environment',
            builder: (y) => {
                // set the quiet flag even though it isn't used for consistency across all commands
                flags.setOptionalCommandFlags(y, flags.cacheDir, flags.quiet, flags.username);
            },
            handler: async (argv) => {
                await self
                    .init(argv)
                    .then(r => {
                    if (!r) {
                        throw new SoloError('Error running init, expected return value to be true');
                    }
                })
                    .catch(error => {
                    throw new SoloError('Error running init', error);
                });
            },
        };
    }
    close() {
        // no-op
        return Promise.resolve();
    }
};
InitCommand = InitCommand_1 = __decorate([
    injectable(),
    __metadata("design:paramtypes", [])
], InitCommand);
export { InitCommand };
//# sourceMappingURL=init.js.map