import { jsx as _jsx } from "react/jsx-runtime";
import { FormattedNumber } from "../components/formatted-number.js";
import { assetTransactionSignByTransactionType, cashTransactionSignByTransactionType, } from "../constants/table.js";
import { FEES_INCOME, feesTransactions, groupTransactionTypeLabels, } from "../constants/transactions.js";
import { calculateCurrentValue } from "./calculations.js";
import { formatDateForDisplay, isISODate } from "./date.js";
import { getFixedIncomeAssets, isAssetGroupTransactionType, } from "./validators.js";
export function handleDateInTable(maybeDate, displayTime = true) {
    const isDate = maybeDate instanceof Date || isISODate(maybeDate);
    if (isDate) {
        const dateStr = maybeDate instanceof Date ? maybeDate.toISOString() : maybeDate;
        return formatDateForDisplay(dateStr, displayTime);
    }
    return maybeDate;
}
export function handleTableDatum(datum, decimalScale = 2, displayTime = true) {
    if (datum === null || datum === undefined)
        return "--";
    if (typeof datum === "number")
        return _jsx(FormattedNumber, { decimalScale: decimalScale, value: datum });
    return handleDateInTable(datum, displayTime);
}
export function calculateUnitPrice(cashAmount, fixedIncomeAmount) {
    if (!cashAmount || !fixedIncomeAmount)
        return 0;
    return cashAmount / fixedIncomeAmount;
}
export function calculateCashBalanceChange(transactionType, cashAmount, fees) {
    if (!cashAmount || !transactionType)
        return 0;
    const sign = cashTransactionSignByTransactionType[transactionType];
    const feeAmounts = (fees?.map((fee) => fee.amount).filter(Boolean) ??
        []);
    const totalFees = feeAmounts.reduce((acc, fee) => acc + fee, 0);
    return cashAmount * sign - totalFees;
}
function makeFixedIncomeTypesTableData(state) {
    const { fixedIncomeTypes } = state;
    return addItemNumber(fixedIncomeTypes);
}
function makeSPVsTableData(state) {
    const { spvs } = state;
    return addItemNumber(spvs);
}
function makeAssetsTableData(state) {
    const { portfolio, transactions, fixedIncomeTypes } = state;
    const currentDate = new Date();
    const fixedIncomes = getFixedIncomeAssets(portfolio);
    const tableItems = fixedIncomes.map((fixedIncome) => {
        const currentValue = calculateCurrentValue({
            asset: fixedIncome,
            currentDate,
            transactions,
            fixedIncomeTypes,
        });
        return {
            ...fixedIncome,
            currentValue,
        };
    });
    return addItemNumber(tableItems);
}
function maybeAddSignToAmount(amount, sign) {
    if (!amount)
        return amount;
    return amount * sign;
}
function makeGroupTransactionsTableData(state) {
    const { transactions, portfolio } = state;
    const fixedIncomes = getFixedIncomeAssets(portfolio);
    const tableData = transactions.map((transaction) => {
        const id = transaction.id;
        const entryTime = transaction.entryTime;
        const asset = fixedIncomes.find((asset) => asset.id === transaction.fixedIncomeTransaction?.assetId)?.name;
        const type = transaction.type;
        const typeLabel = groupTransactionTypeLabels[type];
        const cashTransactionSign = cashTransactionSignByTransactionType[type];
        const assetTransactionSign = isAssetGroupTransactionType(type)
            ? assetTransactionSignByTransactionType[type]
            : 1;
        const quantity = maybeAddSignToAmount(transaction.fixedIncomeTransaction?.amount, assetTransactionSign);
        const cashAmount = maybeAddSignToAmount(transaction.cashTransaction.amount, cashTransactionSign);
        const totalFees = feesTransactions.includes(transaction.type)
            ? (maybeAddSignToAmount(transaction.cashTransaction.amount, transaction.type === FEES_INCOME ? -1 : 1) ?? 0)
            : (transaction.fees?.reduce((acc, fee) => acc + fee.amount, 0) ?? 0);
        const cashBalanceChange = transaction.cashBalanceChange;
        return {
            ...transaction,
            id,
            type,
            typeLabel,
            entryTime,
            asset,
            quantity,
            cashAmount,
            totalFees,
            cashBalanceChange,
        };
    });
    return addItemNumber(tableData);
}
function makeAccountsTableData(state) {
    const { accounts, principalLenderAccountId } = state;
    const withoutPrincipalLender = accounts.filter((account) => account.id !== principalLenderAccountId);
    const tableData = addItemNumber(withoutPrincipalLender);
    return tableData;
}
function makeServiceProviderFeeTypesTableData(state) {
    const { serviceProviderFeeTypes, accounts } = state;
    const tableData = serviceProviderFeeTypes.map((serviceProviderFeeType) => {
        const account = accounts.find((account) => account.id === serviceProviderFeeType.accountId);
        return {
            ...serviceProviderFeeType,
            accountName: account?.label,
            accountReference: account?.reference,
        };
    });
    return addItemNumber(tableData);
}
function addItemNumber(items) {
    return items.map((item, index) => ({
        ...item,
        itemNumber: index + 1,
    }));
}
export function makeTableData(tableName, state) {
    return tableDataMakersByTableName[tableName](state);
}
export const tableDataMakersByTableName = {
    ASSET: makeAssetsTableData,
    TRANSACTION: makeGroupTransactionsTableData,
    ACCOUNT: makeAccountsTableData,
    FIXED_INCOME_TYPE: makeFixedIncomeTypesTableData,
    SERVICE_PROVIDER_FEE_TYPE: makeServiceProviderFeeTypesTableData,
    SPV: makeSPVsTableData,
};
//# sourceMappingURL=table.js.map