"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentTextService = void 0;
const tslib_1 = require("tslib");
/* eslint-disable @typescript-eslint/no-explicit-any */
const core_1 = require("@loopback/core");
const repository_1 = require("@loopback/repository");
const content_text_repository_1 = require("./../../repositories/content-text.repository");
const content_entity_const_1 = require("./content-entity.const");
const content_entity_service_1 = require("./content-entity.service");
let ContentTextService = class ContentTextService {
    constructor(contentEntityService, contentTextRepository) {
        this.contentEntityService = contentEntityService;
        this.contentTextRepository = contentTextRepository;
        this.providerId = content_entity_const_1.CONTENT_ENTITY_PROVIDER;
        this.serviceId = ContentTextService.name;
        this.contentEntityService.registerNewContentType('ContentText', this);
    }
    async getContentDefinition() {
        return {
            properties: {
                contentText: {
                    type: 'string',
                    'x-schema-form': {
                        type: 'textarea',
                    },
                },
            },
        };
    }
    async deleteContents(entities, fieldName = 'contentText') {
        const contentIdsToDelete = entities
            .filter((entity) => {
            return entity[fieldName + 'Id'];
        })
            .map((entity) => entity[fieldName + 'Id']);
        await this.contentTextRepository.deleteAll({ id: { inq: contentIdsToDelete } });
    }
    async manageContent(entity, postedEntity, fieldName = 'contentText') {
        const postedTextId = entity[fieldName + 'Id'];
        const postedText = postedEntity[fieldName];
        if (undefined === postedText) {
            return false;
        }
        if (null === postedText || '' === postedText) {
            if (postedTextId) {
                await this.contentTextRepository.deleteById(postedTextId);
                entity[fieldName + 'Id'] = null;
                return true;
            }
            return false;
        }
        else {
            if (postedTextId) {
                await this.contentTextRepository.updateById(postedTextId, {
                    text: postedText,
                });
                return false;
            }
            else {
                const newText = await this.contentTextRepository.create({
                    text: postedText,
                    objectNodeId: entity.id,
                });
                entity[fieldName + 'Id'] = newText.id;
                return true;
            }
        }
    }
    async getContent(entity, fieldName = 'contentText', args) {
        const text = await this.contentTextRepository.findById(args.contentId);
        return text.text;
    }
    async addTransientContent(entity, fieldName = 'contentText') {
        if (entity[fieldName + 'Id']) {
            entity[fieldName] = await this.getContent(entity, fieldName, {
                contentId: entity[fieldName + 'Id'],
            });
        }
    }
};
ContentTextService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(content_entity_service_1.ContentEntityService)),
    tslib_1.__param(1, repository_1.repository(content_text_repository_1.ContentTextRepository)),
    tslib_1.__metadata("design:paramtypes", [content_entity_service_1.ContentEntityService,
        content_text_repository_1.ContentTextRepository])
], ContentTextService);
exports.ContentTextService = ContentTextService;
//# sourceMappingURL=content-text.service.js.map