"use strict";
var TransientEntityService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransientEntityService = exports.onlineDescription = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const application_service_1 = require("../application.service");
const extension_description_1 = require("./../../integration/extension-description");
const transient_entity_const_1 = require("./transient-entity.const");
function onlineDescription(description) {
    if (lodash_1.isString(description)) {
        return description;
    }
    if (lodash_1.isFunction(description)) {
        return onlineDescription(description());
    }
    return onlineDescription(description.description);
}
exports.onlineDescription = onlineDescription;
let TransientEntityService = TransientEntityService_1 = class TransientEntityService {
    constructor(appCtx) {
        this.appCtx = appCtx;
        this.transientEntities = {};
    }
    registerTransientEntityService(resource, transientEntity) {
        var _a;
        if (!this.transientEntities[resource]) {
            this.transientEntities[resource] = [];
        }
        (_a = this.transientEntities[resource]) === null || _a === void 0 ? void 0 : _a.push(transientEntity);
    }
    getPostTreatmentDescription() {
        const treatments = [];
        for (const resource in this.transientEntities) {
            for (const transientEntity of this.transientEntities[resource]) {
                if (lodash_1.isString(transientEntity.description)) {
                    treatments.push(new extension_description_1.TreatmentDescription(transientEntity.providerId, transientEntity.serviceId, resource + ': ' + transientEntity.description));
                }
                else if (lodash_1.isFunction(transientEntity.description)) {
                    treatments.push(transientEntity.description());
                }
                else {
                    treatments.push(transientEntity.description);
                }
            }
        }
        return treatments;
    }
    registerTransientEntityTypeFunction(functionProviderId, functionServiceId, functionDescription, resource, objectType, transientFunction, methods = []) {
        this.registerTransientEntityService(resource, new (class {
            constructor() {
                this.providerId = functionProviderId;
                this.serviceId = functionServiceId;
                this.description = new extension_description_1.TreatmentDescription(transient_entity_const_1.TRANSIENT_ENTITY_PROVIDER, TransientEntityService_1.name, resource + '.' + objectType + ': ' + functionDescription);
                if (0 < methods.length) {
                    this.description.description =
                        resource +
                            '.' +
                            objectType +
                            '.' +
                            methods.join('|') +
                            ': ' +
                            functionDescription;
                }
            }
            async completeReturnedEntity(entity, ctx) {
                var _a, _b, _c, _d;
                if (0 < methods.length) {
                    if (-1 === lodash_1.indexOf(methods, (_c = (_b = (_a = ctx.uriContext) === null || _a === void 0 ? void 0 : _a.uri) === null || _b === void 0 ? void 0 : _b.value) === null || _c === void 0 ? void 0 : _c.method)) {
                        return;
                    }
                }
                if (-1 < lodash_1.indexOf((_d = entity.entityCtx) === null || _d === void 0 ? void 0 : _d.implementedTypes, objectType)) {
                    return transientFunction(entity, ctx);
                }
            }
        })());
    }
    get ready() {
        return this.appCtx.getExtensionContext(transient_entity_const_1.TRANSIENT_ENTITY_PROVIDER).ready;
    }
    async completeReturnedEntity(entityName, entity, ctx) {
        if (entityName in this.transientEntities) {
            for (const transientEntity of this.transientEntities[entityName]) {
                await transientEntity.completeReturnedEntity(entity, ctx);
            }
        }
        if (!entity.entityCtx) {
            entity.entityCtx = { entityType: entityName };
        }
        entity.entityCtx.loaded = true;
    }
    async completeReturnedEntities(entityName, entities, ctx) {
        for (let childIndex = entities.length - 1; childIndex >= 0; childIndex--) {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const entity = entities[childIndex];
            await this.completeReturnedEntity(entityName, entity, ctx);
        }
    }
};
TransientEntityService = TransientEntityService_1 = tslib_1.__decorate([
    core_1.injectable({ scope: core_1.BindingScope.SINGLETON }),
    tslib_1.__param(0, core_1.service(application_service_1.ApplicationService)),
    tslib_1.__metadata("design:paramtypes", [application_service_1.ApplicationService])
], TransientEntityService);
exports.TransientEntityService = TransientEntityService;
//# sourceMappingURL=transient-entity.service.js.map