"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrinterTagService", {
    enumerable: true,
    get: function() {
        return PrinterTagService;
    }
});
const _baseservice = require("./base.service");
const _printertagentity = require("../../entities/printer-tag.entity");
const _printertagdto = require("../interfaces/printer-tag.dto");
const _tagentity = require("../../entities/tag.entity");
const _classvalidator = require("class-validator");
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
class PrinterTagService extends (0, _baseservice.BaseService)(_printertagentity.PrinterTag, _printertagdto.PrinterTagDto) {
    tagRepository;
    constructor(typeormService){
        super(typeormService);
        this.tagRepository = typeormService.getDataSource().getRepository(_tagentity.Tag);
    }
    async listTags() {
        const tags = await this.tagRepository.find();
        const tagRecords = {};
        for (const tag of tags){
            tagRecords[tag.id] = {
                id: tag.id,
                name: tag.name,
                color: tag.color,
                printers: []
            };
        }
        for (const tag of tags){
            tagRecords[tag.id].printers = await this.repository.findBy({
                tagId: tag.id
            });
        }
        return Object.values(tagRecords);
    }
    async getTag(tagId) {
        const tag = await this.tagRepository.findOneBy({
            id: tagId
        });
        if (!tag) {
            throw new _runtimeexceptions.NotFoundException("Tag does not exist");
        }
        return tag;
    }
    async getPrintersByTag(tagId) {
        const tag = await this.getTag(tagId);
        const printerTags = await this.repository.findBy({
            tagId: tag.id
        });
        return {
            id: tag.id,
            name: tag.name,
            color: tag.color,
            printers: printerTags
        };
    }
    async createTag(dto) {
        await (0, _classvalidator.validate)(dto);
        const entity = this.tagRepository.create(dto);
        await (0, _classvalidator.validate)(entity);
        const tag = await this.tagRepository.save(entity);
        return await this.getPrintersByTag(tag.id);
    }
    async updateTagName(tagId, name) {
        const entity = await this.getTag(tagId);
        const updateDto = {
            name
        };
        await (0, _classvalidator.validate)(updateDto);
        await (0, _classvalidator.validate)(Object.assign(entity, updateDto));
        await this.tagRepository.update(entity.id, updateDto);
    }
    async updateTagColor(tagId, color) {
        const entity = await this.getTag(tagId);
        const updateDto = {
            color
        };
        await (0, _classvalidator.validate)(updateDto);
        await (0, _classvalidator.validate)(Object.assign(entity, updateDto));
        await this.tagRepository.update(entity.id, updateDto);
    }
    async deleteTag(tagId) {
        const tag = await this.getTag(tagId);
        await this.tagRepository.delete({
            id: tag.id
        });
    }
    async addPrinterToTag(tagId, printerId) {
        const tag = await this.getTag(tagId);
        const alreadyExisting = await this.repository.findOneBy({
            tagId: tag.id,
            printerId
        });
        if (alreadyExisting) return alreadyExisting;
        return await this.create({
            tagId: tag.id,
            printerId
        });
    }
    async removePrinterFromTag(tagId, printerId) {
        await this.getTag(tagId);
        await this.repository.delete({
            tagId,
            printerId
        });
    }
    toDto(entity) {
        return {
            printerId: entity.printerId,
            tagId: entity.tagId
        };
    }
}

//# sourceMappingURL=printer-tag.service.js.map