"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addCondition = exports.addConditionOldVersion = exports.json = exports.jsonschema = exports.css = exports.scss = exports.script = exports.template = exports.base64 = exports.image = exports.contentGenericTemplate = exports.toStartCase = exports.toCamelCase = exports.toKebabCase = void 0;
const tslib_1 = require("tslib");
/* eslint-disable no-empty */
const fs_1 = tslib_1.__importDefault(require("fs"));
const lodash_1 = require("lodash");
const path_1 = tslib_1.__importDefault(require("path"));
function toKebabCase(str) {
    return lodash_1.kebabCase(str);
}
exports.toKebabCase = toKebabCase;
function toCamelCase(str) {
    return lodash_1.camelCase(str);
}
exports.toCamelCase = toCamelCase;
function toStartCase(str) {
    return lodash_1.capitalize(lodash_1.startCase(str));
}
exports.toStartCase = toStartCase;
function contentGenericTemplate(dirName, name, templateIds = []) {
    const genericTemplate = {
        templateMustache: '',
        templatesMustache: {},
        templateAngular: '',
        headerScript: '',
        footerScript: '',
        scss: '',
        css: '',
        controller: `function newFunction() {
      return {
        async init(component) {},
      };
    }
    newFunction();
    `,
        refererConfig: {
            properties: {},
        },
    };
    try {
        genericTemplate.templateMustache = template(path_1.default.join(dirName, name), 'mustache.template');
    }
    catch (error) { }
    try {
        genericTemplate.headerScript = script(path_1.default.join(dirName, name), 'header');
    }
    catch (error) { }
    try {
        genericTemplate.footerScript = script(path_1.default.join(dirName, name), 'footer');
    }
    catch (error) { }
    for (const templateId of templateIds) {
        genericTemplate.templatesMustache[templateId] = template(path_1.default.join(dirName, name), templateId + '.mustache.template');
    }
    try {
        genericTemplate.templateAngular = template(path_1.default.join(dirName, name), 'angular.template');
    }
    catch (error) { }
    try {
        genericTemplate.scss = scss(path_1.default.join(dirName, name), 'style');
    }
    catch (error) { }
    try {
        genericTemplate.css = css(path_1.default.join(dirName, name), 'style');
    }
    catch (error) { }
    try {
        genericTemplate.controller = script(path_1.default.join(dirName, name), 'controller');
    }
    catch (error) { }
    /*
    try {
      genericTemplate.config = jsonschema(path.join(dirName, name), 'config');
    } catch (error) {}
    */
    try {
        genericTemplate.refererConfig = jsonschema(path_1.default.join(dirName, name), 'refererConfig');
    }
    catch (error) { }
    return genericTemplate;
}
exports.contentGenericTemplate = contentGenericTemplate;
function image(dirName, filename) {
    return base64(dirName, 'images', filename);
}
exports.image = image;
function base64(dirName, name, filename) {
    return fs_1.default.readFileSync(path_1.default.join(dirName, name + '/' + filename + '.base64'), 'utf8');
}
exports.base64 = base64;
function template(dirName, name) {
    return fs_1.default.readFileSync(path_1.default.join(dirName, name + '.html'), 'utf8');
}
exports.template = template;
function script(dirName, name) {
    return fs_1.default.readFileSync(path_1.default.join(dirName, name + '.js'), 'utf8');
}
exports.script = script;
function scss(dirName, name) {
    return fs_1.default.readFileSync(path_1.default.join(dirName, name + '.scss'), 'utf8');
}
exports.scss = scss;
function css(dirName, name) {
    return fs_1.default.readFileSync(path_1.default.join(dirName, name + '.css'), 'utf8');
}
exports.css = css;
function jsonschema(dirName, name) {
    return json(dirName, name + '.schema');
}
exports.jsonschema = jsonschema;
function json(dirName, name) {
    return JSON.parse(fs_1.default.readFileSync(path_1.default.join(dirName, name + '.json'), 'utf8'));
}
exports.json = json;
function addConditionOldVersion(condition, schema) {
    if (!schema['x-schema-form']) {
        schema['x-schema-form'] = {};
    }
    schema['x-schema-form'].condition = condition;
    if (schema.properties) {
        for (const key of Object.keys(schema.properties)) {
            addCondition(condition, schema.properties[key]);
        }
    }
}
exports.addConditionOldVersion = addConditionOldVersion;
function addCondition(condition, schema, modelPath = 'model') {
    condition = condition.replace('[arrayIndex]', '[arrayIndices]');
    let expresion = 'model = ' + modelPath.replace('[arrayIndex]', '[arrayIndices]') + ';\r\n';
    expresion += 'var result = ' + condition + ';\r\n';
    //  function anonymous(model,arrayIndices
    if (!schema['x-schema-form']) {
        schema['x-schema-form'] = {};
    }
    if (lodash_1.isString(schema['x-schema-form'].condition)) {
        schema['x-schema-form'].condition = {
            functionBody: 'return ' +
                schema['x-schema-form'].condition.replace('[arrayIndex]', '[arrayIndices]'),
        };
    }
    if (!lodash_1.isObject(schema['x-schema-form'].condition)) {
        schema['x-schema-form'].condition = {};
    }
    if (lodash_1.isString(schema['x-schema-form'].condition.functionBody)) {
        expresion +=
            'var embeddedCondition = true;\r\n' +
                'var previousResultFct = (model,arrayIndices) =>{' +
                '\r\n' +
                schema['x-schema-form'].condition.functionBody +
                '\r\n' +
                '}' +
                ';\r\n' +
                'result = result && previousResultFct(model,arrayIndices);\r\n';
    }
    schema['x-schema-form'].condition.functionBody =
        expresion + '\r\n' + 'return result';
    if (schema.properties) {
        for (const key of Object.keys(schema.properties)) {
            addCondition(condition, schema.properties[key], modelPath);
        }
    }
}
exports.addCondition = addCondition;
//# sourceMappingURL=utils.js.map