import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment, useCallback, useMemo } from "react";
import { tableNames } from "../constants/names.js";
import { useEditorContext } from "../context/editor-context.js";
import { sumTotalForProperty } from "../utils/calculations.js";
import { handleTableDatum } from "../utils/table.js";
import { RWATableCell } from "./table-cell.js";
import { RWATableRow } from "./table-row.js";
import { TableWithForm } from "./table-with-form.js";
export function AssetsTable() {
    const tableName = tableNames.ASSET;
    const { selectedTableItem, fixedIncomes, cashAsset } = useEditorContext();
    const cashAssetFormattedAsTableItem = useMemo(() => ({
        id: "special-first-row",
        name: "Cash $USD",
        fixedIncomeTypeId: "--",
        spvId: "--",
        maturity: "--",
        ISIN: "--",
        CUSIP: "--",
        coupon: null,
        notional: cashAsset.balance ?? 0,
        purchaseDate: "--",
        purchasePrice: "--",
        purchaseProceeds: "--",
        salesProceeds: "--",
        totalDiscount: "--",
        realizedSurplus: "--",
        currentValue: "--",
    }), [cashAsset.balance]);
    const totalPurchaseProceeds = sumTotalForProperty(fixedIncomes, "purchaseProceeds");
    const totalSalesProceeds = sumTotalForProperty(fixedIncomes, "salesProceeds");
    const totalTotalDiscount = sumTotalForProperty(fixedIncomes, "totalDiscount");
    const totalRealizedSurplus = sumTotalForProperty(fixedIncomes, "realizedSurplus");
    const specialFirstRow = useCallback((c) => (_jsx(RWATableRow, { children: c.map((column) => (_jsxs(Fragment, { children: [column.key === "name" && _jsx(RWATableCell, { children: "Cash $USD" }), column.key === "notional" && (_jsx(RWATableCell, { className: "text-right", children: handleTableDatum(cashAssetFormattedAsTableItem[column.key]) }, column.key)), column.key !== "name" && column.key !== "notional" && (_jsx(RWATableCell, {}))] }, column.key))) })), [cashAssetFormattedAsTableItem]);
    const specialLastRow = useCallback((c) => (_jsx(RWATableRow, { className: "sticky bottom-0", children: c.map((column) => (_jsxs(Fragment, { children: [column.key === "name" && _jsx(RWATableCell, { children: "Totals" }), column.key === "purchaseProceeds" && (_jsx(RWATableCell, { className: "text-right", children: handleTableDatum(totalPurchaseProceeds) }, column.key)), column.key === "salesProceeds" && (_jsx(RWATableCell, { className: "text-right", children: handleTableDatum(totalSalesProceeds) }, column.key)), column.key === "totalDiscount" && (_jsx(RWATableCell, { className: "text-right", children: handleTableDatum(totalTotalDiscount) }, column.key)), column.key === "realizedSurplus" && (_jsx(RWATableCell, { className: "text-right", children: handleTableDatum(totalRealizedSurplus) }, column.key)), column.key !== "name" &&
                    column.key !== "purchaseProceeds" &&
                    column.key !== "salesProceeds" &&
                    column.key !== "totalDiscount" &&
                    column.key !== "realizedSurplus" && _jsx(RWATableCell, {})] }, column.key))) })), [
        selectedTableItem,
        totalPurchaseProceeds,
        totalRealizedSurplus,
        totalSalesProceeds,
        totalTotalDiscount,
    ]);
    return (_jsx(TableWithForm, { specialFirstRow: specialFirstRow, specialLastRow: specialLastRow, tableName: tableName }));
}
//# sourceMappingURL=assets-table.js.map