"use strict";
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 SchematicService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchematicService = exports.REFRESH_MODEL_COMMAND = exports.REMOVE_FIELDS_COMMAND = exports.ADD_MODULE_COMMAND = void 0;
const common_1 = require("@nestjs/common");
const command_service_1 = require("./command.service");
const solid_registry_1 = require("./solid-registry");
exports.ADD_MODULE_COMMAND = 'add-module';
exports.REMOVE_FIELDS_COMMAND = 'remove-fields';
exports.REFRESH_MODEL_COMMAND = 'refresh-model';
let SchematicService = SchematicService_1 = class SchematicService {
    constructor(commandService, solidRegistry) {
        this.commandService = commandService;
        this.solidRegistry = solidRegistry;
        this.logger = new common_1.Logger(SchematicService_1.name);
        this.SCHEMATIC_PROJECT = '@solidxai/code-builder';
        this.SCHEMATICS_COMMAND = 'schematics';
    }
    async executeSchematicCommand(command, options, debug = false) {
        const commandWithArgs = this.generateSchematicCommand(command, options, debug);
        return await this.commandService.executeCommandWithArgs(commandWithArgs);
    }
    generateSchematicCommand(command, options, debug) {
        const schematicName = `${this.SCHEMATIC_PROJECT}:${command}`;
        const baseArgs = [schematicName, ...(debug ? ['--debug=true'] : [])];
        if (command === exports.REMOVE_FIELDS_COMMAND ||
            command === exports.REFRESH_MODEL_COMMAND) {
            const { fieldNamesForRemoval, ...modelSpecificOptions } = options;
            const modelArgs = this.buildModelGenerationArgs(modelSpecificOptions);
            const fieldArgs = [];
            if (fieldNamesForRemoval && fieldNamesForRemoval.length > 0) {
                const _fieldArgs = this.buildFieldGenerationArgs(fieldNamesForRemoval);
                fieldArgs.push(..._fieldArgs);
            }
            const args = [...baseArgs, ...modelArgs, ...fieldArgs];
            this.logger.debug('schematicCommand args', args);
            return { command: this.SCHEMATICS_COMMAND, args };
        }
        else if (command === exports.ADD_MODULE_COMMAND) {
            const moduleOptions = options;
            const args = [...baseArgs, `--module=${moduleOptions.module}`];
            this.logger.debug('schematicCommand args', args);
            return { command: this.SCHEMATICS_COMMAND, args };
        }
        else {
            throw new Error('Schematic command not supported.');
        }
    }
    buildFieldGenerationArgs(fieldNamesForRemoval) {
        return fieldNamesForRemoval
            .filter((fieldName) => {
            return !this.solidRegistry.getCommonEntityKeys().map(key => key.toString()).includes(fieldName);
        })
            .map((fieldName) => {
            return `--field-names-for-removal=${fieldName}`;
        });
    }
    buildModelGenerationArgs(modelSpecificOptions) {
        const args = [
            `--module=${modelSpecificOptions.module}`,
            `--model=${modelSpecificOptions.model}`,
        ];
        return args;
    }
};
exports.SchematicService = SchematicService;
exports.SchematicService = SchematicService = SchematicService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [command_service_1.CommandService, solid_registry_1.SolidRegistry])
], SchematicService);
//# sourceMappingURL=schematic.service.js.map