"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "CameraStreamService", {
    enumerable: true,
    get: function() {
        return CameraStreamService;
    }
});
const _models = require("../../models");
const _validators = require("../../handlers/validators");
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
const createCameraStreamRules = {
    printerId: "mongoId",
    streamURL: "required|httpurl",
    name: "required|string"
};
class CameraStreamService {
    model = _models.CameraStream;
    printerCache;
    constructor({ printerCache }){
        this.printerCache = printerCache;
    }
    async list() {
        return this.model.find();
    }
    async get(id, throwError = true) {
        const cameraStream = await this.model.findById(id);
        if (!cameraStream && throwError) {
            throw new _runtimeexceptions.NotFoundException(`Floor with provided id does not exist`, "CameraStream");
        }
        return cameraStream;
    }
    async create(data) {
        const input = await (0, _validators.validateInput)(data, createCameraStreamRules);
        if (input.printerId) {
            await this.printerCache.getCachedPrinterOrThrow(input.printerId);
        }
        return this.model.create(input);
    }
    async delete(id) {
        await this.model.findByIdAndDelete(id);
    }
    async update(id, input) {
        await this.get(id);
        const updateInput = await (0, _validators.validateInput)(input, createCameraStreamRules);
        if (input.printerId) {
            await this.printerCache.getCachedPrinterOrThrow(input.printerId);
        }
        await this.model.updateOne({
            id
        }, updateInput);
        return this.get(id);
    }
    toDto(entity) {
        return {
            id: entity.id,
            streamURL: entity.streamURL,
            printerId: entity.printerId === null ? null : entity.printerId?.toString(),
            name: entity.name
        };
    }
}

//# sourceMappingURL=camera-stream.service.js.map