"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const _cliter_1 = require("../@cliter");
const front_handler_1 = require("../@cliter/handlers/front.handler");
const common_1 = require("../@cliter/functions/common");
const back_1 = require("../@cliter/functions/back");
class Load extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(Load);
        if (args.secondArg === _cliter_1.ScopeElement.MODULE) {
            const flagName = (0, common_1.getBoundedContextModuleFromFlag)(this, flags.name);
            const { boundedContextName, moduleName } = await _cliter_1.Prompter.promptForLoadModule(flagName.boundedContextName, flagName.moduleName);
            // create yaml file
            const schema = _cliter_1.YamlManager.loadYamlConfigFile(boundedContextName, moduleName);
            // define state like a generate command
            const generateCommandState = {
                command: this,
                flags,
                lockFiles: (0, common_1.loadJsonLockFile)(boundedContextName, moduleName),
                schema,
            };
            if (args.firstArg === _cliter_1.Scope.BACK) {
                await _cliter_1.BackHandler.generateModule(generateCommandState, {
                    hasGenerateTestingFiles: flags.tests,
                });
                if (!flags.noGraphQLTypes)
                    await (0, back_1.generateGraphqlTypes)(generateCommandState);
            }
            if (args.firstArg === _cliter_1.Scope.FRONT) {
                await front_handler_1.FrontHandler.generateModule(generateCommandState);
            }
            await (0, common_1.reviewOverwrites)(generateCommandState);
        }
        /* if (args.element === TemplateElement.BACK_BOUNDED_CONTEXT)
        {
            const { boundedContextName }: any = await Prompter.promptForLoadBoundedContext(flags.boundedContext);

            const yamlFiles = fs.readdirSync(path.join(process.cwd(), 'cliter', boundedContextName.toKebabCase()));

            const batchOperations = [];
            for (const yamlFile of yamlFiles.filter(files => files.endsWith(cliterConfig.schemaDefinitionExtension)))
            {
                // create yaml file
                const schema: ModuleDefinitionSchema    = YamlManager.loadYamlConfigFile(boundedContextName, yamlFile.replace(cliterConfig.schemaDefinitionExtension, ''));
                const currentLockFiles: LockFile[]      = this.loadJsonLockFile(boundedContextName, schema.moduleName);

                // set stateService
                stateService.command   = this;
                stateService.schema    = schema;
                stateService.lockFiles = currentLockFiles;
                stateService.flags     = flags;

                // generate module files
                batchOperations.push(BackHandler.generateModule());
            }

            await Promise.all(batchOperations);

            // generate graphql files
            await this.reviewOverwrites(stateService);
        } */
    }
}
exports.default = Load;
Load.description = 'Load aurora [bounded-context, module] from yaml file, located in the cliter folder';
Load.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    force: core_1.Flags.boolean({
        char: 'f',
        description: 'Overwrite existing files.',
    }),
    name: core_1.Flags.string({
        char: 'n',
        required: true,
        description: 'Name of element to load.',
    }),
    noGraphQLTypes: core_1.Flags.boolean({
        char: 'g',
        description: 'Avoid generating graphql types.',
    }),
    overwriteInterface: core_1.Flags.boolean({
        char: 'w',
        description: 'Overwrite front interfaces.',
    }),
    tests: core_1.Flags.boolean({
        char: 't',
        description: 'Create test e2e files.',
    }),
    verbose: core_1.Flags.boolean({
        char: 'v',
        description: 'Reports on screen all the steps followed by the command.',
    }),
};
Load.args = {
    firstArg: core_1.Args.string({
        name: 'scope',
        required: true,
        description: 'Scope where our command will act.',
        options: [
            'back',
            'front',
        ],
    }),
    secondArg: core_1.Args.string({
        name: 'element',
        required: true,
        description: 'Type element to load.',
        options: [
            'bounded-context',
            'module',
        ],
    }),
};
Load.examples = [
    '$ aurora load back module -n=my-bounded-context/my-module',
    '$ aurora --help',
];
