"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebSiteService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const sanitize_html_1 = tslib_1.__importDefault(require("sanitize-html"));
const helper_1 = require("../../helper");
const entity_name_1 = require("./../../models/entity-name");
const mustache_service_1 = require("./../../services/entity-definition/mustache.service");
const inside_rest_service_1 = require("./../../services/inside-rest/inside-rest.service");
const object_type_service_1 = require("./../../services/object-type.service");
const uri_complete_service_1 = require("./../../services/uri-complete/uri-complete.service");
const web_site_const_1 = require("./web-site.const");
const web_site_interface_1 = require("./web-site.interface");
let WebSiteService = class WebSiteService {
    constructor(insideRestService, objectTypeService, uriCompleteService, mustacheService) {
        this.insideRestService = insideRestService;
        this.objectTypeService = objectTypeService;
        this.uriCompleteService = uriCompleteService;
        this.mustacheService = mustacheService;
        this.popupContributors = [];
        this.eventContributors = [];
        this.eventCreationMethodContributors = [];
        this.popup = helper_1.contentGenericTemplate(__dirname, 'popup');
    }
    registerPopupContributor(popupContributor) {
        this.popupContributors.push(popupContributor);
    }
    registerEventContributor(eventContributor, addEventMethodContributor) {
        this.eventContributors.push(eventContributor);
        if (addEventMethodContributor) {
            this.eventCreationMethodContributors.push(addEventMethodContributor);
        }
    }
    async completeEventMethodDefinition(methodDefinition) {
        for (const contributor of this.eventCreationMethodContributors) {
            await contributor.declareEventCreationMethod(methodDefinition);
        }
    }
    async completeEventObjectNodeCreation(objectNode, args) {
        for (const contributor of this.eventCreationMethodContributors) {
            await contributor.buildEventObjectNode(objectNode, args);
        }
    }
    async buildEvent(entriesTree, menuTree, eventType, ctx) {
        const event = await web_site_interface_1.WebSiteEvent.get(entriesTree, menuTree, eventType, this.eventContributors, ctx);
        return event;
    }
    async getPopupContent(popupLinkLabels, objectNode, ctx) {
        var _a;
        let popupNode = objectNode;
        if (!((_a = popupNode.entityCtx) === null || _a === void 0 ? void 0 : _a.loaded)) {
            try {
                popupNode = (await this.insideRestService.read(this.uriCompleteService.getUri(entity_name_1.EntityName.objectNode, popupNode.id, ctx), ctx));
            }
            catch (error) { }
        }
        const popupBuilder = {
            popupLinkLabels: popupLinkLabels ? popupLinkLabels : {},
            pageHrefs: {},
            popupParts: {
                uri: undefined,
                title: popupNode.menuTitle
                    ? popupNode.menuTitle
                    : popupNode.pageTitle
                        ? popupNode.pageTitle
                        : popupNode.paragraphTitle
                            ? popupNode.paragraphTitle
                            : popupNode.name,
                subTitle: undefined,
                subTitleParts: [],
                texts: [],
                images: [],
                imageBlocs: [],
                links: [],
                btns: [],
            },
        };
        if (popupNode.contentText && '' !== popupNode.contentText) {
            let text = sanitize_html_1.default(popupNode.contentText);
            if (text.length > 100) {
                text = text.substr(0, 100) + '...';
            }
            popupBuilder.popupParts.texts.push({ text: text, uri: undefined });
        }
        if (popupBuilder.popupLinkLabels.pageLinkLabel &&
            -1 <
                lodash_1.indexOf(await this.objectTypeService.getImplementedTypes(popupNode.objectTypeId), web_site_const_1.PAGE_TYPE.name)) {
            const popupTree = await this.insideRestService.read(this.uriCompleteService.getUri(entity_name_1.EntityName.objectTree, popupNode.id, ctx), ctx);
            popupBuilder.pageHrefs['__href_' + popupNode.id] = {
                pageId: popupNode.id,
                pageName: popupNode.name,
                parentPage: popupTree.parentPage,
            };
            popupBuilder.popupParts.links.push({
                uri: '__href_' + popupNode.id,
                text: popupBuilder.popupLinkLabels.pageLinkLabel,
            });
        }
        for (const contributor of this.popupContributors) {
            if (!(await contributor(popupNode, popupBuilder, ctx))) {
                break;
            }
        }
        if (0 < popupBuilder.popupParts.subTitleParts.length) {
            popupBuilder.popupParts.subTitle = popupBuilder.popupParts.subTitleParts.join(', ');
        }
        if (5 < popupBuilder.popupParts.images.length) {
            const nbOfImages = popupBuilder.popupParts.images.length;
            const step = Math.ceil(nbOfImages / 11) + 1;
            let index = step;
            const availableImages = lodash_1.cloneDeep(popupBuilder.popupParts.images);
            popupBuilder.popupParts.imageBlocs.push([]);
            popupBuilder.popupParts.imageBlocs.push([]);
            while (availableImages.length > 0) {
                index = index % availableImages.length;
                popupBuilder.popupParts.imageBlocs[0].push(...availableImages.splice(index, 1));
                index += step;
                if (availableImages.length > 0) {
                    index = index % availableImages.length;
                    popupBuilder.popupParts.imageBlocs[1].push(...availableImages.splice(index, 1));
                    index += step;
                }
            }
        }
        else {
            popupBuilder.popupParts.imageBlocs.push(lodash_1.cloneDeep(popupBuilder.popupParts.images));
        }
        return {
            uris: popupBuilder.pageHrefs,
            text: this.mustacheService.parse(this.popup.templateMustache, lodash_1.merge({
                hasImg: 0 < popupBuilder.popupParts.images.length,
                hasSeveralImagesBlocs: 1 < popupBuilder.popupParts.imageBlocs.length,
            }, popupBuilder.popupParts)),
        };
    }
};
WebSiteService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(inside_rest_service_1.InsideRestService)),
    tslib_1.__param(1, core_1.service(object_type_service_1.ObjectTypeService)),
    tslib_1.__param(2, core_1.service(uri_complete_service_1.UriCompleteService)),
    tslib_1.__param(3, core_1.service(mustache_service_1.MustacheService)),
    tslib_1.__metadata("design:paramtypes", [inside_rest_service_1.InsideRestService,
        object_type_service_1.ObjectTypeService,
        uri_complete_service_1.UriCompleteService,
        mustache_service_1.MustacheService])
], WebSiteService);
exports.WebSiteService = WebSiteService;
//# sourceMappingURL=web-site.service.js.map