"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CashAllocationProduct = exports.CashAllocationProductSchema = exports.AccountSchema = void 0;
const mongoose_1 = __importStar(require("mongoose"));
const _models_1 = require("./_models");
const normalizeAccountValue = (value) => {
    if (value === null || value === undefined || value === '') {
        return null;
    }
    if (value === 'BANK') {
        return 'BANK';
    }
    if (typeof value === 'string' && mongoose_1.default.isValidObjectId(value)) {
        return new mongoose_1.default.Types.ObjectId(value);
    }
    if (value instanceof mongoose_1.default.Types.ObjectId) {
        return value;
    }
    return null;
};
exports.AccountSchema = {
    type: Map,
    of: new mongoose_1.Schema({
        accountId1: {
            type: mongoose_1.Schema.Types.Mixed,
            required: false,
            set: normalizeAccountValue,
            validate: {
                validator: function (value) {
                    return (value === null ||
                        mongoose_1.default.isValidObjectId(value) ||
                        value === 'BANK');
                },
                message: (props) => `${props.value} is not a valid ObjectId, "BANK", or null`,
            },
        },
        accountId2: {
            type: mongoose_1.Schema.Types.Mixed,
            required: false,
            set: normalizeAccountValue,
            validate: {
                validator: function (value) {
                    return (value === null ||
                        mongoose_1.default.isValidObjectId(value) ||
                        value === 'BANK');
                },
                message: (props) => `${props.value} is not a valid ObjectId, "BANK", or null`,
            },
        },
    }),
    default: {},
};
exports.CashAllocationProductSchema = new mongoose_1.default.Schema({
    order: {
        type: Number,
        required: true,
    },
    isProduct: {
        type: Boolean,
        required: true,
    },
    isCommonSplit: {
        type: Boolean,
        required: false,
    },
    productId: {
        type: mongoose_1.default.Schema.Types.ObjectId,
        ref: _models_1.MODEL_NAMES.loanProducts,
        required: false,
    },
    nonProductName: {
        type: String,
        required: false,
        trim: true,
    },
    accounts: exports.AccountSchema,
    class: {
        type: String,
        required: false,
        trim: true,
    },
    location: {
        type: String,
        required: false,
        trim: true,
    },
    DRMemo: {
        type: String,
        required: false,
        trim: true,
    },
    CRMemo: {
        type: String,
        required: false,
        trim: true,
    },
}, {
    timestamps: { createdAt: true, updatedAt: false },
});
exports.CashAllocationProduct = mongoose_1.default.model(_models_1.MODEL_NAMES.cashAllocationProducts, exports.CashAllocationProductSchema);
