"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransientWebSiteService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const models_1 = require("../../models");
const services_1 = require("../../services");
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 object_tree_service_1 = require("./../../services/object-tree/object-tree.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 transient_content_generic_service_1 = require("./../content-generic-template/transient-content-generic.service");
const web_site_const_1 = require("./web-site.const");
let TransientWebSiteService = class TransientWebSiteService {
    constructor(transientEntityService, insideRestService, objectNodeService, objectTypeService, uriCompleteService, transientUriReferenceService, transientContentGenericService, objectTreeService) {
        this.transientEntityService = transientEntityService;
        this.insideRestService = insideRestService;
        this.objectNodeService = objectNodeService;
        this.objectTypeService = objectTypeService;
        this.uriCompleteService = uriCompleteService;
        this.transientUriReferenceService = transientUriReferenceService;
        this.transientContentGenericService = transientContentGenericService;
        this.objectTreeService = objectTreeService;
        this.transientEntityService.registerTransientEntityTypeFunction(web_site_const_1.WEB_SITE_PROVIDER, TransientWebSiteService.name, 'Add welcomePage Id and Uri', models_1.EntityName.objectTree, web_site_const_1.WEB_SITE_VIEW_TYPE.name, this.completeWebSiteView.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(web_site_const_1.WEB_SITE_PROVIDER, TransientWebSiteService.name, 'Complete menuEntries json schema definition with menuEntries from referenced template', models_1.EntityName.objectNode, web_site_const_1.WEB_SITE_MENU_ENTRIES_TYPE.name, this.completeWebSiteMenuEntriesNode.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(web_site_const_1.WEB_SITE_PROVIDER, TransientWebSiteService.name, 'Add menuEntriesList, tree of site menu entries', models_1.EntityName.objectNode, web_site_const_1.WEB_SITE_MENU_ENTRIES_TYPE.name, this.completeWebSiteMenuEntriesNode2.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(web_site_const_1.WEB_SITE_PROVIDER, TransientWebSiteService.name, 'Complete pageTemplateChoice json schema definition with pageTemplateChoice from referenced template and add its conditional page template configuration', models_1.EntityName.objectNode, web_site_const_1.PAGE_WITH_TEMPLATE_CHOICE.name, this.completePageWithTemplateChoiceNode.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(web_site_const_1.WEB_SITE_PROVIDER, TransientWebSiteService.name, 'Complete paragraphsTemplateChoice json schema definition with paragraphsTemplateChoice from referenced template and add its conditional paragraph template configuration', models_1.EntityName.objectNode, web_site_const_1.PARAGRAPH_WITH_TEMPLATE_CHOICE_TYPE.name, this.completeParagraphWithTemplateChoiceNode.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(web_site_const_1.WEB_SITE_PROVIDER, TransientWebSiteService.name, 'Add new page template configuration json schema definitions, configuration from new added referenced templates', models_1.EntityName.objectNode, web_site_const_1.WEB_SITE_WITH_PAGES_TEMPLATE_TYPE.name, this.completeWebSiteWithPagesTemplateNode.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(web_site_const_1.WEB_SITE_PROVIDER, TransientWebSiteService.name, 'Add new paragraph template configuration json schema definitions, configuration from new added referenced templates', models_1.EntityName.objectNode, web_site_const_1.WEB_SITE_WITH_PARAGRAPHS_TEMPLATE_TYPE.name, this.completeWebSiteWithParagraphsTemplateNode.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(web_site_const_1.WEB_SITE_PROVIDER, TransientWebSiteService.name, 'Add pageTrees field, list of sub-pages', models_1.EntityName.objectTree, web_site_const_1.PAGE_WITH_SUB_PAGE_TYPE.name, this.completePageWithSubPageTree.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(web_site_const_1.WEB_SITE_PROVIDER, TransientWebSiteService.name, 'Add paragraphTrees field, list of paragraphs', models_1.EntityName.objectTree, web_site_const_1.PAGE_WITH_PARAGRAPH_TYPE.name, this.completePageWithParagraphTree.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(web_site_const_1.WEB_SITE_PROVIDER, TransientWebSiteService.name, 'Add paragraphTrees field, list of paragraphs', models_1.EntityName.objectTree, web_site_const_1.PARAGRAPH_CONTAINER_TYPE.name, this.completeParagraphContainerTree.bind(this));
    }
    addTemplatesConfigurationsReferences(objectNode) {
        if (objectNode.templatesConfigurations) {
            if (!objectNode.templatesConfigurations.id) {
                objectNode.templatesConfigurations.id =
                    objectNode.id + '/templatesConfigurations';
            }
            if (!objectNode.templatesConfigurations.entityCtx) {
                objectNode.templatesConfigurations.entityCtx = {
                    entityType: models_1.EntityName.objectNode,
                };
            }
            for (const configurationName of Object.keys(objectNode.templatesConfigurations)) {
                if (lodash_1.isObject(objectNode.templatesConfigurations[configurationName])) {
                    if (!objectNode.templatesConfigurations[configurationName].id) {
                        objectNode.templatesConfigurations[configurationName].id =
                            objectNode.id + '/templatesConfigurations/' + configurationName;
                    }
                    if (!objectNode.templatesConfigurations[configurationName].entityCtx) {
                        objectNode.templatesConfigurations[configurationName].entityCtx = {
                            entityType: models_1.EntityName.objectNode,
                        };
                    }
                }
            }
        }
    }
    addTemplatesConfigurations(objectNode, configurationName, configurationValue) {
        if (!objectNode.templatesConfigurations) {
            objectNode.templatesConfigurations = {};
        }
        objectNode.templatesConfigurations[configurationName] = configurationValue;
        this.addTemplatesConfigurationsReferences(objectNode);
    }
    async completeWebSiteView(webSiteViewTree, ctx) {
        const welcomePages = (await this.objectTreeService.getChildrenByImplementedTypeId(webSiteViewTree))[web_site_const_1.WELCOME_PAGE_TYPE.name];
        if (welcomePages && 0 < welcomePages.length) {
            webSiteViewTree.welcomePageId = welcomePages[0].id;
            webSiteViewTree.welcomePageUri = welcomePages[0].uri;
        }
    }
    async completeWebSiteMenuEntriesNode2(webSiteMenuEntriesNode, ctx) {
        webSiteMenuEntriesNode.menuEntriesList = {
            entityCtx: webSiteMenuEntriesNode.entityCtx,
            uri: webSiteMenuEntriesNode.uri + '/menuEntries',
            id: webSiteMenuEntriesNode.id + '/menuEntries',
        };
        try {
            const webSiteTree = (await this.insideRestService.read(webSiteMenuEntriesNode.webSiteObjectTreeUri, ctx));
            const webSiteMenuEntriesTree = (await this.insideRestService.read(this.uriCompleteService.getUri(models_1.EntityName.objectTree, webSiteMenuEntriesNode.id, ctx), ctx));
            if (webSiteMenuEntriesNode.menuEntries) {
                for (const menuEntry of webSiteTree.treeNode.menuEntries) {
                    if (webSiteMenuEntriesNode.menuEntries[menuEntry.entryKey]) {
                        const children = await this.lookForMenuEntries([webSiteMenuEntriesTree], menuEntry.entryTypes, webSiteMenuEntriesTree, menuEntry.entryKey, menuEntry.menuEntryLabelKey
                            ? menuEntry.menuEntryLabelKey
                            : 'name', !!menuEntry.adminEntry);
                        webSiteMenuEntriesNode.menuEntriesList[menuEntry.entryKey] = {
                            entityCtx: webSiteMenuEntriesTree.entityCtx,
                            treeNode: {
                                menuTitle: webSiteMenuEntriesNode.menuTitle,
                                name: webSiteMenuEntriesNode.name,
                            },
                            id: webSiteMenuEntriesTree.id +
                                '/menuEntries/' +
                                menuEntry.entryKey,
                            uri: webSiteMenuEntriesTree.uri +
                                '/menuEntries/' +
                                menuEntry.entryKey,
                            aliasUri: undefined,
                            children: children,
                            disabled: 0 === children.length,
                            singleMenu: 0 === children.length ||
                                (1 === children.length && children[0].singleMenu),
                            adminEntry: !!menuEntry.adminEntry,
                        };
                        if (webSiteMenuEntriesNode.menuEntriesList[menuEntry.entryKey]
                            .singleMenu &&
                            !webSiteMenuEntriesNode.menuEntriesList[menuEntry.entryKey]
                                .disabled) {
                            webSiteMenuEntriesNode.menuEntriesList[menuEntry.entryKey].pageTreeId = children[0].pageTreeId;
                            webSiteMenuEntriesNode.menuEntriesList[menuEntry.entryKey].pageTreeUri = children[0].pageTreeUri;
                        }
                    }
                }
            }
        }
        catch (error) { }
    }
    async lookForMenuEntries(trees, entryTypes, webSiteMenuEntriesTree, entryKey, menuEntryLabelKey, adminEntry) {
        const parentMenuTrees = [];
        for (const tree of trees) {
            if (!!tree.treeNode[menuEntryLabelKey] &&
                lodash_1.intersection(await this.objectTypeService.getImplementedTypes(tree.treeNode.objectTypeId), entryTypes).length > 0) {
                const children = await this.lookForMenuEntries(tree.children, entryTypes, webSiteMenuEntriesTree, entryKey, menuEntryLabelKey, adminEntry);
                parentMenuTrees.push({
                    entityCtx: tree.entityCtx,
                    pageTreeId: tree.id,
                    pageTreeUri: tree.uri,
                    id: webSiteMenuEntriesTree.id +
                        '/menuEntries/' +
                        entryKey +
                        '/' +
                        tree.id,
                    uri: webSiteMenuEntriesTree.uri +
                        '/menuEntries/' +
                        entryKey +
                        '/' +
                        tree.id,
                    treeNode: tree.treeNode,
                    children: children,
                    singleMenu: 0 === children.length,
                    adminEntry: adminEntry,
                    menuTitle: tree.treeNode[menuEntryLabelKey],
                });
            }
            else {
                parentMenuTrees.push(...(await this.lookForMenuEntries(tree.children, entryTypes, webSiteMenuEntriesTree, entryKey, menuEntryLabelKey, adminEntry)));
            }
        }
        return parentMenuTrees;
    }
    async completeWebSiteMenuEntriesNode(objectNode, ctx) {
        var _a, _b, _c, _d, _e;
        const menuEntriesProperty = (_c = (_b = (_a = objectNode.entityCtx) === null || _a === void 0 ? void 0 : _a.jsonSchema) === null || _b === void 0 ? void 0 : _b.properties) === null || _c === void 0 ? void 0 : _c.menuEntries;
        const templateMenuEntries = (_e = (_d = objectNode.webSiteTree) === null || _d === void 0 ? void 0 : _d.treeNode) === null || _e === void 0 ? void 0 : _e.menuEntries;
        if (menuEntriesProperty && templateMenuEntries) {
            for (const menuEntry of templateMenuEntries) {
                menuEntriesProperty.properties[menuEntry.entryKey] = {
                    type: 'string',
                    title: menuEntry.entryName,
                };
            }
        }
    }
    async completePageWithTemplateChoiceNode(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.pageTemplateChoice) {
            const treeNode = await this.objectNodeService.getTreeNode(objectNode, ctx);
            if (treeNode.webSiteObjectTreeId) {
                const templatePath = treeNode.webSiteObjectTreeId.split('/');
                const templateTree = await this.objectNodeService.searchTree(templatePath[1], templatePath[2], templatePath[3], templatePath[4], templatePath[5], templatePath[6]);
                if (templateTree === null || templateTree === void 0 ? void 0 : templateTree.pageTemplateChoices) {
                    const oneOf = [];
                    for (const pageTemplateChoice of templateTree.pageTemplateChoices) {
                        if (lodash_1.intersection(await this.objectTypeService.getImplementedTypes(objectNode.objectTypeId), pageTemplateChoice.pageTypes).length > 0) {
                            oneOf.push({
                                enum: [pageTemplateChoice.pageTypeKey],
                                title: pageTemplateChoice.pageTypeName,
                            });
                        }
                    }
                    if (0 < oneOf.length) {
                        objectNode.entityCtx.jsonSchema.properties.pageTemplateChoice.oneOf = oneOf;
                        if (objectNode.pageTemplateChoice) {
                            const pageTemplateChoice = lodash_1.find(templateTree.pageTemplateChoices, (choice) => objectNode.pageTemplateChoice === choice.pageTypeKey);
                            if (pageTemplateChoice === null || pageTemplateChoice === void 0 ? void 0 : pageTemplateChoice.pageTemplateObjectTreeId) {
                                objectNode.pageTemplateObjectTreeId =
                                    pageTemplateChoice.pageTemplateObjectTreeId;
                                const pageTemplateObjectTreeIdParts = objectNode.pageTemplateObjectTreeId.split('/');
                                const pageTemplateObjectTreeName = pageTemplateObjectTreeIdParts[pageTemplateObjectTreeIdParts.length - 1];
                                if (pageTemplateChoice.templatesConfigurations &&
                                    pageTemplateObjectTreeName in
                                        pageTemplateChoice.templatesConfigurations) {
                                    this.addTemplatesConfigurations(objectNode, pageTemplateObjectTreeName, pageTemplateChoice.templatesConfigurations[pageTemplateObjectTreeName]);
                                }
                            }
                            this.uriCompleteService.addUri(objectNode, ctx);
                            await this.transientUriReferenceService.completeReturnedEntity(objectNode, ctx);
                        }
                    }
                }
            }
        }
    }
    async completeParagraphWithTemplateChoiceNode(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.paragraphTemplateChoice) {
            const treeNode = await this.objectNodeService.getTreeNode(objectNode, ctx);
            if (treeNode.webSiteObjectTreeId) {
                const templatePath = treeNode.webSiteObjectTreeId.split('/');
                const templateTree = await this.objectNodeService.searchTree(templatePath[1], templatePath[2], templatePath[3], templatePath[4], templatePath[5], templatePath[6]);
                if (templateTree === null || templateTree === void 0 ? void 0 : templateTree.paragraphTemplateChoices) {
                    const oneOf = [];
                    for (const paragraphTemplateChoice of templateTree.paragraphTemplateChoices) {
                        if (
                        /*
                        doesTreeImplementOneOfType(
                          objectNode,
                          paragraphTemplateChoice.paragraphTypes,
                        )*/
                        -1 <
                            lodash_1.indexOf(paragraphTemplateChoice.paragraphTypes, objectNode.objectTypeId)) {
                            oneOf.push({
                                enum: [paragraphTemplateChoice.paragraphTypeKey],
                                title: paragraphTemplateChoice.paragraphTypeName,
                            });
                        }
                    }
                    if (0 < oneOf.length) {
                        objectNode.entityCtx.jsonSchema.properties.paragraphTemplateChoice.oneOf = oneOf;
                        if (!objectNode.paragraphTemplateChoice) {
                            objectNode.paragraphTemplateChoice = oneOf[0].enum[0];
                        }
                        if (objectNode.paragraphTemplateChoice) {
                            const paragraphTemplateChoice = lodash_1.find(templateTree.paragraphTemplateChoices, (choice) => objectNode.paragraphTemplateChoice ===
                                choice.paragraphTypeKey);
                            if (paragraphTemplateChoice === null || paragraphTemplateChoice === void 0 ? void 0 : paragraphTemplateChoice.paragraphTemplateObjectTreeId) {
                                objectNode.paragraphTemplateObjectTreeId =
                                    paragraphTemplateChoice.paragraphTemplateObjectTreeId;
                                const paragraphTemplateObjectTreeIdParts = objectNode.paragraphTemplateObjectTreeId.split('/');
                                const paragraphTemplateObjectTreeName = paragraphTemplateObjectTreeIdParts[paragraphTemplateObjectTreeIdParts.length - 1];
                                if (paragraphTemplateChoice.templatesConfigurations &&
                                    paragraphTemplateObjectTreeName in
                                        paragraphTemplateChoice.templatesConfigurations) {
                                    this.addTemplatesConfigurations(objectNode, paragraphTemplateObjectTreeName, paragraphTemplateChoice.templatesConfigurations[paragraphTemplateObjectTreeName]);
                                }
                            }
                            this.uriCompleteService.addUri(objectNode, ctx);
                            await this.transientUriReferenceService.completeReturnedEntity(objectNode, ctx);
                        }
                    }
                    else {
                        delete objectNode.entityCtx.jsonSchema.properties
                            .paragraphTemplateChoice;
                    }
                }
            }
        }
    }
    async completeWebSiteWithPagesTemplateNode(objectNode, ctx) {
        var _a, _b, _c, _d;
        await this.transientContentGenericService.addTemplateConfiguration((_d = (_c = (_b = (_a = objectNode.entityCtx) === null || _a === void 0 ? void 0 : _a.jsonSchema) === null || _b === void 0 ? void 0 : _b.properties) === null || _c === void 0 ? void 0 : _c.pageTemplateChoices) === null || _d === void 0 ? void 0 : _d.items, 'pageTemplateObjectTreeId', 'model.pageTemplateChoices[arrayIndex]', ctx);
        this.addTemplatesConfigurationsReferences(objectNode);
    }
    async completeWebSiteWithParagraphsTemplateNode(objectNode, ctx) {
        var _a, _b, _c, _d;
        await this.transientContentGenericService.addTemplateConfiguration((_d = (_c = (_b = (_a = objectNode.entityCtx) === null || _a === void 0 ? void 0 : _a.jsonSchema) === null || _b === void 0 ? void 0 : _b.properties) === null || _c === void 0 ? void 0 : _c.paragraphTemplateChoices) === null || _d === void 0 ? void 0 : _d.items, 'paragraphTemplateObjectTreeId', 'model.paragraphTemplateChoices[arrayIndex]', ctx);
        this.addTemplatesConfigurationsReferences(objectNode);
    }
    async completePageWithSubPageTree(entity, ctx) {
        const pageTrees = (await this.objectTreeService.getChildrenByImplementedTypeId(entity))[web_site_const_1.PAGE_TYPE.name];
        entity.pageTrees = pageTrees ? pageTrees : [];
    }
    async completePageWithParagraphTree(entity, ctx) {
        const paragraphTrees = (await this.objectTreeService.getChildrenByImplementedTypeId(entity))[web_site_const_1.PARAGRAPH_TYPE.name];
        entity.paragraphTrees = paragraphTrees ? paragraphTrees : [];
        entity.parentPageTitle = entity.treeNode.pageTitle
            ? entity.treeNode.pageTitle
            : entity.parentPageTitle;
        for (const paragraphTree of entity.paragraphTrees) {
            paragraphTree.parentPageTitle = paragraphTree.treeNode.pageTitle
                ? paragraphTree.treeNode.pageTitle
                : entity.parentPageTitle;
        }
    }
    async completeParagraphContainerTree(entity, ctx) {
        return this.completePageWithParagraphTree(entity, ctx);
    }
};
TransientWebSiteService = 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(services_1.ObjectTypeService)),
    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.__param(6, core_1.service(transient_content_generic_service_1.TransientContentGenericService)),
    tslib_1.__param(7, core_1.service(object_tree_service_1.ObjectTreeService)),
    tslib_1.__metadata("design:paramtypes", [transient_entity_service_1.TransientEntityService,
        inside_rest_service_1.InsideRestService,
        object_node_service_1.ObjectNodeService,
        services_1.ObjectTypeService,
        uri_complete_service_1.UriCompleteService,
        transient_uri_reference_service_1.TransientUriReferenceService,
        transient_content_generic_service_1.TransientContentGenericService,
        object_tree_service_1.ObjectTreeService])
], TransientWebSiteService);
exports.TransientWebSiteService = TransientWebSiteService;
//# sourceMappingURL=transient-web-site.service.js.map