"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentImageTemplateService = void 0;
const tslib_1 = require("tslib");
/* eslint-disable no-empty */
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const models_1 = require("../../models");
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/web-site.const");
const web_site_service_1 = require("./../web-site/web-site.service");
let ContentImageTemplateService = class ContentImageTemplateService {
    constructor(webSiteService, objectTypeService, uriCompleteService, insideRestService) {
        this.webSiteService = webSiteService;
        this.objectTypeService = objectTypeService;
        this.uriCompleteService = uriCompleteService;
        this.insideRestService = insideRestService;
        this.webSiteService.registerPopupContributor(this.contributeToPopup.bind(this));
    }
    async contributeToPopup(popupNode, popupBuilder, ctx) {
        if (popupNode.images && 0 < popupNode.images.length) {
            for (const image of popupNode.images) {
                popupBuilder.popupParts.images.push({
                    uri: image.thumb.contentImageUri,
                    text: image.treeNode.imageTitle
                        ? image.treeNode.imageTitle
                        : image.treeNode.name,
                });
            }
        }
        try {
            const popupTree = (await this.insideRestService.read(this.uriCompleteService.getUri(models_1.EntityName.objectTree, popupNode.id, ctx), ctx));
            await this.lookForImages(popupTree, popupBuilder.popupParts.images, ctx);
        }
        catch (error) { }
        if (popupNode.imageGalleryTree &&
            -1 <
                lodash_1.indexOf(await this.objectTypeService.getImplementedTypes(popupNode.imageGalleryTree.treeNode.objectTypeId), web_site_const_1.PAGE_TYPE.name)) {
            popupBuilder.pageHrefs['__href_' + popupNode.imageGalleryTree.treeNode.id] = {
                pageId: popupNode.imageGalleryTree.treeNode.id,
                pageName: popupNode.imageGalleryTree.treeNode.name,
            };
            popupBuilder.popupParts.links.push({
                uri: '__href_' + popupNode.imageGalleryTree.treeNode.id,
                text: popupBuilder.popupLinkLabels.galleryLinkLabel
                    ? popupBuilder.popupLinkLabels.galleryLinkLabel
                    : 'Navigate to gallery',
            });
        }
        return true;
    }
    async lookForImages(objectTree, images, ctx) {
        for (const child of objectTree.children) {
            if (child.treeNode.imageGalleryObjectTreeId) {
                try {
                    const imagesNode = await this.insideRestService.read(child.treeNode.uri, ctx);
                    if (imagesNode.images && 0 < imagesNode.images.length) {
                        for (const image of imagesNode.images) {
                            if (-1 ===
                                lodash_1.indexOf(lodash_1.map(images, (img) => img.uri), image.thumb.contentImageUri)) {
                                images.push({
                                    uri: image.thumb.contentImageUri,
                                    text: image.treeNode.imageTitle
                                        ? image.treeNode.imageTitle
                                        : image.treeNode.name,
                                });
                            }
                        }
                    }
                }
                catch (error) { }
            }
        }
        for (const child of objectTree.children) {
            await this.lookForImages(child, images, ctx);
        }
    }
};
ContentImageTemplateService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(web_site_service_1.WebSiteService)),
    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(inside_rest_service_1.InsideRestService)),
    tslib_1.__metadata("design:paramtypes", [web_site_service_1.WebSiteService,
        object_type_service_1.ObjectTypeService,
        uri_complete_service_1.UriCompleteService,
        inside_rest_service_1.InsideRestService])
], ContentImageTemplateService);
exports.ContentImageTemplateService = ContentImageTemplateService;
//# sourceMappingURL=content-image-template.service.js.map