"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBBCAPByAPPeriod = exports.getAllAPCustomers = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const AccountPayableItem_model_1 = require("../models/AccountPayableItem.model");
const collaterals_db_1 = require("./collaterals.db");
const collaterals_enum_1 = require("../enums/collaterals.enum");
const getAllAPCustomers = async (borrowerId) => {
    const bbcSheets = await (0, collaterals_db_1.getBBCSheetsForBorrowerAndType)(borrowerId, collaterals_enum_1.ECollaterals.ACCOUNT_PAYABLE);
    return AccountPayableItem_model_1.AccountPayableItemModel.aggregate([
        {
            $match: {
                'bbcSheetId': {
                    $in: bbcSheets.map((bbcSheet) => new mongoose_1.default.Types.ObjectId(String(bbcSheet._id)))
                }
            }
        },
        {
            $group: {
                '_id': null,
                'customers': {
                    $addToSet: '$customerName'
                }
            }
        },
        {
            $project: {
                '_id': 0
            }
        },
    ]);
};
exports.getAllAPCustomers = getAllAPCustomers;
const getBBCAPByAPPeriod = async (bbcSheetsIds, rangeStart, rangeEnd) => {
    return AccountPayableItem_model_1.AccountPayableItemModel.aggregate([
        {
            $match: {
                $and: [
                    { 'bbcSheetId': { $in: bbcSheetsIds } },
                    { 'apDate': { $gte: new Date(rangeStart) } },
                    { 'apDate': { $lte: new Date(rangeEnd) } },
                ]
            }
        },
        {
            $sort: {
                'apDate': 1
            }
        },
    ]);
};
exports.getBBCAPByAPPeriod = getBBCAPByAPPeriod;
