import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useMemo, useRef } from "react";
import { Table } from "../../../table";
import { Numeral, Text } from "../../../text";
import { useAccount, usePrivateInfiniteQuery, useQuery } from "@orderly.network/hooks";
import { Tooltip } from "../../../tooltip";
import { NetworkImage } from "../../../icon";
import { formatTxID, generateKeyFun, getInfiniteData, upperFirstLetter } from "../utils";
import { useEndReached } from "../../../listView/useEndReached";
import { AccountStatusEnum } from "@orderly.network/types";
import { cn } from "../../../utils";
const AssetHistory = (props)=>{
    const sentinelRef = useRef(null);
    const { state } = useAccount();
    const { data: chains } = useQuery("/v1/public/chain_info");
    const { data, size, setSize, isLoading } = usePrivateInfiniteQuery(generateKeyFun("/v1/asset/history", {
        size: 100
    }), {
        initialSize: 1,
        formatter: (data)=>data,
        revalidateOnFocus: false
    });
    const dataSource = useMemo(()=>{
        if (state.status < AccountStatusEnum.EnableTrading) {
            return [];
        }
        return getInfiniteData(data);
    }, [
        state,
        data
    ]);
    useEndReached(sentinelRef, ()=>{
        if (!isLoading) {
            setSize(size + 1);
        }
    });
    const columns = useMemo(()=>{
        return [
            {
                title: "Token",
                dataIndex: "token",
                render (value, record, index) {
                    return /*#__PURE__*/ _jsxs("div", {
                        className: "orderly-flex orderly-items-center orderly-text-base-contrast-80",
                        children: [
                            /*#__PURE__*/ _jsx(NetworkImage, {
                                type: "token",
                                name: value,
                                size: "small",
                                rounded: true
                            }),
                            /*#__PURE__*/ _jsx("div", {
                                className: "orderly-ml-[6px]",
                                children: value
                            })
                        ]
                    });
                }
            },
            {
                title: "Time",
                dataIndex: "created_time",
                render (value, record, index) {
                    return /*#__PURE__*/ _jsx(Text, {
                        rule: "date",
                        formatString: "YYYY-MM-DD HH:mm:ss",
                        className: "orderly-text-base-contrast-98 orderly-text-3xs",
                        children: value
                    });
                }
            },
            {
                title: "TxID",
                dataIndex: "tx_id",
                render (value, record, index) {
                    if (!value) {
                        return /*#__PURE__*/ _jsx("div", {
                            className: "orderly-text-base-contrast-54",
                            children: "-"
                        });
                    }
                    const chainInfo = chains?.find((item)=>parseInt(record.chain_id) === parseInt(item.chain_id));
                    const explorer_base_url = chainInfo?.explorer_base_url;
                    const href = `${explorer_base_url}/tx/${value}`;
                    return /*#__PURE__*/ _jsx("a", {
                        href: href,
                        target: "_blank",
                        children: /*#__PURE__*/ _jsx(Tooltip, {
                            content: value,
                            delayDuration: 0,
                            children: /*#__PURE__*/ _jsx("span", {
                                className: "orderly-text-base-contrast-54 orderly-border-b-[1px] orderly-border-dashed orderly-border-base-contrast-54 orderly-cursor-pointer",
                                children: formatTxID(value)
                            })
                        })
                    });
                }
            },
            {
                title: "Status",
                dataIndex: "trans_status",
                render (value, record, index) {
                    const isEnd = [
                        "COMPLETED",
                        "CANCELED",
                        "FAILED"
                    ].includes(value);
                    return /*#__PURE__*/ _jsx("div", {
                        className: isEnd ? "orderly-text-base-contrast-54" : "orderly-text-base-contrast-98",
                        children: upperFirstLetter(value)
                    });
                }
            },
            {
                title: "Type",
                dataIndex: "side",
                render (value, record, index) {
                    return /*#__PURE__*/ _jsx("div", {
                        className: value === "DEPOSIT" ? "orderly-text-success-light" : "orderly-text-danger-light",
                        children: upperFirstLetter(value)
                    });
                }
            },
            {
                title: "Amount",
                dataIndex: "amount",
                align: "right",
                className: "orderly-w-[120px]",
                render (value, record, index) {
                    const isDeposit = record.side === "DEPOSIT";
                    return /*#__PURE__*/ _jsxs("div", {
                        className: isDeposit ? "orderly-text-success-light" : "orderly-text-danger-light",
                        children: [
                            isDeposit ? "+" : "-",
                            /*#__PURE__*/ _jsx(Numeral, {
                                children: Math.abs(value)
                            })
                        ]
                    });
                }
            }
        ];
    }, [
        chains
    ]);
    return /*#__PURE__*/ _jsxs("div", {
        className: "orderly-overflow-y-auto orderly-h-[100vh] orderly-pb-[300px]",
        children: [
            /*#__PURE__*/ _jsx(Table, {
                dataSource: dataSource,
                columns: columns,
                loading: isLoading,
                className: "orderly-text-2xs orderly-min-h-[300px]",
                headerClassName: 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__*/ _jsx("div", {
                ref: sentinelRef,
                className: "orderly-relative orderly-invisible orderly-h-[1px] orderly-top-[-300px]"
            })
        ]
    });
};
export default AssetHistory;

//# sourceMappingURL=assetHistory.js.map