"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InterceptImageService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const __1 = require("../..");
const node_intercept_service_1 = require("../../services/entity-intercept/node-intercept.service");
const content_image_const_1 = require("./content-image.const");
const transient_image_service_1 = require("./transient-image.service");
let InterceptImageService = class InterceptImageService {
    constructor(transientImageService, nodeInterceptService) {
        this.transientImageService = transientImageService;
        this.nodeInterceptService = nodeInterceptService;
        this.nodeInterceptService.registerEntityInterceptorService(content_image_const_1.CONTENT_IMAGE_PROVIDER, InterceptImageService.name, 'Set position and date from Exif image data and Reduce big image size', content_image_const_1.IMAGE_TYPE.name, __1.EntityActionType.create, this.interceptImageCreate.bind(this));
        this.nodeInterceptService.registerEntityInterceptorService(content_image_const_1.CONTENT_IMAGE_PROVIDER, InterceptImageService.name, 'Reduce big image size', content_image_const_1.IMAGE_TYPE.name, __1.EntityActionType.update, this.interceptImageUpdate.bind(this));
    }
    async interceptImageCreate(entityId, image, requestEntity, ctx) {
        try {
            if (!requestEntity.imagePosition || !requestEntity.imageDate) {
                const exif = (await this.transientImageService.getImageExif(requestEntity));
                if (!requestEntity.imagePosition) {
                    const position = this.transientImageService.getPositionFromExif(exif);
                    if (position) {
                        requestEntity.imagePosition = position;
                    }
                }
                if (!requestEntity.imageDate) {
                    const date = this.transientImageService.getImageDateFromExif(exif);
                    if (date) {
                        requestEntity.imageDate = date;
                    }
                }
            }
        }
        catch (error) { }
        return this.checkAndreduceImageEntitySize(requestEntity);
    }
    async interceptImageUpdate(entityId, entity, requestEntity, ctx) {
        return this.checkAndreduceImageEntitySize(requestEntity);
    }
    async checkAndreduceImageEntitySize(image) {
        if (image && image.contentImage) {
            return await this.checkAndReduceImageSize(image.contentImage);
        }
        return true;
    }
    async checkAndReduceImageSize(image) {
        try {
            if (image && image.base64) {
                const imageSizeCheck = await this.transientImageService.changeImgSize(image, 4000);
                if (imageSizeCheck.size !== image.size) {
                    image.base64 = imageSizeCheck.base64;
                    image.size = imageSizeCheck.size;
                }
            }
        }
        catch (error) { }
        return true;
    }
};
InterceptImageService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(transient_image_service_1.TransientImageService)),
    tslib_1.__param(1, core_1.service(node_intercept_service_1.NodeInterceptService)),
    tslib_1.__metadata("design:paramtypes", [transient_image_service_1.TransientImageService,
        node_intercept_service_1.NodeInterceptService])
], InterceptImageService);
exports.InterceptImageService = InterceptImageService;
//# sourceMappingURL=intercept-image.service.js.map