"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransientContentGenericService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const helper_1 = require("../../helper");
const models_1 = require("../../models");
const services_1 = require("../../services");
const object_node_definition_service_1 = require("../../services/entity-definition/object-node-definition.service");
const inside_rest_service_1 = require("../../services/inside-rest/inside-rest.service");
const transient_uri_reference_service_1 = require("../../services/inside-rest/transient-uri-reference.service");
const object_node_service_1 = require("../../services/object-node/object-node.service");
const transient_entity_service_1 = require("../../services/transient-entity/transient-entity.service");
const uri_complete_service_1 = require("../../services/uri-complete/uri-complete.service");
const content_generic_template_const_1 = require("./content-generic-template.const");
let TransientContentGenericService = class TransientContentGenericService {
    constructor(transientEntityService, insideRestService, objectNodeService, objectNodeDefinitionService, uriCompleteService, transientUriReferenceService) {
        this.transientEntityService = transientEntityService;
        this.insideRestService = insideRestService;
        this.objectNodeService = objectNodeService;
        this.objectNodeDefinitionService = objectNodeDefinitionService;
        this.uriCompleteService = uriCompleteService;
        this.transientUriReferenceService = transientUriReferenceService;
        this.transientEntityService.registerTransientEntityTypeFunction(content_generic_template_const_1.CONTENT_GENERIC_PROVIDER, TransientContentGenericService.name, 'Add template configuration json schema definitions, configuration from referenced templates', models_1.EntityName.objectNode, content_generic_template_const_1.TEMPLATE_REFERER_WITH_CONFIGURATION_TYPE.name, this.completeTemplateRefererWithConfigurationNode.bind(this));
    }
    async completeTemplateRefererWithConfigurationNode(objectNode, ctx) {
        var _a;
        for (let key of Object.keys(objectNode)) {
            if (key + 'Id' in objectNode &&
                objectNode[key].entityCtx &&
                -1 <
                    lodash_1.indexOf(objectNode[key].entityCtx.implementedTypes, content_generic_template_const_1.TEMPLATE_VIEW_TYPE.name)) {
                if (key.endsWith('Tree')) {
                    key = key.substr(0, key.length - 4) + 'ObjectTree';
                }
                else if (key.endsWith('Node')) {
                    key = key.substr(0, key.length - 4) + 'ObjectNode';
                }
                else if (key.endsWith('Type')) {
                    key = key.substr(0, key.length - 4) + 'ObjectType';
                }
                await this.addTemplateConfiguration((_a = objectNode.entityCtx) === null || _a === void 0 ? void 0 : _a.jsonSchema, key + 'Id', 'model', ctx);
            }
        }
    }
    async addTemplateConfiguration(templateRefJsonSchema, templateKeyId, templateParentJsonPath, ctx) {
        var _a;
        if ((templateRefJsonSchema === null || templateRefJsonSchema === void 0 ? void 0 : templateRefJsonSchema.properties) &&
            templateRefJsonSchema.properties[templateKeyId] &&
            lodash_1.isArray(templateRefJsonSchema.properties[templateKeyId].oneOf) &&
            0 < templateRefJsonSchema.properties[templateKeyId].oneOf.length) {
            for (const templateEnum of templateRefJsonSchema.properties[templateKeyId]
                .oneOf) {
                const templateTreeId = templateEnum.enum[0];
                const template = (_a = ctx.nodeContext.references[templateTreeId]) === null || _a === void 0 ? void 0 : _a.value;
                if (template) {
                    if (!template.contentGenericTemplate) {
                        template.contentGenericTemplate = {};
                        await this.transientEntityService.completeReturnedEntity(models_1.EntityName.objectNode, template, services_1.CurrentContext.get(ctx, {
                            nodeContext: {
                                node: ctx.nodeContext.references[templateTreeId],
                            },
                        }));
                    }
                    await this.addTemplateConfigurationProperties(template, templateParentJsonPath, 'model.' + templateKeyId, templateTreeId, templateRefJsonSchema, ctx);
                }
            }
        }
    }
    async addTemplateConfigurationProperties(templateView, modelPath, expressionPath, templateObjectTreeId, jsonSchema, ctx) {
        var _a, _b, _c, _d, _e;
        if (jsonSchema && ((_b = (_a = templateView === null || templateView === void 0 ? void 0 : templateView.contentGenericTemplate) === null || _a === void 0 ? void 0 : _a.refererConfig) === null || _b === void 0 ? void 0 : _b.properties) &&
            0 <
                Object.keys((_d = (_c = templateView === null || templateView === void 0 ? void 0 : templateView.contentGenericTemplate) === null || _c === void 0 ? void 0 : _c.refererConfig) === null || _d === void 0 ? void 0 : _d.properties).length) {
            if (!jsonSchema.properties.templatesConfigurations) {
                jsonSchema.properties.templatesConfigurations = {
                    type: 'object',
                    title: 'Object template configurattion',
                    'x-schema-form': { labelHtmlClass: 'd-none' },
                    properties: {},
                };
            }
            if ((_e = jsonSchema === null || jsonSchema === void 0 ? void 0 : jsonSchema.properties) === null || _e === void 0 ? void 0 : _e.templatesConfigurations.properties) {
                jsonSchema.properties.templatesConfigurations.properties[templateView.name] = lodash_1.merge({
                    type: 'object',
                    'x-schema-form': { labelHtmlClass: 'd-none' },
                }, templateView.contentGenericTemplate.refererConfig);
                helper_1.addCondition(expressionPath + "=='" + templateObjectTreeId + "'", jsonSchema.properties.templatesConfigurations.properties[templateView.name], modelPath);
                if (jsonSchema.properties.templatesConfigurations.properties[templateView.name].properties) {
                    await this.objectNodeDefinitionService.completeProperties(jsonSchema.properties.templatesConfigurations.properties[templateView.name].properties, ctx.nodeContext);
                }
            }
        }
    }
};
TransientContentGenericService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(transient_entity_service_1.TransientEntityService)),
    tslib_1.__param(1, core_1.service(inside_rest_service_1.InsideRestService)),
    tslib_1.__param(2, core_1.service(object_node_service_1.ObjectNodeService)),
    tslib_1.__param(3, core_1.service(object_node_definition_service_1.ObjectNodeDefinitionService)),
    tslib_1.__param(4, core_1.service(uri_complete_service_1.UriCompleteService)),
    tslib_1.__param(5, core_1.service(transient_uri_reference_service_1.TransientUriReferenceService)),
    tslib_1.__metadata("design:paramtypes", [transient_entity_service_1.TransientEntityService,
        inside_rest_service_1.InsideRestService,
        object_node_service_1.ObjectNodeService,
        object_node_definition_service_1.ObjectNodeDefinitionService,
        uri_complete_service_1.UriCompleteService,
        transient_uri_reference_service_1.TransientUriReferenceService])
], TransientContentGenericService);
exports.TransientContentGenericService = TransientContentGenericService;
//# sourceMappingURL=transient-content-generic.service.js.map