"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveSeasonalRates = exports.getSeasonalRates = exports.getNewSeasonalRates = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const InventorySeasonalRates_model_1 = require("../models/InventorySeasonalRates.model");
const getNewSeasonalRates = async (borrowerId) => {
    return {
        borrowerId: new mongoose_1.default.Types.ObjectId(borrowerId),
        items: [],
    };
};
exports.getNewSeasonalRates = getNewSeasonalRates;
const getSeasonalRates = async (borrowerId) => {
    const foundRates = await InventorySeasonalRates_model_1.InventorySeasonalEntryModel.findOne({ borrowerId });
    if (foundRates) {
        return foundRates;
    }
    return await (0, exports.getNewSeasonalRates)(borrowerId);
};
exports.getSeasonalRates = getSeasonalRates;
const saveSeasonalRates = async (borrowerId, rates) => {
    const foundRates = await InventorySeasonalRates_model_1.InventorySeasonalEntryModel.findOne({ borrowerId });
    if (foundRates) {
        const mappedRates = rates.map((rate) => {
            const { _id, ...restRate } = rate;
            return restRate;
        });
        await InventorySeasonalRates_model_1.InventorySeasonalEntryModel.findByIdAndUpdate(foundRates._id, { items: mappedRates });
    }
    else {
        const mappedRates = rates.map((rate) => {
            const { _id, ...restRate } = rate;
            return restRate;
        });
        const newRates = await InventorySeasonalRates_model_1.InventorySeasonalEntryModel.create({ borrowerId, items: mappedRates });
        await newRates.save();
    }
};
exports.saveSeasonalRates = saveSeasonalRates;
