"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MediaStorageProviderMetadataService = void 0;
const common_1 = require("@nestjs/common");
const error_messages_1 = require("../constants/error-messages");
const media_storage_provider_metadata_repository_1 = require("../repository/media-storage-provider-metadata.repository");
const crud_helper_service_1 = require("./crud-helper.service");
let MediaStorageProviderMetadataService = class MediaStorageProviderMetadataService {
    constructor(mediaStorageProviderRepo, crudHelperService) {
        this.mediaStorageProviderRepo = mediaStorageProviderRepo;
        this.crudHelperService = crudHelperService;
    }
    async find(basicFilterDto) {
        return this.findMany(basicFilterDto);
    }
    async findMany(basicFilterDto) {
        const alias = 'mediaStorageProviderMetadata';
        let { limit, offset, populateMedia } = basicFilterDto;
        var qb = await this.mediaStorageProviderRepo.createSecurityRuleAwareQueryBuilder(alias);
        qb = this.crudHelperService.buildFilterQuery(qb, basicFilterDto, alias);
        const [entities, count] = await qb.getManyAndCount();
        const currentPage = Math.floor(offset / limit) + 1;
        const totalPages = Math.ceil(count / limit);
        const nextPage = currentPage < totalPages ? currentPage + 1 : null;
        const prevPage = currentPage > 1 ? currentPage - 1 : null;
        const r = {
            meta: {
                totalRecords: count,
                currentPage: currentPage,
                nextPage: nextPage,
                prevPage: prevPage,
                totalPages: totalPages,
                perPage: +limit,
            },
            records: entities
        };
        return r;
    }
    async findOne(id, relations = {}) {
        const lov = await this.mediaStorageProviderRepo.findOne({
            where: {
                id: id,
            },
            relations: relations,
        });
        if (!lov) {
            throw new common_1.NotFoundException(`Media Storage Provider with #${id} not found`);
        }
        return lov;
    }
    async findOneByUserKey(name, relations = {}) {
        const lov = await this.mediaStorageProviderRepo.findOne({
            where: {
                name: name,
            },
            relations: relations,
        });
        return lov;
    }
    async findOnebyType(type, relations = {}) {
        const lov = await this.mediaStorageProviderRepo.findOne({
            where: {
                type: type,
            },
            relations: relations,
        });
        if (!lov) {
            throw new common_1.NotFoundException(error_messages_1.ERROR_MESSAGES.MEDIA_STORAGE_PROVIDER_ID_NOT_FOUND(type));
        }
        return lov;
    }
    async create(createDto) {
        const lov = this.mediaStorageProviderRepo.create(createDto);
        return this.mediaStorageProviderRepo.save(lov);
    }
    async upsert(updateMediaStorageProviderDto) {
        const existingMediaStorageProvider = await this.mediaStorageProviderRepo.findOne({
            where: {
                name: updateMediaStorageProviderDto.name
            }
        });
        if (existingMediaStorageProvider) {
            const updatedMediaStorageProviderDto = { ...existingMediaStorageProvider, ...updateMediaStorageProviderDto };
            return this.mediaStorageProviderRepo.save(updatedMediaStorageProviderDto);
        }
        else {
            const moduleMetadata = this.mediaStorageProviderRepo.create(updateMediaStorageProviderDto);
            return this.mediaStorageProviderRepo.save(moduleMetadata);
        }
    }
    async update(id, updateMediaStorageProviderMetadataDto) {
        const entity = await this.mediaStorageProviderRepo.preload({
            id,
            ...updateMediaStorageProviderMetadataDto,
        });
        if (!entity) {
            throw new common_1.NotFoundException(error_messages_1.ERROR_MESSAGES.MODULE_ID_NOT_FOUND(id));
        }
        return this.mediaStorageProviderRepo.save(entity);
    }
    async deleteMany(ids) {
        if (!ids || ids.length === 0) {
            throw new Error(error_messages_1.ERROR_MESSAGES.DELETE_IDS_REQUIRED);
        }
        const removedEntities = [];
        for (let i = 0; i < ids.length; i++) {
            const id = ids[i];
            const entity = await this.mediaStorageProviderRepo.findOne({
                where: {
                    id: id,
                }
            });
            removedEntities.push(await this.mediaStorageProviderRepo.remove(entity));
        }
        return removedEntities;
    }
    async delete(id) {
        const lov = await this.findOne(id);
        return this.mediaStorageProviderRepo.remove(lov);
    }
};
exports.MediaStorageProviderMetadataService = MediaStorageProviderMetadataService;
exports.MediaStorageProviderMetadataService = MediaStorageProviderMetadataService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [media_storage_provider_metadata_repository_1.MediaStorageProviderMetadataRepository,
        crud_helper_service_1.CrudHelperService])
], MediaStorageProviderMetadataService);
//# sourceMappingURL=media-storage-provider-metadata.service.js.map