"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentEntityService = void 0;
const application_error_1 = require("../../helper/application-error");
const extension_description_1 = require("./../../integration/extension-description");
class ContentEntityService {
    constructor( /* Add @inject to inject parameters */) {
        this.contentTypeDefinitions = {};
    }
    async getContentDefinition(contentType) {
        if (this.hasContentManager(contentType)) {
            return this.contentTypeDefinitions[contentType].getContentDefinition();
        }
        return { properties: {} };
    }
    hasContentManager(contentType) {
        return contentType && contentType in this.contentTypeDefinitions;
    }
    getPostTreatmentDescription() {
        const treatment = [];
        for (const contentType of Object.keys(this.contentTypeDefinitions)) {
            treatment.push(new extension_description_1.TreatmentDescription(this.contentTypeDefinitions[contentType].providerId, this.contentTypeDefinitions[contentType].serviceId, contentType + ': Add transient fields'));
        }
        return treatment;
    }
    registerNewContentType(contentType, service) {
        this.contentTypeDefinitions[contentType] = service;
    }
    get contentTypes() {
        return Object.keys(this.contentTypeDefinitions);
    }
    manageContent(contentType, entity, postedEntity) {
        if (contentType && this.hasContentManager(contentType)) {
            return this.contentTypeDefinitions[contentType].manageContent(entity, postedEntity);
        }
        return Promise.resolve(false);
    }
    async deleteContents(contentType, entities) {
        if (contentType && this.hasContentManager(contentType)) {
            return this.contentTypeDefinitions[contentType].deleteContents(entities);
        }
    }
    addTransientContent(entityType, contentType, entity) {
        if (!entity.entityCtx) {
            entity.entityCtx = { entityType: entityType };
        }
        if (contentType && this.hasContentManager(contentType)) {
            return this.contentTypeDefinitions[contentType].addTransientContent(entity);
        }
        return Promise.resolve();
    }
    getContent(contentType, entity, fieldName, args) {
        if (contentType && this.hasContentManager(contentType)) {
            return this.contentTypeDefinitions[contentType].getContent(entity, fieldName, args);
        }
        throw application_error_1.ApplicationError.notFound({ contentType: contentType });
    }
}
exports.ContentEntityService = ContentEntityService;
//# sourceMappingURL=content-entity.service.js.map