"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessRightsInit = void 0;
const application_service_1 = require("./../application.service");
const access_rights_const_1 = require("./access-rights.const");
class AccessRightsInit {
    constructor(appCtx, objectTypeService, objectNodeService) {
        this.appCtx = appCtx;
        this.objectTypeService = objectTypeService;
        this.objectNodeService = objectNodeService;
        this.ready = new Promise((resolve, reject) => {
            this.init().then(() => {
                resolve();
            }, (error) => {
                reject(error);
            });
        });
    }
    async init() {
        const userType = await this.appCtx.userType.getOrSetValue(async () => {
            return this.objectTypeService.registerApplicationType({
                name: application_service_1.ApplicationService.OBJECT_TYPE_NAMES.USER,
                contentType: application_service_1.ApplicationService.CONTENT_TYPE.USER,
            });
        });
        const anonymousUserType = await this.appCtx.anonymousUserType.getOrSetValue(async () => {
            return this.objectTypeService.registerApplicationType({
                name: application_service_1.ApplicationService.OBJECT_TYPE_NAMES.ANONYMOUS_USER,
            });
        });
        const accessRightsDefinition = await this.appCtx.accessRightsDefinitionType.getOrSetValue(async () => {
            return this.objectTypeService.registerApplicationType(access_rights_const_1.ACCESS_RIGHTS_DEFINITION_TYPE);
        });
        const accessRightsGroup = await this.appCtx.accessRightsGroupType.getOrSetValue(async () => {
            return this.objectTypeService.registerApplicationType(access_rights_const_1.ACCESS_RIGHTS_GROUP_TYPE);
        });
        const accessRightsOwners = await this.appCtx.accessRightsOwnersType.getOrSetValue(async () => {
            return this.objectTypeService.registerApplicationType(access_rights_const_1.ACCESS_RIGHTS_OWNERS_TYPE);
        });
        const accessRightsAccessManagers = await this.appCtx.accessRightsAccessManagersType.getOrSetValue(async () => {
            return this.objectTypeService.registerApplicationType(access_rights_const_1.ACCESS_RIGHTS_ACCESS_MANAGERS_TYPE);
        });
        await this.objectTypeService.getOrCreateObjectSubType((await this.appCtx.repositoryType.waitForValue).id, accessRightsDefinition.id, access_rights_const_1.ACCESS_RIGHT_SUBTYPE);
        await this.objectTypeService.getOrCreateObjectSubType(accessRightsDefinition.id, accessRightsGroup.id, {
            name: application_service_1.ApplicationService.OBJECT_TYPE_NAMES.ACCESS_RIGHT_GROUP,
            acl: false,
            namespace: false,
            owner: false,
            tree: false,
        });
        await this.objectTypeService.getOrCreateObjectSubType(accessRightsGroup.id, userType.id, {
            name: application_service_1.ApplicationService.OBJECT_TYPE_NAMES.USER,
            acl: false,
            namespace: false,
            owner: false,
            tree: false,
        });
        await this.objectTypeService.getOrCreateObjectSubType(accessRightsGroup.id, anonymousUserType.id, {
            name: application_service_1.ApplicationService.OBJECT_TYPE_NAMES.ANONYMOUS_USER,
            acl: false,
            namespace: false,
            owner: false,
            tree: false,
            max: 1,
        });
        await this.objectTypeService.getOrCreateObjectSubType(accessRightsDefinition.id, accessRightsOwners.id, {
            name: application_service_1.ApplicationService.OBJECT_TYPE_NAMES.ACCESS_RIGHT_OWNERS,
            acl: false,
            namespace: false,
            owner: false,
            tree: false,
            max: 1,
        });
        await this.objectTypeService.getOrCreateObjectSubType(accessRightsOwners.id, userType.id, {
            name: application_service_1.ApplicationService.OBJECT_TYPE_NAMES.USER,
            acl: false,
            namespace: false,
            owner: false,
            tree: false,
        });
        await this.objectTypeService.getOrCreateObjectSubType(accessRightsDefinition.id, accessRightsAccessManagers.id, {
            name: application_service_1.ApplicationService.OBJECT_TYPE_NAMES.ACCESS_RIGHT_ACCESS_MANAGERS,
            acl: false,
            namespace: false,
            owner: false,
            tree: false,
            max: 1,
        });
        await this.objectTypeService.getOrCreateObjectSubType(accessRightsAccessManagers.id, userType.id, {
            name: application_service_1.ApplicationService.OBJECT_TYPE_NAMES.USER,
            acl: false,
            namespace: false,
            owner: false,
            tree: false,
        });
        await this.objectTypeService.getOrCreateObjectSubType((await this.appCtx.tenantType.waitForValue).id, accessRightsDefinition.id, access_rights_const_1.ACCESS_RIGHT_SUBTYPE);
        const rootACL = (await this.objectNodeService.getOrCreateChildren((await this.appCtx.rootNode.waitForValue).id, accessRightsDefinition.id, { name: application_service_1.ApplicationService.OBJECT_NODE_NAMES.AccessRightsDefinition }))[0];
        const rootACLOwners = (await this.objectNodeService.getOrCreateChildren(rootACL.id, accessRightsOwners.id, access_rights_const_1.ACCESS_RIGHT_OWNERS_NODE))[0];
        const rooOwner = (await this.objectNodeService.getOrCreateChildren(rootACLOwners.id, userType.id, {
            name: 'Jacques Lebourgeois',
            contentUser: 'jacques.lebourgeois@gmail.com',
        }))[0];
    }
}
exports.AccessRightsInit = AccessRightsInit;
//# sourceMappingURL=access-rights.init.js.map