"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentUserService = void 0;
const tslib_1 = require("tslib");
const authentication_jwt_1 = require("@loopback/authentication-jwt");
/* eslint-disable @typescript-eslint/no-explicit-any */
const core_1 = require("@loopback/core");
const repository_1 = require("@loopback/repository");
const application_service_1 = require("./../application.service");
const content_entity_const_1 = require("./content-entity.const");
const content_entity_service_1 = require("./content-entity.service");
const FIELD_NAME = 'contentUser';
let ContentUserService = class ContentUserService {
    constructor(contentEntityService, userRepository) {
        this.contentEntityService = contentEntityService;
        this.userRepository = userRepository;
        this.providerId = content_entity_const_1.CONTENT_ENTITY_PROVIDER;
        this.serviceId = ContentUserService.name;
        this.contentEntityService.registerNewContentType(application_service_1.ApplicationService.CONTENT_TYPE.USER, this);
    }
    async deleteContents(entities) { }
    async getContentDefinition() {
        return {
            properties: {
                contentUser: {
                    type: 'string',
                    title: 'User email',
                },
            },
        };
    }
    async manageContent(entity, postedEntity, fieldName = FIELD_NAME) {
        const postedUserId = entity[fieldName + 'Id'];
        const postedUser = postedEntity[fieldName];
        if (undefined === postedUser) {
            return false;
        }
        if (null === postedUser || '' === postedUser) {
            if (postedUserId) {
                entity[fieldName + 'Id'] = null;
                return true;
            }
            return false;
        }
        else {
            let user = await this.userRepository.findOne({
                where: { email: postedUser },
            });
            if (!user) {
                user = await this.userRepository.create({ email: postedUser });
            }
            entity[fieldName + 'Id'] = user.id;
            return true;
        }
    }
    async getContent(entity, fieldName = FIELD_NAME, args) {
        const user = await this.userRepository.findById(args.contentId);
        return user.email;
    }
    async addTransientContent(entity, fieldName = FIELD_NAME) {
        if (entity[fieldName + 'Id']) {
            entity[fieldName] = await this.getContent(entity, fieldName, {
                contentId: entity[fieldName + 'Id'],
            });
        }
    }
};
ContentUserService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(content_entity_service_1.ContentEntityService)),
    tslib_1.__param(1, repository_1.repository(authentication_jwt_1.UserRepository)),
    tslib_1.__metadata("design:paramtypes", [content_entity_service_1.ContentEntityService,
        authentication_jwt_1.UserRepository])
], ContentUserService);
exports.ContentUserService = ContentUserService;
//# sourceMappingURL=content-user.service.js.map