"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollateralAdjustmentsService = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const decimal_js_1 = __importDefault(require("decimal.js"));
const collaterals_enum_1 = require("../enums/collaterals.enum");
const bbcDates_db_1 = require("../db/bbcDates.db");
const collateral_adjustments_db_1 = require("../db/collateral-adjustments.db");
class CollateralAdjustmentsService {
    signsService;
    constructor(signsService) {
        this.signsService = signsService;
    }
    async getCollateralAdjustmentSummary(bbcDateId) {
        const bbcDateIdID = new mongoose_1.default.Types.ObjectId(bbcDateId);
        const collateralAdjustments = await (0, collateral_adjustments_db_1.getCollateralAdjustmentDocs)([bbcDateIdID]);
        if (!collateralAdjustments.length) {
            return {};
        }
        return collateralAdjustments[0].summary;
    }
    async getCollateralAdjustment(bbcDateId) {
        const bbcDateIdID = new mongoose_1.default.Types.ObjectId(bbcDateId);
        const cCollateralAdjustments = await (0, collateral_adjustments_db_1.getCollateralAdjustmentDocs)([bbcDateIdID]);
        if (!!cCollateralAdjustments.length) {
            return cCollateralAdjustments.pop();
        }
        const lastItems = await this.getLastCopyCollateralAdjustmentItems(bbcDateId);
        const isBBCLockedResult = await this.signsService.isBBCLocked(bbcDateId);
        if (isBBCLockedResult) {
            const summary = this.calculateSummary(lastItems);
            return await (0, collateral_adjustments_db_1.copyCollateralAdjustmentWithoutSaving)(bbcDateIdID, lastItems, summary);
        }
        return this.saveCollateralAdjustment(bbcDateId, { items: lastItems, notes: '' });
    }
    async saveCollateralAdjustment(bbcDateId, collateralAdjustmentsUpdate) {
        const isBBCLockedResult = await this.signsService.isBBCLocked(bbcDateId);
        if (isBBCLockedResult) {
            return null;
        }
        const summary = this.calculateSummary(collateralAdjustmentsUpdate.items);
        return await (0, collateral_adjustments_db_1.saveCollateralAdjustmentDoc)(new mongoose_1.default.Types.ObjectId(bbcDateId), {
            ...collateralAdjustmentsUpdate,
            summary,
        });
    }
    ;
    async getLastCopyCollateralAdjustmentItems(bbcDateId) {
        const { borrowerId } = await (0, bbcDates_db_1.getBBCDateDoc)(bbcDateId.toString());
        const collateralAdjustmentDocs = await (0, collateral_adjustments_db_1.getLastCollateralAdjustmentDocs)(bbcDateId, borrowerId.toString());
        if (!!collateralAdjustmentDocs.length) {
            return collateralAdjustmentDocs.pop().items;
        }
        return [];
    }
    calculateSummary(collateralAdjustmentItems) {
        const summary = [];
        const summaryOrder = [
            collaterals_enum_1.ECollaterals.INVENTORY,
            collaterals_enum_1.ECollaterals.RECEIVABLE,
            collaterals_enum_1.ECollaterals.OTHER,
        ];
        const result = Object.keys(collaterals_enum_1.ECollaterals).map((equipmentType) => {
            const typeSummary = { rowDef: equipmentType, amount: 0 };
            collateralAdjustmentItems.map((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.CollateralAdjustmentsService = CollateralAdjustmentsService;
