"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteActorImages = exports.updateActorImages = void 0;
const tslib_1 = require("tslib");
const logger_1 = require("../../../helpers/logger");
const actor_image_1 = require("../../../models/actor/actor-image");
function updateActorImages(actor, type, imagesInfo, t) {
    var _a;
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const getAvatarsOrBanners = () => {
            const result = type === 1
                ? actor.Avatars
                : actor.Banners;
            return result || [];
        };
        if (imagesInfo.length === 0) {
            yield deleteActorImages(actor, type, t);
        }
        for (const oldImageModel of getAvatarsOrBanners()) {
            if (oldImageModel.width)
                continue;
            yield safeDeleteActorImage(actor, oldImageModel, type, t);
        }
        for (const imageInfo of imagesInfo) {
            const oldImageModel = getAvatarsOrBanners().find(i => imageInfo.width && i.width === imageInfo.width);
            if (oldImageModel) {
                if (imageInfo.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) {
                    continue;
                }
                yield safeDeleteActorImage(actor, oldImageModel, type, t);
            }
            const imageModel = yield actor_image_1.ActorImageModel.create({
                filename: imageInfo.name,
                onDisk: (_a = imageInfo.onDisk) !== null && _a !== void 0 ? _a : false,
                fileUrl: imageInfo.fileUrl,
                height: imageInfo.height,
                width: imageInfo.width,
                type,
                actorId: actor.id
            }, { transaction: t });
            addActorImage(actor, type, imageModel);
        }
        return actor;
    });
}
exports.updateActorImages = updateActorImages;
function deleteActorImages(actor, type, t) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        try {
            const association = buildAssociationName(type);
            for (const image of actor[association]) {
                yield image.destroy({ transaction: t });
            }
            actor[association] = [];
        }
        catch (err) {
            logger_1.logger.error('Cannot remove old image of actor %s.', actor.url, { err });
        }
        return actor;
    });
}
exports.deleteActorImages = deleteActorImages;
function safeDeleteActorImage(actor, toDelete, type, t) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        try {
            yield toDelete.destroy({ transaction: t });
            const association = buildAssociationName(type);
            actor[association] = actor[association].filter(image => image.id !== toDelete.id);
        }
        catch (err) {
            logger_1.logger.error('Cannot remove old actor image of actor %s.', actor.url, { err });
        }
    });
}
function addActorImage(actor, type, imageModel) {
    const association = buildAssociationName(type);
    if (!actor[association])
        actor[association] = [];
    actor[association].push(imageModel);
}
function buildAssociationName(type) {
    return type === 1
        ? 'Avatars'
        : 'Banners';
}
//# sourceMappingURL=image.js.map