"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentEncryptedObjectService = exports.ContentEncryptedObjectRepository = exports.ContentEncryptedObject = exports.EncryptedObject = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const repository_1 = require("@loopback/repository");
const lodash_1 = require("lodash");
const simple_crypto_js_1 = tslib_1.__importDefault(require("simple-crypto-js"));
const constants_1 = require("../../constants");
const db_datasource_1 = require("../../datasources/db.datasource");
const content_extension_definition_1 = require("../../integration/content-extension.definition");
const content_entity_service_1 = require("../../services/content-entity/content-entity.service");
const application_service_1 = require("./../../services/application.service");
const content_encrypted_object_const_1 = require("./content-encrypted-object.const");
let EncryptedObject = class EncryptedObject extends repository_1.Entity {
};
tslib_1.__decorate([
    repository_1.property({
        required: false,
    }),
    tslib_1.__metadata("design:type", Object)
], EncryptedObject.prototype, "value", void 0);
EncryptedObject = tslib_1.__decorate([
    repository_1.model()
], EncryptedObject);
exports.EncryptedObject = EncryptedObject;
let ContentEncryptedObject = class ContentEncryptedObject extends content_extension_definition_1.ContentExtension {
};
ContentEncryptedObject = tslib_1.__decorate([
    repository_1.model({ settings: { strict: false } })
], ContentEncryptedObject);
exports.ContentEncryptedObject = ContentEncryptedObject;
let ContentEncryptedObjectRepository = class ContentEncryptedObjectRepository extends content_extension_definition_1.ContentExtensionRepository {
    constructor(dataSource, objectNodeRepositoryGetter, appCtx) {
        super(ContentEncryptedObject, dataSource, objectNodeRepositoryGetter);
        this.objectNodeRepositoryGetter = objectNodeRepositoryGetter;
        this.appCtx = appCtx;
        this.simpleCrypto = new simple_crypto_js_1.default(appCtx.config.serverSecretKey);
    }
    async create(entity, options) {
        return this.decrypt(await super.create(this.encrypt(entity), options));
    }
    encrypt(content) {
        var _a;
        const result = lodash_1.clone(content);
        if ((_a = result.content) === null || _a === void 0 ? void 0 : _a.value) {
            result.content.value = this.simpleCrypto.encrypt(result.content.value);
        }
        return result;
    }
    decrypt(content) {
        const result = lodash_1.clone(content);
        result.content.value = this.simpleCrypto.decrypt(result.content.value);
        return result;
    }
    async updateById(id, data, options) {
        return super.updateById(id, this.encrypt(data), options);
    }
    async findById(id, filter, options) {
        return this.decrypt(await super.findById(id, filter, options));
    }
};
ContentEncryptedObjectRepository = tslib_1.__decorate([
    tslib_1.__param(0, core_1.inject(constants_1.DATASTORE_DB)),
    tslib_1.__param(1, repository_1.repository.getter('ObjectNodeRepository')),
    tslib_1.__param(2, core_1.service(application_service_1.ApplicationService)),
    tslib_1.__metadata("design:paramtypes", [db_datasource_1.DbDataSource, Function, application_service_1.ApplicationService])
], ContentEncryptedObjectRepository);
exports.ContentEncryptedObjectRepository = ContentEncryptedObjectRepository;
let ContentEncryptedObjectService = class ContentEncryptedObjectService extends content_extension_definition_1.ContentExtensionService {
    constructor(contentEntityService, contentExtensionRepository) {
        super(content_encrypted_object_const_1.CONTENT_ENCRYPTED_OBJECT_PROVIDER, ContentEncryptedObjectService.name, contentEntityService, contentExtensionRepository, content_encrypted_object_const_1.CONTENT_ENCRYPTED_OBJECT);
        this.contentEntityService = contentEntityService;
        this.contentExtensionRepository = contentExtensionRepository;
    }
    async getContentDefinition() {
        return {
            properties: {
                contentEncryptedObject: {
                    type: 'object',
                    title: 'Encrypted object',
                    properties: {
                        value: {
                            type: 'object',
                            title: 'Encrypted value',
                            default: '',
                            'x-schema-form': {
                                type: 'json',
                            },
                        },
                    },
                },
            },
        };
    }
};
ContentEncryptedObjectService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(content_entity_service_1.ContentEntityService)),
    tslib_1.__param(1, repository_1.repository(ContentEncryptedObjectRepository)),
    tslib_1.__metadata("design:paramtypes", [content_entity_service_1.ContentEntityService,
        ContentEncryptedObjectRepository])
], ContentEncryptedObjectService);
exports.ContentEncryptedObjectService = ContentEncryptedObjectService;
//# sourceMappingURL=content-encrypted-object.definition.js.map