"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InventoryAvailabilityModel = exports.InventoryAvailabilityScheme = exports.getNormalizedValue = exports.mapMatcherObjNew = exports.mapMatcherObj = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const _models_1 = require("./_models");
const inventory_availability_results_enum_1 = require("../enums/inventory-availability-results.enum");
const mongooseLeanId = require('../plugins/id.plugin');
exports.mapMatcherObj = {
    GT: (query) => {
        return { [query.field]: { $gt: (0, exports.getNormalizedValue)(query.filterValue) } };
    },
    GTE: (query) => {
        return { [query.field]: { $gte: (0, exports.getNormalizedValue)(query.filterValue) } };
    },
    LT: (query) => {
        return { [query.field]: { $lt: (0, exports.getNormalizedValue)(query.filterValue) } };
    },
    LTE: (query) => {
        return { [query.field]: { $lte: (0, exports.getNormalizedValue)(query.filterValue) } };
    },
    EQ: (query) => {
        return { [query.field]: { $eq: (0, exports.getNormalizedValue)(query.filterValue) } };
    },
    NE: (query) => {
        return { [query.field]: { $ne: (0, exports.getNormalizedValue)(query.filterValue) } };
    },
    CON: (query) => {
        return { [query.field]: { $regex: query.filterValue.toString(), $options: 'i' } };
    },
    NC: (query) => {
        return { [query.field]: { $not: { $regex: query.filterValue.toString(), $options: 'i' } } };
    },
};
const mapMatcherObjNew = (queries) => {
    return queries.reduce((acc, query) => [...acc, exports.mapMatcherObj[query.filter](query)], []);
};
exports.mapMatcherObjNew = mapMatcherObjNew;
const getNormalizedValue = (value) => {
    if (typeof value === 'number') {
        return value;
    }
    if (value instanceof Date) {
        return value;
    }
    if (value.match(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/)) {
        return new Date(value.slice(0, -1));
    }
    return value;
};
exports.getNormalizedValue = getNormalizedValue;
exports.InventoryAvailabilityScheme = new mongoose_1.default.Schema({
    status: {
        type: String,
        required: true,
        default: 'NEW',
    },
    borrowerId: {
        type: mongoose_1.default.Schema.Types.ObjectId,
        ref: _models_1.MODEL_NAMES.borrowers,
        required: true,
    },
    bbcDateId: {
        type: mongoose_1.default.Schema.Types.ObjectId,
        ref: _models_1.MODEL_NAMES.inventories,
        required: true,
    },
    options: {
        maxAdvanceRatePriority: {
            type: Boolean,
            required: true,
        },
    },
    useManualInputs: {
        type: Boolean,
        default: false,
    },
    filters: [
        {
            active: {
                type: Boolean,
                required: true,
                default: true,
            },
            inventoryName: {
                type: String,
                required: true,
                trim: true,
            },
            advanceRate: {
                type: Number,
                required: true,
            },
            valid: {
                type: Boolean,
                required: true,
            },
            queries: [
                {
                    conjunction: {
                        type: String,
                        required: true,
                        trim: true,
                    },
                    field: {
                        type: String,
                        required: true,
                        trim: true,
                    },
                    filter: {
                        type: String,
                        required: true,
                        trim: true,
                    },
                    filterValue: {
                        type: mongoose_1.default.Schema.Types.Mixed,
                        required: true,
                        trim: true,
                    },
                }
            ],
        },
    ],
    summary: [
        {
            order: {
                type: Number,
                required: true,
            },
            isGroup: {
                type: Boolean,
                required: true,
            },
            inventoryName: {
                type: String,
                required: true,
            },
            totalQty: {
                type: Number,
                required: true,
            },
            totalValue: {
                type: Number,
                required: true,
            },
            advanceRate: {
                type: Number,
                required: true,
            },
            availability: {
                type: Number,
                required: true,
            },
        }
    ],
    __v: { type: Number, select: false },
}, { timestamps: true });
exports.InventoryAvailabilityScheme.plugin(mongooseLeanId);
exports.InventoryAvailabilityModel = mongoose_1.default.model(_models_1.MODEL_NAMES.inventoryAvailability, exports.InventoryAvailabilityScheme);
