"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FinancialSpreadingSheet = exports.FinancialSpreadingSheetSchema = exports.financialSpreadingSheetViewValidationSchema = exports.FinancialSpreadingSheetDTO = exports.financeSpreadingLists = exports.financialSpreadingTotalDictionary = exports.financeSpreadingBSList = exports.EFinanceSpreadingBSTotal = exports.financeSpreadingPLList = exports.EFinanceSpreadingPLTotal = exports.EFinancialSpreadingType = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const joi_1 = __importDefault(require("joi"));
const _models_1 = require("./_models");
const db_data_helper_1 = require("../helpers/db-data.helper");
var EFinancialSpreadingType;
(function (EFinancialSpreadingType) {
    EFinancialSpreadingType["PROFIT_LOSS"] = "PROFIT_LOSS";
    EFinancialSpreadingType["BALANCE_SHEET"] = "BALANCE_SHEET";
})(EFinancialSpreadingType || (exports.EFinancialSpreadingType = EFinancialSpreadingType = {}));
var EFinanceSpreadingPLTotal;
(function (EFinanceSpreadingPLTotal) {
    EFinanceSpreadingPLTotal["SALES"] = "SALES";
    EFinanceSpreadingPLTotal["COST_OF_SALES"] = "COST_OF_SALES";
    EFinanceSpreadingPLTotal["GROSS_PROFIT"] = "GROSS_PROFIT";
    EFinanceSpreadingPLTotal["GROSS_MARGIN"] = "GROSS_MARGIN";
    EFinanceSpreadingPLTotal["OPERATING_EXPENSES"] = "OPERATING_EXPENSES";
    EFinanceSpreadingPLTotal["INCOME_FROM_OPERATIONS"] = "INCOME_FROM_OPERATIONS";
    EFinanceSpreadingPLTotal["OPERATING_MARGIN"] = "OPERATING_MARGIN";
    EFinanceSpreadingPLTotal["NON_OPERATING_EXPENSES"] = "NON_OPERATING_EXPENSES";
    EFinanceSpreadingPLTotal["NON_OPERATING_INCOME"] = "NON_OPERATING_INCOME";
    EFinanceSpreadingPLTotal["FINANCING_COSTS"] = "FINANCING_COSTS";
    EFinanceSpreadingPLTotal["DEPRECIATION_AMORTIZATION"] = "DEPRECIATION_AMORTIZATION";
    EFinanceSpreadingPLTotal["PRE_TAX_INCOME"] = "PRE_TAX_INCOME";
    EFinanceSpreadingPLTotal["TAX"] = "TAX";
    EFinanceSpreadingPLTotal["NET_INCOME"] = "NET_INCOME";
})(EFinanceSpreadingPLTotal || (exports.EFinanceSpreadingPLTotal = EFinanceSpreadingPLTotal = {}));
exports.financeSpreadingPLList = {
    [EFinanceSpreadingPLTotal.SALES]: false,
    [EFinanceSpreadingPLTotal.COST_OF_SALES]: false,
    [EFinanceSpreadingPLTotal.GROSS_PROFIT]: true,
    [EFinanceSpreadingPLTotal.GROSS_MARGIN]: true,
    [EFinanceSpreadingPLTotal.OPERATING_EXPENSES]: false,
    [EFinanceSpreadingPLTotal.OPERATING_MARGIN]: true,
    [EFinanceSpreadingPLTotal.INCOME_FROM_OPERATIONS]: true,
    [EFinanceSpreadingPLTotal.NON_OPERATING_EXPENSES]: false,
    [EFinanceSpreadingPLTotal.NON_OPERATING_INCOME]: false,
    [EFinanceSpreadingPLTotal.FINANCING_COSTS]: false,
    [EFinanceSpreadingPLTotal.DEPRECIATION_AMORTIZATION]: false,
    [EFinanceSpreadingPLTotal.PRE_TAX_INCOME]: true,
    [EFinanceSpreadingPLTotal.TAX]: false,
    [EFinanceSpreadingPLTotal.NET_INCOME]: true,
};
var EFinanceSpreadingBSTotal;
(function (EFinanceSpreadingBSTotal) {
    EFinanceSpreadingBSTotal["CASH_EQUIVALENTS"] = "CASH_EQUIVALENTS";
    EFinanceSpreadingBSTotal["TRADE_RECEIVABLES"] = "TRADE_RECEIVABLES";
    EFinanceSpreadingBSTotal["INVENTORY"] = "INVENTORY";
    EFinanceSpreadingBSTotal["OTHER_CURRENT_ASSETS"] = "OTHER_CURRENT_ASSETS";
    EFinanceSpreadingBSTotal["TOTAL_CURRENT_ASSET"] = "TOTAL_CURRENT_ASSET";
    EFinanceSpreadingBSTotal["FIXED_ASSETS"] = "FIXED_ASSETS";
    EFinanceSpreadingBSTotal["OTHER_NON_CURRENT_ASSETS"] = "OTHER_NON_CURRENT_ASSETS";
    EFinanceSpreadingBSTotal["TOTAL_NON_CURRENT_ASSETS"] = "TOTAL_NON_CURRENT_ASSETS";
    EFinanceSpreadingBSTotal["TOTAL_ASSETS"] = "TOTAL_ASSETS";
    EFinanceSpreadingBSTotal["ACCOUNTS_PAYABLE"] = "ACCOUNTS_PAYABLE";
    EFinanceSpreadingBSTotal["OTHER_CURRENT_LIABILITIES"] = "OTHER_CURRENT_LIABILITIES";
    EFinanceSpreadingBSTotal["TOTAL_CURRENT_LIABILITIES"] = "TOTAL_CURRENT_LIABILITIES";
    EFinanceSpreadingBSTotal["COMPANY_DEBT"] = "COMPANY_DEBT";
    EFinanceSpreadingBSTotal["OTHER_DEBT"] = "OTHER_DEBT";
    EFinanceSpreadingBSTotal["OTHER_LONG_TERM_LIABILITIES"] = "OTHER_LONG_TERM_LIABILITIES";
    EFinanceSpreadingBSTotal["LONG_TERM_LIABILITIES"] = "LONG_TERM_LIABILITIES";
    EFinanceSpreadingBSTotal["TOTAL_LIABILITIES"] = "TOTAL_LIABILITIES";
    EFinanceSpreadingBSTotal["RETAINED_EARNINGS"] = "RETAINED_EARNINGS";
    EFinanceSpreadingBSTotal["EQUITY_RESERVES"] = "EQUITY_RESERVES";
    EFinanceSpreadingBSTotal["CHECK"] = "CHECK";
})(EFinanceSpreadingBSTotal || (exports.EFinanceSpreadingBSTotal = EFinanceSpreadingBSTotal = {}));
exports.financeSpreadingBSList = {
    [EFinanceSpreadingBSTotal.CASH_EQUIVALENTS]: false,
    [EFinanceSpreadingBSTotal.TRADE_RECEIVABLES]: false,
    [EFinanceSpreadingBSTotal.INVENTORY]: false,
    [EFinanceSpreadingBSTotal.OTHER_CURRENT_ASSETS]: false,
    [EFinanceSpreadingBSTotal.TOTAL_CURRENT_ASSET]: true,
    [EFinanceSpreadingBSTotal.FIXED_ASSETS]: false,
    [EFinanceSpreadingBSTotal.OTHER_NON_CURRENT_ASSETS]: false,
    [EFinanceSpreadingBSTotal.TOTAL_NON_CURRENT_ASSETS]: true,
    [EFinanceSpreadingBSTotal.TOTAL_ASSETS]: true,
    [EFinanceSpreadingBSTotal.ACCOUNTS_PAYABLE]: false,
    [EFinanceSpreadingBSTotal.OTHER_CURRENT_LIABILITIES]: false,
    [EFinanceSpreadingBSTotal.TOTAL_CURRENT_LIABILITIES]: true,
    [EFinanceSpreadingBSTotal.COMPANY_DEBT]: false,
    [EFinanceSpreadingBSTotal.OTHER_DEBT]: false,
    [EFinanceSpreadingBSTotal.OTHER_LONG_TERM_LIABILITIES]: false,
    [EFinanceSpreadingBSTotal.LONG_TERM_LIABILITIES]: true,
    [EFinanceSpreadingBSTotal.TOTAL_LIABILITIES]: true,
    [EFinanceSpreadingBSTotal.RETAINED_EARNINGS]: false,
    [EFinanceSpreadingBSTotal.EQUITY_RESERVES]: false,
    [EFinanceSpreadingBSTotal.CHECK]: true,
};
exports.financialSpreadingTotalDictionary = {
    [EFinanceSpreadingPLTotal.SALES]: 'Sales',
    [EFinanceSpreadingPLTotal.COST_OF_SALES]: 'Cost of Sales',
    [EFinanceSpreadingPLTotal.GROSS_PROFIT]: 'Gross Profit',
    [EFinanceSpreadingPLTotal.GROSS_MARGIN]: 'Gross Margin',
    [EFinanceSpreadingPLTotal.OPERATING_EXPENSES]: 'Operating Expenses',
    [EFinanceSpreadingPLTotal.INCOME_FROM_OPERATIONS]: 'Income from Operations',
    [EFinanceSpreadingPLTotal.OPERATING_MARGIN]: 'Operating Margin',
    [EFinanceSpreadingPLTotal.NON_OPERATING_EXPENSES]: 'Non operating Expenses',
    [EFinanceSpreadingPLTotal.NON_OPERATING_INCOME]: 'Non operating Income',
    [EFinanceSpreadingPLTotal.FINANCING_COSTS]: 'Financing Costs',
    [EFinanceSpreadingPLTotal.DEPRECIATION_AMORTIZATION]: 'Depreciation & Amortization',
    [EFinanceSpreadingPLTotal.PRE_TAX_INCOME]: 'Pre-Tax Income',
    [EFinanceSpreadingPLTotal.TAX]: 'Tax',
    [EFinanceSpreadingPLTotal.NET_INCOME]: 'Net Income',
    [EFinanceSpreadingBSTotal.CASH_EQUIVALENTS]: 'Cash & Equivalents',
    [EFinanceSpreadingBSTotal.TRADE_RECEIVABLES]: 'Trade Receivables',
    [EFinanceSpreadingBSTotal.INVENTORY]: 'Inventory',
    [EFinanceSpreadingBSTotal.OTHER_CURRENT_ASSETS]: 'Other Current Assets',
    [EFinanceSpreadingBSTotal.TOTAL_CURRENT_ASSET]: 'Total Current Assets',
    [EFinanceSpreadingBSTotal.FIXED_ASSETS]: 'Fixed Assets',
    [EFinanceSpreadingBSTotal.OTHER_NON_CURRENT_ASSETS]: 'Other Non Current Assets',
    [EFinanceSpreadingBSTotal.TOTAL_NON_CURRENT_ASSETS]: 'Total Non Current Assets',
    [EFinanceSpreadingBSTotal.ACCOUNTS_PAYABLE]: 'Accounts Payable',
    [EFinanceSpreadingBSTotal.OTHER_CURRENT_LIABILITIES]: 'Other Current Liabilities',
    [EFinanceSpreadingBSTotal.TOTAL_CURRENT_LIABILITIES]: 'Total Current Liabilities',
    [EFinanceSpreadingBSTotal.COMPANY_DEBT]: 'Senior Debt',
    [EFinanceSpreadingBSTotal.OTHER_DEBT]: 'Other Debt',
    [EFinanceSpreadingBSTotal.OTHER_LONG_TERM_LIABILITIES]: 'Other Long Term Liabilities',
    [EFinanceSpreadingBSTotal.LONG_TERM_LIABILITIES]: 'Long Term Liabilities',
    [EFinanceSpreadingBSTotal.TOTAL_ASSETS]: 'Total Assets',
    [EFinanceSpreadingBSTotal.TOTAL_LIABILITIES]: 'Total Liabilities',
    [EFinanceSpreadingBSTotal.RETAINED_EARNINGS]: 'Retained Earnings',
    [EFinanceSpreadingBSTotal.EQUITY_RESERVES]: 'Equity & Reserves',
    [EFinanceSpreadingBSTotal.CHECK]: 'Check',
};
exports.financeSpreadingLists = {
    [EFinancialSpreadingType.PROFIT_LOSS]: exports.financeSpreadingPLList,
    [EFinancialSpreadingType.BALANCE_SHEET]: exports.financeSpreadingBSList,
};
class FinancialSpreadingSheetDTO extends db_data_helper_1.BasicDTO {
    borrowerId;
    financialSpreadingType;
    rowType;
    name;
    order;
    suborder;
    isTotal;
    isCalculated;
    isFixedCost;
}
exports.FinancialSpreadingSheetDTO = FinancialSpreadingSheetDTO;
exports.financialSpreadingSheetViewValidationSchema = joi_1.default.object({
    _id: joi_1.default.string().required(),
    name: joi_1.default.string().required(),
    borrowerId: joi_1.default.string().required(),
    financialSpreadingType: joi_1.default.string().required(),
    rowType: joi_1.default.string().required(),
    order: joi_1.default.number().integer().required(),
    suborder: joi_1.default.number().integer().required(),
    isTotal: joi_1.default.boolean().required(),
    isCalculated: joi_1.default.boolean().required(),
    isFixedCost: joi_1.default.boolean().allow(null),
});
exports.FinancialSpreadingSheetSchema = new mongoose_1.default.Schema({
    borrowerId: {
        type: mongoose_1.default.Schema.Types.ObjectId,
        ref: _models_1.MODEL_NAMES.borrowers,
        required: true,
    },
    financialSpreadingType: {
        type: String,
        enum: Object.values(EFinancialSpreadingType),
        required: true,
    },
    rowType: {
        type: String,
        enum: [...Object.values(EFinanceSpreadingPLTotal), ...Object.values(EFinanceSpreadingBSTotal)],
        required: true,
        validate: {
            validator: function (value) {
                if (this.financialSpreadingType === EFinancialSpreadingType.PROFIT_LOSS && !(value in EFinanceSpreadingPLTotal)) {
                    return false;
                }
                if (this.financialSpreadingType === EFinancialSpreadingType.BALANCE_SHEET && !(value in EFinanceSpreadingBSTotal)) {
                    return false;
                }
                return true;
            },
            message: 'Invalid rowType for the provided financialSpreadingType',
        },
    },
    name: {
        type: String,
        required: true,
    },
    order: {
        type: Number,
        required: true,
    },
    suborder: {
        type: Number,
        required: true,
    },
    isTotal: {
        type: Boolean,
        required: true,
    },
    isCalculated: {
        type: Boolean,
        required: true,
    },
    isFixedCost: {
        type: Boolean,
        default: false,
    },
}, {
    timestamps: { createdAt: true, updatedAt: true },
});
exports.FinancialSpreadingSheet = mongoose_1.default.model(_models_1.MODEL_NAMES.financialSpreadingSheets, exports.FinancialSpreadingSheetSchema);
