"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransientImageService = void 0;
const tslib_1 = require("tslib");
/* eslint-disable no-empty */
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const helper_1 = require("../../helper");
const services_1 = require("../../services");
const entity_name_1 = require("./../../models/entity-name");
const inside_rest_service_1 = require("./../../services/inside-rest/inside-rest.service");
const object_node_service_1 = require("./../../services/object-node/object-node.service");
const object_type_service_1 = require("./../../services/object-type.service");
const transient_entity_service_1 = require("./../../services/transient-entity/transient-entity.service");
const content_image_const_1 = require("./content-image.const");
const content_image_definition_1 = require("./content-image.definition");
let TransientImageService = class TransientImageService {
    constructor(transientEntityService, insideRestService, contentImageService, objectTreeService, objectNodeService, objectTypeService) {
        this.transientEntityService = transientEntityService;
        this.insideRestService = insideRestService;
        this.contentImageService = contentImageService;
        this.objectTreeService = objectTreeService;
        this.objectNodeService = objectNodeService;
        this.objectTypeService = objectTypeService;
        this.transientEntityService.registerTransientEntityTypeFunction(content_image_const_1.CONTENT_IMAGE_PROVIDER, TransientImageService.name, 'Add referenced images field and its json schema definition. Add load Images method. Add create Gallery method.', entity_name_1.EntityName.objectNode, content_image_const_1.IMAGE_GALLERY_REFERRER_TYPE.name, this.completeImageGalleryReferrerNode.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(content_image_const_1.CONTENT_IMAGE_PROVIDER, TransientImageService.name, 'Add load Images method definition to load several images in one operation', entity_name_1.EntityName.objectNode, content_image_const_1.IMAGE_GALLERY_TYPE.name, this.completeImageGalleryTypeNode.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(content_image_const_1.CONTENT_IMAGE_PROVIDER, TransientImageService.name, 'Add all images of the gallery as referenced images', entity_name_1.EntityName.objectTree, content_image_const_1.DISPLAYED_IMAGE_GALLERY_TYPE.name, this.completeDisplayedImageGalleryTree.bind(this));
    }
    async completeDisplayedImageGalleryTree(entity, ctx) {
        const images = (await this.objectTreeService.getChildrenByImplementedTypeId(entity))[content_image_const_1.IMAGE_TYPE.name];
        entity.images = images ? images : [];
    }
    async completeImageGalleryTypeNode(objectNode, ctx) {
        await this.addLoadImagesJsonSchemaMethod(objectNode, ctx);
    }
    async getImageGalleriesParents(objectNode) {
        const galleriesType = await this.objectTypeService.getParentTypesOfImplementingType(content_image_const_1.IMAGE_GALLERY_TYPE.name);
        return this.objectNodeService.searchByParentNamespaceId(objectNode.parentNamespaceId, {
            objectTypeIds: galleriesType,
        });
    }
    async addCreateImagesGalleryJsonSchemaMethod(objectNode, ctx) {
        var _a, _b, _c, _d, _e, _f, _g;
        if (!objectNode.entityCtx) {
            objectNode.entityCtx = { entityType: entity_name_1.EntityName.objectNode };
        }
        if (!((_a = objectNode.entityCtx) === null || _a === void 0 ? void 0 : _a.actions)) {
            objectNode.entityCtx.actions = {};
        }
        if (!objectNode.entityCtx.actions.methods) {
            objectNode.entityCtx.actions.methods = [];
        }
        const imageGalleries = await this.getImageGalleriesParents(objectNode);
        if (imageGalleries && 0 < imageGalleries.length) {
            objectNode.entityCtx.actions.methods.push({
                methodId: 'createGallery',
                methodName: 'Create images gallery',
                actionName: 'Create gallery',
                parameters: {
                    type: 'object',
                    properties: {
                        useExistingGallery: {
                            type: 'boolean',
                            title: 'Use existing gallery',
                        },
                        name: {
                            type: 'string',
                            title: 'Gallery name',
                        },
                    },
                },
                handlebarsMethodSampling: `[
          {{^useExistingGallery}}
            {
              "methodId": "createGallery",
              "parameters":{
                "name": {{&json name}},
                "parentId": {{&json parentId}}
              }
            }
          {{/useExistingGallery}}
          {{#useExistingGallery}}
            {
              "methodId": "configureGallery",
              "parameters":{
                "imageGalleryObjectTreeId": {{&json imageGalleryObjectTreeId}}
              }
            }
          {{/useExistingGallery}}
          {{#images}},
          {
            "methodId":"load",
            "parameters": {
              "images": [
                {{&json this}}
              ]
            }
          }{{/images}}
          ]`,
                icon: 'far fa-images',
            });
            const properties = objectNode.entityCtx.actions.methods[objectNode.entityCtx.actions.methods.length - 1].parameters.properties;
            if (1 < imageGalleries.length) {
                properties.parentId = {
                    type: 'string',
                    title: 'Galleries parent',
                    required: true,
                    oneOf: imageGalleries.map((gallery) => ({
                        enum: [gallery.id],
                        title: gallery.name,
                    })),
                };
            }
            if (((_e = (_d = (_c = (_b = objectNode.entityCtx) === null || _b === void 0 ? void 0 : _b.jsonSchema) === null || _c === void 0 ? void 0 : _c.properties.imageGalleryObjectTreeId) === null || _d === void 0 ? void 0 : _d.oneOf) === null || _e === void 0 ? void 0 : _e.length) > 0) {
                helper_1.addCondition('true !== model.useExistingGallery', properties.name);
                if (properties.parentId) {
                    helper_1.addCondition('true !== model.useExistingGallery', properties.parentId);
                }
                properties.imageGalleryObjectTreeId = lodash_1.cloneDeep((_g = (_f = objectNode.entityCtx) === null || _f === void 0 ? void 0 : _f.jsonSchema) === null || _g === void 0 ? void 0 : _g.properties.imageGalleryObjectTreeId);
                properties.imageGalleryObjectTreeId.required = true;
                helper_1.addCondition('true === model.useExistingGallery', properties.imageGalleryObjectTreeId);
            }
            else {
                delete properties.useExistingGallery;
            }
            properties.images = {
                type: 'array',
                'x-schema-form': {
                    type: 'images',
                },
                items: (await this.contentImageService.getContentDefinition())
                    .properties.contentImage,
            };
            helper_1.addCondition('(true === model.useExistingGallery && model.imageGalleryObjectTreeId) || !model.useExistingGallery', properties.images);
        }
    }
    async addLoadImagesJsonSchemaMethod(objectNode, ctx) {
        var _a;
        if (!objectNode.entityCtx) {
            objectNode.entityCtx = { entityType: entity_name_1.EntityName.objectNode };
        }
        if (!((_a = objectNode.entityCtx) === null || _a === void 0 ? void 0 : _a.actions)) {
            objectNode.entityCtx.actions = {};
        }
        if (!objectNode.entityCtx.actions.methods) {
            objectNode.entityCtx.actions.methods = [];
        }
        objectNode.entityCtx.actions.methods.push({
            methodId: 'load',
            methodName: 'Add images',
            actionName: 'Load images',
            parameters: {
                type: 'object',
                properties: {
                    images: {
                        type: 'array',
                        'x-schema-form': {
                            type: 'images',
                        },
                        items: (await this.contentImageService.getContentDefinition())
                            .properties.contentImage,
                    },
                },
            },
            handlebarsMethodSampling: `[{{#images}}
          {
            "methodId":"load",
            "parameters": {
              "images": [
                {{&json this}}
              ]
            }
          }{{#unless @last}},{{/unless}}{{/images}}
        ]`,
            icon: 'far fa-images',
        });
    }
    async completeImageGalleryReferrerNode(objectNode, ctx) {
        var _a, _b, _c, _d, _e;
        const images = [];
        if (!objectNode.imageGalleryTree && objectNode.imageGalleryObjectTreeUri) {
            try {
                objectNode.imageGalleryTree = (await this.insideRestService.read(objectNode.imageGalleryObjectTreeUri, ctx));
            }
            catch (error) { }
        }
        if ((_a = objectNode.imageGalleryTree) === null || _a === void 0 ? void 0 : _a.children) {
            for (const image of objectNode.imageGalleryTree.children) {
                images.push(image);
            }
        }
        objectNode.images = images;
        if (-1 <
            lodash_1.indexOf((_b = objectNode.entityCtx) === null || _b === void 0 ? void 0 : _b.implementedTypes, content_image_const_1.IMAGE_GALLERY_SELECTOR_TYPE.name) && ((_e = (_d = (_c = objectNode.entityCtx) === null || _c === void 0 ? void 0 : _c.jsonSchema) === null || _d === void 0 ? void 0 : _d.properties) === null || _e === void 0 ? void 0 : _e.selectedImages)) {
            if (0 < images.length) {
                objectNode.entityCtx.jsonSchema.properties.selectedImages.items.enum = [];
                objectNode.entityCtx.jsonSchema.properties.selectedImages.items.enumNames = [];
                for (const image of images) {
                    if (-1 ===
                        lodash_1.indexOf(objectNode.entityCtx.jsonSchema.properties.selectedImages.items
                            .enum, image.treeNode.name)) {
                        objectNode.entityCtx.jsonSchema.properties.selectedImages.items.enum.push(image.treeNode.name);
                        objectNode.entityCtx.jsonSchema.properties.selectedImages.items.enumNames.push(image.treeNode.name +
                            '<span class="imageSelect"><img onclick="showImg(\'' +
                            image.treeNode.contentImageUri +
                            '\')" src="' +
                            image.treeNode.contentImageUri +
                            '" ></span>');
                    }
                }
                if (objectNode.selectedImages) {
                    objectNode.images = lodash_1.filter(images, (image) => -1 < lodash_1.indexOf(objectNode.selectedImages, image.treeNode.name));
                }
                else {
                    objectNode.selectedImages =
                        objectNode.entityCtx.jsonSchema.properties.selectedImages.items.enum;
                }
                helper_1.addCondition("model.imageGalleryObjectTreeId=='" +
                    objectNode.imageGalleryObjectTreeId +
                    "'", objectNode.entityCtx.jsonSchema.properties.selectedImages);
            }
            else {
                delete objectNode.entityCtx.jsonSchema.properties.selectedImages;
            }
        }
        if (objectNode.imageGalleryTree) {
            await this.addLoadImagesJsonSchemaMethod(objectNode, ctx);
        }
        else {
            await this.addCreateImagesGalleryJsonSchemaMethod(objectNode, ctx);
        }
    }
};
TransientImageService = 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(content_image_definition_1.ContentImageService)),
    tslib_1.__param(3, core_1.service(services_1.ObjectTreeService)),
    tslib_1.__param(4, core_1.service(object_node_service_1.ObjectNodeService)),
    tslib_1.__param(5, core_1.service(object_type_service_1.ObjectTypeService)),
    tslib_1.__metadata("design:paramtypes", [transient_entity_service_1.TransientEntityService,
        inside_rest_service_1.InsideRestService,
        content_image_definition_1.ContentImageService,
        services_1.ObjectTreeService,
        object_node_service_1.ObjectNodeService,
        object_type_service_1.ObjectTypeService])
], TransientImageService);
exports.TransientImageService = TransientImageService;
//# sourceMappingURL=transient-image.service.js.map