"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUnitCostDifferenceHeader = exports.getInvoiceMovement = exports.getInvoiceMovementFull = void 0;
const excel_helper_1 = require("../../helpers/excel.helper");
const BBCDate_model_1 = require("../../models/BBCDate.model");
const collaterals_db_1 = require("../../db/collaterals.db");
const inventories_db_1 = require("../../db/inventories.db");
const collaterals_enum_1 = require("../../enums/collaterals.enum");
const getInvoiceMovementFull = async (params) => {
    const { reportAdditions, reportRemoved } = await (0, exports.getInvoiceMovement)(params);
    const headerAdditions = (0, exports.getUnitCostDifferenceHeader)('additions');
    const headerRemoved = (0, exports.getUnitCostDifferenceHeader)('removed');
    const tableAdditionsFull = (0, excel_helper_1.addHeaderToData)(headerAdditions, reportAdditions);
    const tableRemovedFull = (0, excel_helper_1.addHeaderToData)(headerRemoved, reportRemoved);
    return { 'additions': tableAdditionsFull, 'removed': tableRemovedFull };
};
exports.getInvoiceMovementFull = getInvoiceMovementFull;
const getInvoiceMovement = async (params) => {
    const { bbcDateId, borrowerId } = params;
    const currentBbcDate = await BBCDate_model_1.BBCDateModel.findById(bbcDateId);
    if (currentBbcDate) {
        const previousDocuments = await (0, collaterals_db_1.getPreviousCollateralDocument)(currentBbcDate.borrowerId.toString(), currentBbcDate.bbcDate, collaterals_enum_1.ECollaterals.INVENTORY, 1);
        if (previousDocuments.length > 0) {
            return (0, inventories_db_1.getInventoryMovement)(borrowerId.toString(), previousDocuments[0].bbcDate.toString(), currentBbcDate.bbcDate.toString());
        }
    }
    return { reportAdditions: [], reportRemoved: [] };
};
exports.getInvoiceMovement = getInvoiceMovement;
const getUnitCostDifferenceHeader = (type) => {
    const headerMap = {
        'additions': {
            sku: 'SKU',
            previousValue: 'Previous',
            currentValue: 'Current',
            units: 'New units',
            value: 'Value increase',
        },
        'removed': {
            sku: 'SKU',
            previousValue: 'Previous',
            currentValue: 'Current',
            units: 'Removed units',
            value: 'Removed increase',
        },
    };
    return headerMap[type];
};
exports.getUnitCostDifferenceHeader = getUnitCostDifferenceHeader;
