"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 models_1 = require("../../models");
const object_node_definition_service_1 = require("../../services/entity-definition/object-node-definition.service");
const node_intercept_service_1 = require("../../services/entity-intercept/node-intercept.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 application_service_1 = require("./../../services/application.service");
const object_type_service_1 = require("./../../services/object-type.service");
const content_generic_template_const_1 = require("./content-generic-template.const");
let TransientContentGenericService = class TransientContentGenericService {
    constructor(transientEntityService, insideRestService, objectNodeService, objectNodeDefinitionService, objectTypeService, uriCompleteService, transientUriReferenceService, nodeInterceptService) {
        /*
        this.transientEntityService.registerTransientEntityTypeFunction(
          CONTENT_GENERIC_PROVIDER,
          TransientContentGenericService.name,
          'Add template configuration json schema definitions, configuration from referenced templates',
          EntityName.objectNode,
          TEMPLATE_REFERER_WITH_CONFIGURATION_TYPE.name,
          this.completeTemplateRefererWithConfigurationNode.bind(this),
        );*/
        this.transientEntityService = transientEntityService;
        this.insideRestService = insideRestService;
        this.objectNodeService = objectNodeService;
        this.objectNodeDefinitionService = objectNodeDefinitionService;
        this.objectTypeService = objectTypeService;
        this.uriCompleteService = uriCompleteService;
        this.transientUriReferenceService = transientUriReferenceService;
        this.nodeInterceptService = nodeInterceptService;
        this.nodeInterceptService.registerEntityInterceptorService(content_generic_template_const_1.CONTENT_GENERIC_PROVIDER, TransientContentGenericService.name, 'Set templateObjectTreeId to parent template', content_generic_template_const_1.TEMPLATE_EMBEDDED_CONFIGURATION_TYPE.name, application_service_1.EntityActionType.create, this.interceptTemplateEmbeddedConfigurationCreate.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(content_generic_template_const_1.CONTENT_GENERIC_PROVIDER, TransientContentGenericService.name, 'Add template configuration json schema definitions to Template Configuration node', models_1.EntityName.objectNode, content_generic_template_const_1.TEMPLATE_CONFIGURATION_TYPE.name, this.completeTemplateConfigurationNode.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(content_generic_template_const_1.CONTENT_GENERIC_PROVIDER, TransientContentGenericService.name, 'Add template configuration reference to temaplete referer', models_1.EntityName.objectNode, content_generic_template_const_1.TEMPLATE_REFERRER_TYPE.name, this.completeTemplateRefererNode.bind(this));
    }
    async interceptTemplateEmbeddedConfigurationCreate(entityId, entity, requestEntity, ctx) {
        const parentTree = await this.insideRestService.read(this.uriCompleteService.getUri(models_1.EntityName.objectTree, entity.parentNodeId, ctx), ctx);
        requestEntity.templateObjectTreeId = parentTree.aliasId;
        return true;
    }
    async completeTemplateConfigurationNode(objectNode, ctx) {
        var _a, _b, _c, _d;
        if (objectNode.templateTreeId) {
            let templateNode = await this.insideRestService.read(this.uriCompleteService.getUri(models_1.EntityName.objectNode, objectNode.templateTreeId, ctx), ctx);
            if (templateNode.contentGenericTemplate &&
                templateNode.contentGenericTemplate.refererConfig &&
                templateNode.contentGenericTemplate.refererConfig.properties) {
                lodash_1.merge((_b = (_a = objectNode.entityCtx) === null || _a === void 0 ? void 0 : _a.jsonSchema) === null || _b === void 0 ? void 0 : _b.properties.configuration.properties, templateNode.contentGenericTemplate.refererConfig.properties);
                await this.objectNodeDefinitionService.completeProperties((_d = (_c = objectNode.entityCtx) === null || _c === void 0 ? void 0 : _c.jsonSchema) === null || _d === void 0 ? void 0 : _d.properties.configuration.properties, ctx.nodeContext);
            }
        }
    }
    async completeTemplateRefererNode(objectNode, ctx) {
        var _a, _b;
        if ((_b = (_a = objectNode.entityCtx) === null || _a === void 0 ? void 0 : _a.jsonSchema) === null || _b === void 0 ? void 0 : _b.properties) {
            await this.addConfigurationRefernceToProperties(objectNode.entityCtx.jsonSchema.properties, ctx);
        }
        await this.addTemplatesConfigurations(objectNode, ctx);
    }
    async addTemplatesConfigurations(objectNode, ctx) {
        const templateTypes = await this.objectTypeService.getImplementingTypes(content_generic_template_const_1.TEMPLATE_VIEW_TYPE.name);
        for (const key of Object.keys(objectNode)) {
            if (lodash_1.isArray(objectNode[key])) {
                for (const subObjectNode of objectNode[key]) {
                    if (lodash_1.isObject(subObjectNode)) {
                        await this.addTemplatesConfigurations(subObjectNode, ctx);
                    }
                }
            }
            else if (lodash_1.isObject(objectNode[key])) {
                for (const subObjectNode of Object.values(objectNode[key])) {
                    if (lodash_1.isObject(subObjectNode)) {
                        await this.addTemplatesConfigurations(subObjectNode, ctx);
                    }
                }
            }
            else if (lodash_1.isString(objectNode[key]) && key.endsWith('ObjectTreeId')) {
                const refParts = objectNode[key].split('/');
                const refType = refParts[refParts.length - 2];
                if (templateTypes.includes(refType)) {
                    const refTemplate = key.replace('ObjectTreeId', 'Tree');
                    const refConfiguration = key.replace('ObjectTreeId', 'ConfigurationTree');
                    const refConfigurationId = key.replace('ObjectTreeId', 'ConfigurationObjectTreeId');
                    if (objectNode[refConfigurationId]) {
                        if (!objectNode[refTemplate]) {
                            objectNode[refTemplate] = await this.insideRestService.read(this.uriCompleteService.getUri(models_1.EntityName.objectTree, objectNode[key], ctx), ctx);
                        }
                        if (!objectNode[refConfiguration]) {
                            objectNode[refConfiguration] = await this.insideRestService.read(this.uriCompleteService.getUri(models_1.EntityName.objectTree, objectNode[refConfigurationId], ctx), ctx);
                        }
                        if (objectNode[refTemplate] && objectNode[refConfiguration]) {
                            if (!objectNode.templatesConfigurations) {
                                objectNode.templatesConfigurations = {};
                            }
                            const config = await this.insideRestService.read(objectNode[refConfiguration].treeNode.uri, ctx);
                            objectNode.templatesConfigurations[objectNode[refTemplate].treeNode.name] = config.configuration;
                        }
                    }
                }
            }
        }
    }
    async addConfigurationRefernceToProperties(properties, ctx) {
        const templateTypes = await this.objectTypeService.getImplementingTypes(content_generic_template_const_1.TEMPLATE_VIEW_TYPE.name);
        for (const key of Object.keys(properties)) {
            if ('object' === properties[key].type && properties[key].properties) {
                await this.addConfigurationRefernceToProperties(properties[key].properties, ctx);
            }
            else if ('array' === properties[key].type && properties[key].items) {
                await this.addConfigurationRefernceToProperties(properties[key], ctx);
            }
            else if ('string' === properties[key].type &&
                properties[key].oneOf &&
                0 < properties[key].oneOf.length &&
                key.endsWith('ObjectTreeId')) {
                const refParts = properties[key].oneOf[0].enum[0].split('/');
                const refType = refParts[refParts.length - 2];
                if (templateTypes.includes(refType)) {
                    const configurationType = refType + 'Configuration';
                    const configurationKey = key.replace('ObjectTreeId', 'ConfigurationObjectTreeId');
                    if (await this.objectTypeService.searchById(configurationType)) {
                        properties[configurationKey] = {
                            type: 'string',
                            title: properties[key].title + ' Configuration',
                            default: '',
                            oneOfTree: [
                                {
                                    treeType: configurationType,
                                    namespaceName: 'templates',
                                    namespaceType: 'RepositoryCategory',
                                    ownerName: 'public',
                                    ownerType: 'Repository',
                                },
                                {
                                    treeType: configurationType,
                                },
                            ],
                        };
                        await this.objectNodeDefinitionService.completeProperties(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(object_type_service_1.ObjectTypeService)),
    tslib_1.__param(5, core_1.service(uri_complete_service_1.UriCompleteService)),
    tslib_1.__param(6, core_1.service(transient_uri_reference_service_1.TransientUriReferenceService)),
    tslib_1.__param(7, core_1.service(node_intercept_service_1.NodeInterceptService)),
    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,
        object_type_service_1.ObjectTypeService,
        uri_complete_service_1.UriCompleteService,
        transient_uri_reference_service_1.TransientUriReferenceService,
        node_intercept_service_1.NodeInterceptService])
], TransientContentGenericService);
exports.TransientContentGenericService = TransientContentGenericService;
//# sourceMappingURL=transient-content-generic.service.js.map