"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionWebSiteService = void 0;
const tslib_1 = require("tslib");
const objects_website_1 = require("@jacquesparis/objects-website");
const core_1 = require("@loopback/core");
const helper_1 = require("../../helper");
const generated_response_1 = require("../../helper/generated-response");
const action_entity_service_1 = require("../../services/action-entity/action-entity.service");
const object_node_service_1 = require("../../services/object-node/object-node.service");
const generated_response_2 = require("./../../helper/generated-response");
const entity_name_1 = require("./../../models/entity-name");
const application_service_1 = require("./../../services/application.service");
const mustache_service_1 = require("./../../services/entity-definition/mustache.service");
const inside_rest_service_1 = require("./../../services/inside-rest/inside-rest.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_service_1 = require("./web-site.service");
let ActionWebSiteService = class ActionWebSiteService {
    constructor(actionEntityService, objectNodeService, uriCompleteService, insideRestService, mustacheService, webSiteService) {
        this.actionEntityService = actionEntityService;
        this.objectNodeService = objectNodeService;
        this.uriCompleteService = uriCompleteService;
        this.insideRestService = insideRestService;
        this.mustacheService = mustacheService;
        this.webSiteService = webSiteService;
        this.websiteGenerationService = objects_website_1.WebsiteGenerationService.get();
        this.actionEntityService.registerNewViewFunction(web_site_const_1.WEB_SITE_PROVIDER, ActionWebSiteService.name, 'Build ajax view', entity_name_1.EntityName.objectTree, 'ajax', web_site_const_1.WEB_SITE_VIEW_TYPE.name, this.ajaxWebSiteViewTree.bind(this), 'read');
        this.actionEntityService.registerNewViewFunction(web_site_const_1.WEB_SITE_PROVIDER, ActionWebSiteService.name, 'Build html view', entity_name_1.EntityName.objectTree, 'html', web_site_const_1.WEB_SITE_VIEW_TYPE.name, this.htmlWebSiteViewTree.bind(this), 'read');
        this.actionEntityService.registerNewViewFunction(web_site_const_1.WEB_SITE_PROVIDER, ActionWebSiteService.name, 'Build popup view', entity_name_1.EntityName.objectTree, 'popup', web_site_const_1.WEB_SITE_VIEW_TYPE.name, this.popupWebSiteViewTree.bind(this), 'read');
        this.doc = helper_1.contentGenericTemplate(__dirname, 'doc');
    }
    async popupWebSiteViewTree(entity, args, ctx) {
        const objectNode = await this.objectNodeService.getNode(args[0], application_service_1.CurrentContext.get(ctx, {}));
        return new generated_response_2.JsonGeneratedResponse(await this.webSiteService.getPopupContent(entity.treeNode.popupLinkLabels, objectNode, ctx));
    }
    async htmlWebSiteViewTree(entity, args, ctx) {
        const result = await this.ajaxWebSiteViewTree(entity, args, ctx);
        result.json.headerScripts.pageScript = `
    function getPageHref(page) {
      let baseUri = '${this.uriCompleteService.getUri(entity_name_1.EntityName.objectTree, entity.id, ctx)}/view/html';
      if (page && page.parentPage) {
        return baseUri + '/' +
          page.parentPage.pageId +
          '#' +
          page.parentPage.anchor;
      }
      return baseUri +
        (page ? '/' + page.treeNode.id : '');
    }
    `;
        return this.getHtmlDocResponseFromAjaxResult(result);
    }
    getHtmlDocResponseFromAjaxResult(response) {
        return new generated_response_2.HtmlGeneratedResponse(this.getHtmlDocFromAjaxResult(response));
    }
    getHtmlDocFromAjaxResult(response) {
        let result = undefined;
        if (response instanceof generated_response_1.TextGeneratedResponse) {
            result = this.mustacheService.parse(this.doc.templateMustache, {
                body: response.response,
                headerScript: this.doc.headerScript,
            });
        }
        else if (response instanceof generated_response_2.JsonGeneratedResponse) {
            const docParts = response.json;
            docParts.headerScript = this.doc.headerScript;
            result = this.mustacheService.parse(this.doc.templateMustache, docParts);
        }
        return result;
    }
    getPageHref(page, site, ctx) {
        const viewId = site.id +
            '/view/html' +
            (page
                ? '/' +
                    (page.parentPage
                        ? page.parentPage.pageId
                        : page.treeNode.id)
                : '') +
            (page && page.parentPage ? '#' + page.parentPage.anchor : '');
        return this.uriCompleteService.getUri(entity_name_1.EntityName.objectTree, viewId, ctx);
    }
    getPopupHref(page, site, ctx) {
        const viewId = site.id +
            '/view/popup' +
            (page ? '/' + page.treeNode.id : '');
        return this.uriCompleteService.getUri(entity_name_1.EntityName.objectTree, viewId, ctx);
    }
    getAdminHref(page, site, ctx) {
        return ('/admin/#/admin/owner/' +
            page.ownerType +
            '/' +
            page.ownerName +
            '/namespace/' +
            page.namespaceType +
            '/' +
            page.namespaceName);
    }
    async getWebSiteAjaxResponse(webSiteTree, getPageHref, getAdminHref, getPopupHref, ctx) {
        return this.ajaxWebSiteViewTree(webSiteTree, {}, ctx, getPageHref, getAdminHref, getPopupHref);
    }
    async getWebSitePageAjaxResponse(webSiteTree, pageNodeId, getPageHref, getAdminHref, getPopupHref, ctx) {
        return this.ajaxWebSiteViewTree(webSiteTree, { 0: pageNodeId }, ctx, getPageHref, getAdminHref, getPopupHref);
    }
    async ajaxWebSiteViewTree(entity, args, ctx, getPageHref = this.getPageHref.bind(this), getAdminHref = this.getAdminHref.bind(this), getPopupHref = this.getPopupHref.bind(this)) {
        const ajaxResult = await this.websiteGenerationService.getAjaxContent({
            getCachedOrRemoteObjectById: async (treeId) => {
                try {
                    return (await this.insideRestService.read(this.uriCompleteService.getUri(entity_name_1.EntityName.objectTree, treeId, ctx), ctx));
                }
                catch (error) {
                    return { children: [], treeNode: { name: 'unknown' } };
                }
            },
        }, {
            getCachedOrRemoteObjectById: async (nodeId) => {
                try {
                    return (await this.insideRestService.read(this.uriCompleteService.getUri(entity_name_1.EntityName.objectNode, nodeId, ctx), ctx));
                }
                catch (error) {
                    return { name: 'unknown' };
                }
            },
        }, {
            getPageHref: (page) => {
                return getPageHref(page, entity, ctx);
            },
            getAdminHref: (page) => {
                return getAdminHref(page, entity, ctx);
            },
            getPopupHref: (page) => {
                return getPopupHref(page, entity, ctx);
            },
        }, entity.id, args[0], args[1], args[2]);
        return new generated_response_2.JsonGeneratedResponse(ajaxResult);
    }
};
ActionWebSiteService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(action_entity_service_1.ActionEntityService)),
    tslib_1.__param(1, core_1.service(object_node_service_1.ObjectNodeService)),
    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.__param(4, core_1.service(mustache_service_1.MustacheService)),
    tslib_1.__param(5, core_1.service(web_site_service_1.WebSiteService)),
    tslib_1.__metadata("design:paramtypes", [action_entity_service_1.ActionEntityService,
        object_node_service_1.ObjectNodeService,
        uri_complete_service_1.UriCompleteService,
        inside_rest_service_1.InsideRestService,
        mustache_service_1.MustacheService,
        web_site_service_1.WebSiteService])
], ActionWebSiteService);
exports.ActionWebSiteService = ActionWebSiteService;
//# sourceMappingURL=action-web-site.service.js.map