"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YamlManager = void 0;
const fs = require("node:fs");
const path = require("node:path");
const yaml = require("js-yaml");
const property_1 = require("./property");
const properties_1 = require("./properties");
const additional_api_1 = require("./additional-api");
const additional_apis_1 = require("./additional-apis");
const config_1 = require("../config");
class YamlManager {
    static loadYamlConfigFile(boundedContextName, moduleName) {
        var _a;
        const yamlPath = path.join(process.cwd(), 'cliter', boundedContextName.toKebabCase(), moduleName.toKebabCase() + config_1.cliterConfig.schemaDefinitionExtension);
        // read yaml file
        const yamlObj = yaml.load(fs.readFileSync(yamlPath, 'utf8'));
        YamlManager.parseModuleDefinitionSchema(yamlObj);
        const properties = new properties_1.Properties();
        properties.moduleName = yamlObj.moduleName;
        for (const property of yamlObj.aggregateProperties) {
            properties.add(new property_1.Property({
                name: property.name,
                type: property.type,
                primaryKey: property === null || property === void 0 ? void 0 : property.primaryKey,
                autoIncrement: property === null || property === void 0 ? void 0 : property.autoIncrement,
                enumOptions: (_a = property === null || property === void 0 ? void 0 : property.enumOptions) === null || _a === void 0 ? void 0 : _a.join(),
                decimals: property === null || property === void 0 ? void 0 : property.decimals,
                length: property === null || property === void 0 ? void 0 : property.length,
                minLength: property === null || property === void 0 ? void 0 : property.minLength,
                maxLength: property === null || property === void 0 ? void 0 : property.maxLength,
                nullable: property === null || property === void 0 ? void 0 : property.nullable,
                defaultValue: property === null || property === void 0 ? void 0 : property.defaultValue,
                relationship: property === null || property === void 0 ? void 0 : property.relationship,
                relationshipSingularName: property === null || property === void 0 ? void 0 : property.relationshipSingularName,
                relationshipAggregate: property === null || property === void 0 ? void 0 : property.relationshipAggregate,
                relationshipModulePath: property === null || property === void 0 ? void 0 : property.relationshipModulePath,
                relationshipKey: property === null || property === void 0 ? void 0 : property.relationshipKey,
                relationshipField: property === null || property === void 0 ? void 0 : property.relationshipField,
                relationshipAvoidConstraint: property === null || property === void 0 ? void 0 : property.relationshipAvoidConstraint,
                relationshipPackageName: property === null || property === void 0 ? void 0 : property.relationshipPackageName,
                pivotAggregateName: property === null || property === void 0 ? void 0 : property.pivotAggregateName,
                pivotPath: property === null || property === void 0 ? void 0 : property.pivotPath,
                pivotFileName: property === null || property === void 0 ? void 0 : property.pivotFileName,
                isDenormalized: property === null || property === void 0 ? void 0 : property.isDenormalized,
                index: property === null || property === void 0 ? void 0 : property.index,
                indexName: property === null || property === void 0 ? void 0 : property.indexName,
                isI18n: property === null || property === void 0 ? void 0 : property.isI18n,
                example: property === null || property === void 0 ? void 0 : property.example,
                faker: property === null || property === void 0 ? void 0 : property.faker,
            }));
        }
        const additionalApis = new additional_apis_1.AdditionalApis();
        if (Array.isArray(yamlObj.additionalApis)) {
            for (const additionalApi of yamlObj.additionalApis) {
                additionalApis.add(new additional_api_1.AdditionalApi({
                    path: additionalApi.path,
                    resolverType: additionalApi.resolverType,
                    httpMethod: additionalApi.httpMethod,
                }));
            }
        }
        return {
            boundedContextName: yamlObj.boundedContextName,
            moduleName: yamlObj.moduleName,
            moduleNames: yamlObj.moduleNames,
            aggregateName: yamlObj.aggregateName,
            hasOAuth: yamlObj.hasOAuth,
            hasTenant: yamlObj.hasTenant,
            hasAuditing: yamlObj.hasAuditing,
            properties,
            additionalApis,
            excluded: yamlObj.excluded,
        };
    }
    static parseModuleDefinitionSchema(yamlObj) {
        if (typeof yamlObj.boundedContextName !== 'string')
            throw new Error('Yaml file structure error, boundedContextName field missing');
        if (typeof yamlObj.moduleName !== 'string')
            throw new Error('Yaml file structure error, moduleName field missing');
        if (typeof yamlObj.moduleNames !== 'string')
            throw new Error('Yaml file structure error, moduleNames field missing');
        if (typeof yamlObj.aggregateName !== 'string')
            throw new Error('Yaml file structure error, aggregateName field missing');
        if (typeof yamlObj.hasOAuth !== 'boolean')
            throw new Error('Yaml file structure error, hasOAuth field missing');
        if (typeof yamlObj.hasTenant !== 'boolean')
            throw new Error('Yaml file structure error, hasTenant field missing');
        if (typeof yamlObj.hasAuditing !== 'boolean')
            throw new Error('Yaml file structure error, hasAuditing field missing in ' + yamlObj.aggregateName);
    }
}
exports.YamlManager = YamlManager;
