"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentExtensionService = exports.ContentExtensionRepository = exports.ContentExtension = void 0;
const tslib_1 = require("tslib");
const repository_1 = require("@loopback/repository");
const models_1 = require("../models");
const object_node_model_1 = require("../models/object-node.model");
let ContentExtension = class ContentExtension extends models_1.ContentEntity {
    constructor(data) {
        super(data);
    }
};
tslib_1.__decorate([
    repository_1.property({
        type: 'object',
    }),
    tslib_1.__metadata("design:type", Object)
], ContentExtension.prototype, "content", void 0);
tslib_1.__decorate([
    repository_1.belongsTo(() => object_node_model_1.ObjectNode),
    tslib_1.__metadata("design:type", String)
], ContentExtension.prototype, "objectNodeId", void 0);
ContentExtension = tslib_1.__decorate([
    repository_1.model({ settings: { strict: false } }),
    tslib_1.__metadata("design:paramtypes", [Object])
], ContentExtension);
exports.ContentExtension = ContentExtension;
class ContentExtensionRepository extends repository_1.DefaultCrudRepository {
    constructor(entityClass, dataSource, objectNodeRepositoryGetter) {
        super(entityClass, dataSource);
        this.objectNodeRepositoryGetter = objectNodeRepositoryGetter;
        this.objectNode = this.createBelongsToAccessorFor('objectNode', objectNodeRepositoryGetter);
        this.registerInclusionResolver('objectNode', this.objectNode.inclusionResolver);
    }
}
exports.ContentExtensionRepository = ContentExtensionRepository;
class ContentExtensionService {
    constructor(providerId, serviceId, contentEntityService, contentExtensionRepository, contentTypeName) {
        this.providerId = providerId;
        this.serviceId = serviceId;
        this.contentEntityService = contentEntityService;
        this.contentExtensionRepository = contentExtensionRepository;
        this.contentTypeName = contentTypeName;
        this.contentEntityService.registerNewContentType(contentTypeName, this);
    }
    get fieldName() {
        return (this.contentTypeName.charAt(0).toLowerCase() +
            this.contentTypeName.slice(1));
    }
    async deleteContents(entities, fieldName = this.fieldName) {
        const contentIdsToDelete = entities
            .filter((entity) => {
            return entity[fieldName + 'Id'];
        })
            .map((entity) => entity[fieldName + 'Id']);
        await this.contentExtensionRepository.deleteAll({
            id: { inq: contentIdsToDelete },
        });
    }
    async manageContent(entity, postedEntity, fieldName = this.fieldName) {
        const postedExtensionId = entity[fieldName + 'Id'];
        const postedExtension = postedEntity[fieldName];
        if (undefined === postedExtension) {
            return false;
        }
        if (null === postedExtension || '' === postedExtension) {
            if (postedExtensionId) {
                await this.contentExtensionRepository.deleteById(postedExtensionId);
                entity[fieldName + 'Id'] = null;
                return true;
            }
            return false;
        }
        else {
            if (postedExtensionId) {
                await this.contentExtensionRepository.updateById(postedExtensionId, {
                    content: postedExtension,
                });
                return false;
            }
            else {
                const newExtension = await this.contentExtensionRepository.create({
                    content: postedExtension,
                    objectNodeId: entity.id,
                });
                entity[fieldName + 'Id'] = newExtension.id;
                return true;
            }
        }
    }
    async getContent(entity, fieldName = this.fieldName, args) {
        const contentExenstion = await this.contentExtensionRepository.findById(args.contentId);
        return contentExenstion.content;
    }
    async addTransientContent(entity, fieldName = this.fieldName) {
        if (entity[fieldName + 'Id']) {
            entity[fieldName] = await this.getContent(entity, fieldName, {
                contentId: entity[fieldName + 'Id'],
            });
        }
    }
}
exports.ContentExtensionService = ContentExtensionService;
//# sourceMappingURL=content-extension.definition.js.map