"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UriCompleteService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const helper_1 = require("../../helper");
const entity_name_1 = require("./../../models/entity-name");
let UriCompleteService = class UriCompleteService {
    completeReturnedEntity(result, ctx) {
        this.addUri(result, ctx);
        ctx.uriContext.returnedEntity.value = result;
    }
    getUri(entityName, id, ctx) {
        const baseUri = ctx.uriContext.uri.value.baseUri;
        const objectUri = '/' + this.getEntityUri(entityName) + '/';
        return baseUri + objectUri + id;
    }
    addUri(
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    result, ctx, baseUri, objectUri, deep = 0) {
        if (deep > 2) {
            return;
        }
        if (!baseUri) {
            baseUri = ctx.uriContext.uri.value.baseUri;
        }
        if (!objectUri) {
            objectUri = ctx.uriContext.uri.value.objectUri;
        }
        if (lodash_1.isArray(result)) {
            result.forEach((item) => {
                this.addUri(item, ctx, baseUri, objectUri, deep);
            });
        }
        else if (lodash_1.isObject(result)) {
            if ('entityName' in result) {
                deep = 0;
                objectUri =
                    '/' +
                        this.getEntityUri(result.entityName) +
                        '/';
                //   delete (result as EntityType).entityName;
            }
            if ('id' in result &&
                !result.uri &&
                result.id &&
                '' !== result.id &&
                'null' !== result.id) {
                result.uri =
                    baseUri + objectUri + result.id;
            }
            Object.keys(result).forEach((key) => {
                if (lodash_1.isObject(result[key]) ||
                    lodash_1.isArray(result[key])) {
                    this.addUri(result[key], ctx, baseUri, `${objectUri}${result.id}/${this.getEntityUri(key)}/`, deep + 1);
                }
                else if (lodash_1.isString(result[key]) &&
                    key.endsWith('Id') &&
                    '' !== result[key] &&
                    'null' !== result[key]) {
                    const field = key.substr(0, key.length - 2);
                    if (!result[field + 'Uri']) {
                        const entityUri = this.getEntityUri(field);
                        if (entityUri === helper_1.toKebabCase(field)) {
                            result[field + 'Uri'] = `${baseUri}${objectUri}${result.id}/${entityUri}/${result[key]}`;
                        }
                        else {
                            result[field + 'Uri'] = `${baseUri}/${entityUri}/${result[key]}`;
                        }
                    }
                }
            });
        }
    }
    getEntityUri(entityName) {
        if (entityName.endsWith('ObjectNode')) {
            return 'object-nodes';
        }
        if (entityName.endsWith('ObjectTree')) {
            return 'object-trees';
        }
        if (entityName.endsWith('ObjectType')) {
            return 'object-types';
        }
        switch (entityName) {
            case entity_name_1.EntityName.objectType:
            case 'subObjectType':
                return 'object-types';
            case entity_name_1.EntityName.objectNode:
            case 'parentNode':
            case 'parentAcl':
            case 'parentOwner':
            case 'parentNamespace':
            case 'parentTree':
                return 'object-nodes';
            default:
                if (entityName in entity_name_1.EntityName) {
                    return helper_1.toKebabCase(entityName) + 's';
                }
                return helper_1.toKebabCase(entityName);
        }
    }
};
UriCompleteService = tslib_1.__decorate([
    core_1.injectable({ scope: core_1.BindingScope.SINGLETON })
], UriCompleteService);
exports.UriCompleteService = UriCompleteService;
//# sourceMappingURL=uri-complete.service.js.map