"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectNodeRepository = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const repository_1 = require("@loopback/repository");
const datasources_1 = require("../datasources");
const helper_1 = require("../helper");
const models_1 = require("../models");
const constants_1 = require("./../constants");
let ObjectNodeRepository = class ObjectNodeRepository extends repository_1.DefaultCrudRepository {
    constructor(dataSource, objectTypeRepositoryGetter, objectNodeRepositoryGetter) {
        super(models_1.ObjectNode, dataSource);
        this.objectTypeRepositoryGetter = objectTypeRepositoryGetter;
        this.objectNodeRepositoryGetter = objectNodeRepositoryGetter;
        this.parentNode = this.createBelongsToAccessorFor('parentNode', objectNodeRepositoryGetter);
        this.registerInclusionResolver('parentNode', this.parentNode.inclusionResolver);
        this.parentOwner = this.createBelongsToAccessorFor('parentOwner', objectNodeRepositoryGetter);
        this.registerInclusionResolver('parentOwner', this.parentOwner.inclusionResolver);
        this.parentTree = this.createBelongsToAccessorFor('parentTree', objectNodeRepositoryGetter);
        this.registerInclusionResolver('parentTree', this.parentTree.inclusionResolver);
        this.parentNamespace = this.createBelongsToAccessorFor('parentNamespace', objectNodeRepositoryGetter);
        this.registerInclusionResolver('parentNamespace', this.parentNamespace.inclusionResolver);
        this.parentACL = this.createBelongsToAccessorFor('parentACL', objectNodeRepositoryGetter);
        this.registerInclusionResolver('parentACL', this.parentACL.inclusionResolver);
        this.objectType = this.createBelongsToAccessorFor('objectType', objectTypeRepositoryGetter);
        this.registerInclusionResolver('objectType', this.objectType.inclusionResolver);
    }
    // to deal with retro-compatibility, before title existence
    buildTitle(dataNode) {
        if (dataNode && !dataNode.title) {
            if (dataNode.menuTitle) {
                dataNode.title = dataNode.menuTitle;
            }
            else if (dataNode.pageTitle) {
                dataNode.title = dataNode.pageTitle;
            }
            else if (dataNode.paragraphTitle) {
                dataNode.title = dataNode.paragraphTitle;
            }
            else if (dataNode.pageUrl) {
                dataNode.title = dataNode.pageUrl;
            }
            else if (dataNode.host) {
                dataNode.title =
                    'http://' +
                        dataNode.host +
                        (dataNode.path ? '/site/' + dataNode.path : '');
            }
            else {
                dataNode.title = helper_1.toStartCase(dataNode.name);
            }
        }
    }
    async find(filter, options) {
        const result = await super.find(filter, options);
        if (result) {
            for (const type of result) {
                this.buildTitle(type);
            }
        }
        return result;
    }
    async findById(id, filter, options) {
        const result = await super.findById(id, filter, options);
        this.buildTitle(result);
        return result;
    }
    async findOne(filter, options) {
        const result = await super.findOne(filter, options);
        this.buildTitle(result);
        return result;
    }
};
ObjectNodeRepository = tslib_1.__decorate([
    tslib_1.__param(0, core_1.inject(constants_1.DATASTORE_DB)),
    tslib_1.__param(1, repository_1.repository.getter('ObjectTypeRepository')),
    tslib_1.__param(2, repository_1.repository.getter('ObjectNodeRepository')),
    tslib_1.__metadata("design:paramtypes", [datasources_1.DbDataSource, Function, Function])
], ObjectNodeRepository);
exports.ObjectNodeRepository = ObjectNodeRepository;
//# sourceMappingURL=object-node.repository.js.map