"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MapService = void 0;
const tslib_1 = require("tslib");
/* eslint-disable no-empty */
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const entity_name_1 = require("./../../models/entity-name");
const inside_rest_service_1 = require("./../../services/inside-rest/inside-rest.service");
const object_type_service_1 = require("./../../services/object-type.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_web_site_service_1 = require("./../web-site/transient-web-site.service");
const web_site_service_1 = require("./../web-site/web-site.service");
const map_const_1 = require("./map.const");
const map_interface_1 = require("./map.interface");
let MapService = class MapService {
    constructor(transientEntityService, insideRestService, objectTypeService, uriCompleteService, transientWebSiteService, webSiteService) {
        this.transientEntityService = transientEntityService;
        this.insideRestService = insideRestService;
        this.objectTypeService = objectTypeService;
        this.uriCompleteService = uriCompleteService;
        this.transientWebSiteService = transientWebSiteService;
        this.webSiteService = webSiteService;
        this.transientEntityService.registerTransientEntityTypeFunction(map_const_1.MAP_PROVIDER, MapService.name, 'Build map entries', entity_name_1.EntityName.objectNode, map_const_1.MAP_ENTRIES_TYPE.name, this.completeMapEntriesNode.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(map_const_1.MAP_PROVIDER, MapService.name, 'Add map references to map paragraph', entity_name_1.EntityName.objectNode, map_const_1.MAP_TYPE.name, this.completeMapNode.bind(this));
        this.webSiteService.registerEventContributor(this.contributeToEvent.bind(this));
        this.webSiteService.registerPopupContributor(this.contributeToPopup.bind(this));
    }
    async completeMapNode(objectNode, ctx) {
        var _a, _b, _c, _d, _e, _f, _g;
        if (!objectNode.mapEntriesObjectTreeId && ((_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.mapEntriesObjectTreeId) === null || _d === void 0 ? void 0 : _d.oneOf) &&
            objectNode.entityCtx.jsonSchema.properties.mapEntriesObjectTreeId.oneOf
                .length > 0) {
            objectNode.mapEntriesObjectTreeId =
                objectNode.entityCtx.jsonSchema.properties.mapEntriesObjectTreeId.oneOf[0].enum[0];
        }
        if (objectNode.mapEntriesObjectTreeId &&
            !objectNode.mapEntriesObjectTreeUri) {
            objectNode.mapEntriesObjectTreeUri = this.uriCompleteService.getUri(entity_name_1.EntityName.objectTree, objectNode.mapEntriesObjectTreeId, ctx);
        }
        if (objectNode.mapEntriesObjectTreeId) {
            objectNode.mapEntriesObjectNodeUri = this.uriCompleteService.getUri(entity_name_1.EntityName.objectNode, objectNode.mapEntriesObjectTreeId, ctx);
        }
        if (objectNode.mapEntriesObjectNodeUri) {
            try {
                const mapEntriesNode = (await this.insideRestService.read(objectNode.mapEntriesObjectNodeUri, ctx));
                if (mapEntriesNode.mapEntriesList) {
                    const oneOf = [];
                    for (const entryKey of Object.keys(mapEntriesNode.mapEntriesList)) {
                        oneOf.push({
                            enum: [entryKey],
                            title: mapEntriesNode.mapEntriesList[entryKey].title,
                        });
                    }
                    if (0 < oneOf.length && ((_g = (_f = (_e = objectNode.entityCtx) === null || _e === void 0 ? void 0 : _e.jsonSchema) === null || _f === void 0 ? void 0 : _f.properties) === null || _g === void 0 ? void 0 : _g.mapEntryKey)) {
                        objectNode.entityCtx.jsonSchema.properties.mapEntryKey.oneOf = oneOf;
                        if (!objectNode.mapEntryKey) {
                            objectNode.mapEntryKey = oneOf[0].enum[0];
                        }
                    }
                }
            }
            catch (error) { }
        }
        if (objectNode.mapEntriesObjectNodeUri && objectNode.mapEntryKey) {
            try {
                const mapEntriesNode = (await this.insideRestService.read(objectNode.mapEntriesObjectNodeUri, ctx));
                const map = mapEntriesNode.mapEntriesList[objectNode.mapEntryKey];
                objectNode.map = map;
            }
            catch (error) { }
        }
    }
    async getMapMenuEntries(entriesTree, mapMenus, menuEntryDef, ctx) {
        let positions = undefined;
        if (0 < mapMenus.length) {
            positions = {
                key: menuEntryDef.entryKey,
                title: menuEntryDef.entryName,
                positions: [],
            };
            await this.buildMapPositions(entriesTree, positions, mapMenus, ctx);
        }
        return positions;
    }
    async buildMapPositions(entriesTree, positions, children, ctx) {
        for (const mapEntry of children) {
            const newPosition = await this.buildMapEntry(entriesTree, mapEntry, ctx);
            if (newPosition) {
                positions.positions.push(newPosition);
            }
            if (mapEntry.children && 0 < mapEntry.children.length) {
                await this.buildMapPositions(entriesTree, positions, mapEntry.children, ctx);
            }
        }
    }
    async buildMapEntry(entriesTree, menuTree, ctx) {
        if (menuTree.treeNode.locationPosition &&
            2 === menuTree.treeNode.locationPosition.split(',').length) {
            return this.webSiteService.buildEvent(entriesTree, menuTree, map_interface_1.MapEntryNode, ctx);
        }
        return undefined;
    }
    async contributeToEvent(webSiteEvent, entriesTree, menuTree, ctx) {
        if (menuTree.treeNode.locationPosition) {
            const positionParts = menuTree.treeNode.locationPosition.split(',');
            if (2 === positionParts.length) {
                const position = [
                    Number(positionParts[0]),
                    Number(positionParts[1]),
                ];
                webSiteEvent.addEventMenuTitle(menuTree.treeNode.locationName, map_interface_1.MapEntryNode.TYPE);
                webSiteEvent.addSpecificFields({
                    position: position,
                    icon: menuTree.treeNode.locationType
                        ? menuTree.treeNode.locationType
                        : 'fas fa-splotch',
                });
            }
        }
        return true;
    }
    async contributeToPopup(popupNode, builder, ctx) {
        if (popupNode.locationName &&
            '' !== popupNode.locationName &&
            -1 ===
                lodash_1.indexOf(builder.popupParts.subTitleParts, popupNode.locationName) &&
            popupNode.locationName !== builder.popupParts.title) {
            builder.popupParts.subTitleParts.push(popupNode.locationName);
        }
        return true;
    }
    async completeMapEntriesNode(mapEntriesNode, ctx) {
        mapEntriesNode.mapEntriesList = {};
        try {
            const webSiteTree = (await this.insideRestService.read(mapEntriesNode.webSiteObjectTreeUri, ctx));
            const implementingMapEntry = await this.objectTypeService.getImplementingTypes(map_const_1.MAP_ENTRY_TYPE.name);
            for (const menuEntryDef of webSiteTree.treeNode.menuEntries) {
                if (lodash_1.intersection(implementingMapEntry, menuEntryDef.entryTypes).length > 0) {
                    const mapEntriesTree = (await this.insideRestService.read(this.uriCompleteService.getUri(entity_name_1.EntityName.objectTree, mapEntriesNode.id, ctx), ctx));
                    const menuTrees = mapEntriesNode.menuEntriesList &&
                        menuEntryDef.entryKey in mapEntriesNode.menuEntriesList
                        ? mapEntriesNode.menuEntriesList[menuEntryDef.entryKey].children
                        : await this.transientWebSiteService.lookForMenuEntries([mapEntriesTree], menuEntryDef.entryTypes, mapEntriesTree, menuEntryDef.entryKey, menuEntryDef.menuEntryLabelKey
                            ? menuEntryDef.menuEntryLabelKey
                            : 'name', !!menuEntryDef.adminEntry);
                    mapEntriesNode.mapEntriesList[menuEntryDef.entryKey] = await this.getMapMenuEntries(mapEntriesTree, menuTrees, menuEntryDef, ctx);
                }
            }
        }
        catch (error) { }
    }
};
MapService = 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_type_service_1.ObjectTypeService)),
    tslib_1.__param(3, core_1.service(uri_complete_service_1.UriCompleteService)),
    tslib_1.__param(4, core_1.service(transient_web_site_service_1.TransientWebSiteService)),
    tslib_1.__param(5, core_1.service(web_site_service_1.WebSiteService)),
    tslib_1.__metadata("design:paramtypes", [transient_entity_service_1.TransientEntityService,
        inside_rest_service_1.InsideRestService,
        object_type_service_1.ObjectTypeService,
        uri_complete_service_1.UriCompleteService,
        transient_web_site_service_1.TransientWebSiteService,
        web_site_service_1.WebSiteService])
], MapService);
exports.MapService = MapService;
//# sourceMappingURL=map.service.js.map