"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "default", {
    enumerable: true,
    get: function() {
        return _default;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _table = require("../../../table");
const _text = require("../../../text");
const _hooks = require("@orderly.network/hooks");
const _icon = require("../../../icon");
const _utils = require("@orderly.network/utils");
const _utils1 = require("../utils");
const _useEndReached = require("../../../listView/useEndReached");
const _types = require("@orderly.network/types");
const _utils2 = require("../../../utils");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const FundingFee = ()=>{
    const sentinelRef = (0, _react.useRef)(null);
    const { state } = (0, _hooks.useAccount)();
    const symbolInfo = (0, _hooks.useSymbolsInfo)();
    const { data, size, setSize, isLoading } = (0, _hooks.usePrivateInfiniteQuery)((0, _utils1.generateKeyFun)("/v1/funding_fee/history", {
        size: 100
    }), {
        initialSize: 1,
        formatter: (data)=>data,
        revalidateOnFocus: false
    });
    const dataSource = (0, _react.useMemo)(()=>{
        if (state.status < _types.AccountStatusEnum.EnableTrading) {
            return [];
        }
        return (0, _utils1.getInfiniteData)(data);
    }, [
        state,
        data
    ]);
    (0, _useEndReached.useEndReached)(sentinelRef, ()=>{
        if (!isLoading) {
            setSize(size + 1);
        }
    });
    const columns = (0, _react.useMemo)(()=>{
        return [
            {
                title: "Instrument",
                dataIndex: "symbol",
                render (value, record, index) {
                    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                        className: " orderly-flex orderly-items-center orderly-text-base-contrast-80",
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_icon.NetworkImage, {
                                type: "symbol",
                                symbol: value,
                                size: "small",
                                rounded: true
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Text, {
                                rule: "symbol",
                                className: "orderly-ml-[6px]",
                                children: value
                            })
                        ]
                    });
                }
            },
            {
                title: "Time",
                dataIndex: "created_time",
                render (value, record, index) {
                    return /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Text, {
                        rule: "date",
                        formatString: "YYYY-MM-DD HH:mm:ss",
                        className: "orderly-text-base-contrast-98 orderly-text-3xs",
                        children: value
                    });
                }
            },
            {
                title: "Funding rate / Annual rate",
                dataIndex: "funding_rate",
                render (value, record, index) {
                    const funding_period = symbolInfo?.[record.symbol]?.("funding_period");
                    const percent = new _utils.Decimal(value).mul(100).toFixed(6);
                    const annualRate = (0, _utils1.getAnnualRate)(value, funding_period);
                    return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        className: "orderly-text-base-contrast-80",
                        children: `${percent}% / ${annualRate}%`
                    });
                }
            },
            {
                title: "Payment type",
                dataIndex: "payment_type",
                render (value, record, index) {
                    const map = {
                        Pay: "Paid",
                        Receive: "Received"
                    };
                    return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        className: "orderly-text-base-contrast-80",
                        children: map[value] || value
                    });
                }
            },
            {
                title: "Funding fee (USDC)",
                dataIndex: "funding_fee",
                align: "right",
                className: "orderly-w-[150px]",
                render (value, record, index) {
                    const isReceived = record.payment_type === "Receive";
                    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                        className: isReceived ? "orderly-text-success-light" : "orderly-text-danger-light",
                        children: [
                            isReceived ? "+" : "-",
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Numeral, {
                                precision: 6,
                                children: Math.abs(value)
                            })
                        ]
                    });
                }
            }
        ];
    }, [
        symbolInfo
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        className: "orderly-overflow-y-auto orderly-h-[100vh] orderly-pb-[50px]",
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_table.Table, {
                dataSource: dataSource,
                columns: columns,
                loading: isLoading,
                className: "orderly-text-2xs orderly-min-h-[300px]",
                headerClassName: (0, _utils2.cn)("orderly-h-[40px] orderly-text-base-contrast-54 orderly-bg-base-900", "orderly-border-b orderly-border-b-divider"),
                generatedRowKey: (record)=>record.id,
                onRow: (record)=>({
                        className: "orderly-h-[40px] orderly-border-b-[1px] orderly-border-b-solid orderly-border-[rgba(255,255,255,0.04)]"
                    })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                ref: sentinelRef,
                className: "orderly-relative orderly-invisible orderly-h-[1px] orderly-top-[-300px]"
            })
        ]
    });
};
const _default = FundingFee;

//# sourceMappingURL=fundingFee.js.map