"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInventoryAvailabilityItems = exports.createInventoryAvailability = void 0;
const lodash_1 = __importDefault(require("lodash"));
const mongoose_1 = __importDefault(require("mongoose"));
const InventoryAvailability_model_1 = require("../models/InventoryAvailability.model");
const InventoryAvailabilityItem_model_1 = require("../models/InventoryAvailabilityItem.model");
const inventory_availability_results_enum_1 = require("../enums/inventory-availability-results.enum");
const collaterals_db_1 = require("./collaterals.db");
const createInventoryAvailability = async (inventoryAvailability) => {
    const update = lodash_1.default.pick(inventoryAvailability, ['borrowerId', 'filters', 'options']);
    return InventoryAvailability_model_1.InventoryAvailabilityModel
        .findOneAndUpdate({ bbcDateId: inventoryAvailability.bbcDateId }, update, { upsert: true, new: true })
        .lean();
};
exports.createInventoryAvailability = createInventoryAvailability;
const getInventoryAvailabilityItems = async (inventoryAvailabilityId, paginators) => {
    const getMap = {
        [inventory_availability_results_enum_1.EInventoryAvailabilityResults.UNIQ_ITEMS]: {
            unique: true,
            matched: true,
        },
        [inventory_availability_results_enum_1.EInventoryAvailabilityResults.NON_UNIQ_ITEMS]: {
            unique: false,
            matched: true,
        },
        [inventory_availability_results_enum_1.EInventoryAvailabilityResults.NOT_MATCHED_ITEMS]: {
            unique: false,
            matched: false,
        },
    };
    const allItems = await Promise.all(Object.entries(getMap).map(async ([key, settings]) => {
        const items = await InventoryAvailabilityItem_model_1.InventoryAvailabilityItemModel.aggregate([
            {
                $match: {
                    'inventoryAvailabilityId': new mongoose_1.default.Types.ObjectId(inventoryAvailabilityId),
                    'unique': settings.unique,
                    'matched': settings.matched,
                }
            },
            {
                $sort: {
                    order: 1
                }
            },
            ...(0, collaterals_db_1.ITEMS_PAGINATION)(paginators[key]),
            {
                $lookup: {
                    from: 'inventoryitems',
                    localField: 'inventoryId',
                    foreignField: '_id',
                    as: 'inventory',
                },
            },
            {
                $replaceRoot: {
                    newRoot: {
                        $mergeObjects: [
                            {
                                $arrayElemAt: ['$inventory', 0],
                            },
                            '$$ROOT',
                        ],
                    },
                },
            },
            {
                $unwind: {
                    path: '$inventory',
                },
            },
            {
                $lookup: {
                    from: 'bbcsheets',
                    localField: 'inventory.bbcSheetId',
                    foreignField: '_id',
                    as: 'bbcSheet',
                },
            },
            {
                $unwind: {
                    path: '$bbcSheet',
                },
            },
            {
                $project: {
                    inventory: 0,
                },
            },
        ]);
        const totalItems = await InventoryAvailabilityItem_model_1.InventoryAvailabilityItemModel.countDocuments({
            'inventoryAvailabilityId': new mongoose_1.default.Types.ObjectId(inventoryAvailabilityId),
            'unique': settings.unique,
            'matched': settings.matched,
        });
        return { [key]: { items, totalItems, paginatorOptions: paginators[key] } };
    }, {}));
    return allItems.reduce((acc, group) => ({ ...acc, ...group }), {});
};
exports.getInventoryAvailabilityItems = getInventoryAvailabilityItems;
