"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionTreeService = exports.SortMethod = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const action_entity_service_1 = require("../action-entity/action-entity.service");
const application_service_1 = require("../application.service");
const application_error_1 = require("./../../helper/application-error");
const entity_name_1 = require("./../../models/entity-name");
const application_service_2 = require("./../application.service");
const inside_rest_service_1 = require("./../inside-rest/inside-rest.service");
const object_node_service_1 = require("./../object-node/object-node.service");
const object_tree_const_1 = require("./object-tree.const");
class SortMethod {
    constructor(objectNodeService) {
        this.objectNodeService = objectNodeService;
        this.providerId = object_tree_const_1.OBJECT_TREE_PROVIDER;
        this.serviceId = ActionTreeService.name;
        this.description = 'Sort tree children nodes';
        this.accessRightsScope = 'create';
    }
    async runMethod(objectTree, childrenIds, ctx) {
        for (let childIndex = 0; childIndex < childrenIds.length; childIndex++) {
            const childId = childrenIds[childIndex];
            const child = objectTree.children.find((tree) => childId === tree.treeNode.id);
            if (child && childIndex * 10 + 10 !== child.treeNode.index) {
                child.treeNode = await this.objectNodeService.modifyById(child.treeNode.id, { index: childIndex * 10 + 10 }, application_service_1.CurrentContext.get(ctx, {
                    nodeContext: { node: new application_service_1.ExpectedValue(child === null || child === void 0 ? void 0 : child.treeNode) },
                }), true);
            }
        }
        objectTree.children.sort((a, b) => {
            var _a, _b;
            return (undefined === ((_a = a.treeNode) === null || _a === void 0 ? void 0 : _a.index) ? 1000 : a.treeNode.index) -
                (undefined === ((_b = b.treeNode) === null || _b === void 0 ? void 0 : _b.index) ? 1000 : b.treeNode.index);
        });
    }
    async hasMethod(objectTree, ctx) {
        return true;
    }
}
exports.SortMethod = SortMethod;
class MoveToMethod {
    constructor(objectNodeService, applicationService) {
        this.objectNodeService = objectNodeService;
        this.applicationService = applicationService;
        this.providerId = object_tree_const_1.OBJECT_TREE_PROVIDER;
        this.serviceId = ActionTreeService.name;
        this.description = 'Move tree branch';
        this.accessRightsScope = 'delete';
    }
    async runMethod(objectTree, target, ctx) {
        var _a, _b;
        const insideRestService = await this.applicationService.app.getService(inside_rest_service_1.InsideRestService);
        const targetTree = (await insideRestService.read(target.targetUri, ctx));
        if (!((_b = (_a = targetTree === null || targetTree === void 0 ? void 0 : targetTree.entityCtx) === null || _a === void 0 ? void 0 : _a.actions) === null || _b === void 0 ? void 0 : _b.creations) ||
            !(objectTree.treeNode.objectTypeId in
                targetTree.entityCtx.actions.creations)) {
            throw application_error_1.ApplicationError.incompatible({
                sourceName: objectTree.treeNode.name,
                targetUri: target.targetUri,
            });
        }
        await this.objectNodeService.moveTo(objectTree.treeNode, targetTree.treeNode);
    }
    async hasMethod(objectTree, ctx) {
        return true;
    }
}
let ActionTreeService = class ActionTreeService {
    constructor(actionEntityService, objectNodeService, applicationService) {
        this.actionEntityService = actionEntityService;
        this.objectNodeService = objectNodeService;
        this.applicationService = applicationService;
        this.actionEntityService.registerNewMethodOrView(entity_name_1.EntityName.objectTree, 'sort', new SortMethod(objectNodeService));
        this.actionEntityService.registerNewMethodOrView(entity_name_1.EntityName.objectTree, 'moveTo', new MoveToMethod(objectNodeService, applicationService));
    }
};
ActionTreeService = 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(application_service_2.ApplicationService)),
    tslib_1.__metadata("design:paramtypes", [action_entity_service_1.ActionEntityService,
        object_node_service_1.ObjectNodeService,
        application_service_2.ApplicationService])
], ActionTreeService);
exports.ActionTreeService = ActionTreeService;
//# sourceMappingURL=action-tree.service.js.map