"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectTreeProvider = void 0;
const extension_provider_1 = require("../../integration/extension.provider");
const application_service_1 = require("./../application.service");
const action_tree_service_1 = require("./action-tree.service");
const object_tree_const_1 = require("./object-tree.const");
class ObjectTreeProvider extends extension_provider_1.ExtensionProvider {
    constructor(app) {
        super(object_tree_const_1.OBJECT_TREE_PROVIDER, app);
        this.app = app;
        this.services.push({ cls: action_tree_service_1.ActionTreeService });
        this.objectTypes.push(object_tree_const_1.REPOSITORY_TYPE, object_tree_const_1.FOLDER_TYPE, object_tree_const_1.TENANT_TYPE, object_tree_const_1.REPOSITORY_CATEGORY_TYPE, object_tree_const_1.ROOT_TYPE);
        this.objectSubTypes.push(object_tree_const_1.REPOSITORY_REPOSITORY_SUBTYPE, object_tree_const_1.REPOSITORY_REPOSITORY_CATEGORY_SUBTYPE, object_tree_const_1.REPOSITORY_TENANT_SUBTYPE, object_tree_const_1.FOLDER_FOLDER_SUBTYPE);
        this.objectTrees.public = {
            parentNode: () => this.appCtx.rootNode.value,
            treeNodeTypeId: object_tree_const_1.REPOSITORY_TYPE.name,
            treeNodeName: 'public',
            tree: { treeNode: {}, children: {} },
        };
        this.objectTrees.publicTemplates = {
            parentNode: () => this.appCtx.publicNode.value,
            treeNodeTypeId: application_service_1.ApplicationService.OBJECT_TYPE_NAMES.REPOSITORY_CATEGORY,
            treeNodeName: object_tree_const_1.TEMPLATES_OBJECT_NAME,
            tree: { treeNode: {}, children: {} },
        };
        this.objectTrees.demonstrationAccount = {
            parentNode: () => this.appCtx.rootNode.value,
            treeNodeTypeId: object_tree_const_1.TENANT_TYPE.name,
            treeNodeName: 'Demonstration',
            tree: { treeNode: {}, children: {} },
        };
        this.objectTrees.demonstrationExamples = {
            parentNode: () => this.appCtx.demonstrationAccountNode.value,
            treeNodeTypeId: object_tree_const_1.FOLDER_TYPE.name,
            treeNodeName: 'Examples',
            tree: { treeNode: {}, children: {} },
        };
        this.objectTrees.demonstrationSandbox = {
            parentNode: () => this.appCtx.demonstrationAccountNode.value,
            treeNodeTypeId: object_tree_const_1.FOLDER_TYPE.name,
            treeNodeName: 'Sandbox',
            tree: { treeNode: {}, children: {} },
        };
    }
    async boot() {
        await this.appCtx.rootNode.getOrSetValue(async () => {
            let newRoot = await this.objectNodeService.searchOwner(object_tree_const_1.ROOT_TYPE.name, application_service_1.ApplicationService.OBJECT_NODE_NAMES[application_service_1.ApplicationService.OBJECT_TYPE_NAMES.REPOSITORY]);
            if (!newRoot) {
                // Look for Convert Root Repository
                newRoot = await this.objectNodeService.searchOwner(object_tree_const_1.REPOSITORY_TYPE.name, application_service_1.ApplicationService.OBJECT_NODE_NAMES[application_service_1.ApplicationService.OBJECT_TYPE_NAMES.REPOSITORY]);
                if (newRoot) {
                    // Convert Root Repository to Root
                    newRoot = await this.objectNodeService.modifyById(newRoot.id, {
                        objectTypeId: object_tree_const_1.ROOT_TYPE.name,
                    }, application_service_1.CurrentContext.get(new application_service_1.CurrentContext(), {
                        nodeContext: { node: new application_service_1.ExpectedValue(newRoot) },
                    }), true);
                }
                else {
                    newRoot = await this.objectNodeService.add({
                        name: application_service_1.ApplicationService.OBJECT_NODE_NAMES[application_service_1.ApplicationService.OBJECT_TYPE_NAMES.REPOSITORY],
                        objectTypeId: object_tree_const_1.REPOSITORY_TYPE.name,
                        owner: true,
                        tree: true,
                        namesapce: true,
                        acl: true,
                    }, new application_service_1.CurrentContext(), true);
                }
            }
            return newRoot;
        });
        await super.boot();
    }
}
exports.ObjectTreeProvider = ObjectTreeProvider;
//# sourceMappingURL=object-tree.provider.js.map