"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReserveService = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const decimal_js_1 = __importDefault(require("decimal.js"));
const bbcDates_db_1 = require("../db/bbcDates.db");
const reserve_types_enum_1 = require("../enums/reserve-types.enum");
const reserve_db_1 = require("../db/reserve.db");
class ReserveService {
    signsService;
    constructor(signsService) {
        this.signsService = signsService;
    }
    async getReserve(bbcDateId) {
        const bbcDateIdID = new mongoose_1.default.Types.ObjectId(bbcDateId);
        const reserves = await (0, reserve_db_1.getReserveDocs)([bbcDateIdID]);
        if (!!reserves.length) {
            return reserves.pop();
        }
        const lastItems = await this.getLastCopyReserve(bbcDateId);
        const isBBCLockedResult = await this.signsService.isBBCLocked(bbcDateId);
        if (isBBCLockedResult) {
            const summary = this.calculateSummary(lastItems);
            return await (0, reserve_db_1.copyReserveWithoutSaving)(bbcDateIdID, lastItems, summary);
        }
        return this.saveAndReturnReserve(bbcDateId, { items: lastItems, notes: '' });
    }
    async saveAndReturnReserve(bbcDateId, reserveUpdate) {
        const isBBCLockedResult = await this.signsService.isBBCLocked(bbcDateId);
        if (isBBCLockedResult) {
            return null;
        }
        const summary = this.calculateSummary(reserveUpdate.items);
        return await (0, reserve_db_1.saveReserveDoc)(new mongoose_1.default.Types.ObjectId(bbcDateId), { ...reserveUpdate, summary });
    }
    ;
    async getLastCopyReserve(bbcDateId) {
        const { borrowerId } = await (0, bbcDates_db_1.getBBCDateDoc)(bbcDateId.toString());
        const equipment = await (0, reserve_db_1.getLastReserveDocs)(bbcDateId, borrowerId.toString());
        if (!!equipment.length) {
            return equipment.pop().items;
        }
        return [];
    }
    calculateSummary(reserveItems) {
        const summary = [];
        const summaryOrder = [
            reserve_types_enum_1.EReserveTypes.RENT,
            reserve_types_enum_1.EReserveTypes.PAYROLL,
            reserve_types_enum_1.EReserveTypes.INSURANCE,
            reserve_types_enum_1.EReserveTypes.TAX,
            reserve_types_enum_1.EReserveTypes.INVENTORY,
            reserve_types_enum_1.EReserveTypes.RECEIVABLES,
            reserve_types_enum_1.EReserveTypes.EQUIPMENT,
            reserve_types_enum_1.EReserveTypes.OTHER,
        ];
        const result = Object.keys(reserve_types_enum_1.EReserveTypes).map((equipmentType) => {
            const typeSummary = { rowDef: equipmentType, amount: 0 };
            reserveItems.forEach((item) => {
                if (item.type === equipmentType) {
                    typeSummary.amount = new decimal_js_1.default(typeSummary.amount).add(item.amount ?? 0).toNumber();
                }
                return typeSummary;
            });
            return typeSummary;
        });
        summaryOrder.forEach((summaryType) => {
            summary.push(...result.filter((r) => r.rowDef === summaryType));
        });
        return summary.reduce((acc, s) => ({ ...acc, [s.rowDef]: s }), {});
    }
}
exports.ReserveService = ReserveService;
