"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 _tooltip = require("../../../tooltip");
const _icon = require("../../../icon");
const _utils = require("../utils");
const _useEndReached = require("../../../listView/useEndReached");
const _types = require("@orderly.network/types");
const _utils1 = 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 AssetHistory = (props)=>{
    const sentinelRef = (0, _react.useRef)(null);
    const { state } = (0, _hooks.useAccount)();
    const { data: chains } = (0, _hooks.useQuery)("/v1/public/chain_info");
    const { data, size, setSize, isLoading } = (0, _hooks.usePrivateInfiniteQuery)((0, _utils.generateKeyFun)("/v1/asset/history", {
        size: 100
    }), {
        initialSize: 1,
        formatter: (data)=>data,
        revalidateOnFocus: false
    });
    const dataSource = (0, _react.useMemo)(()=>{
        if (state.status < _types.AccountStatusEnum.EnableTrading) {
            return [];
        }
        return (0, _utils.getInfiniteData)(data);
    }, [
        state,
        data
    ]);
    (0, _useEndReached.useEndReached)(sentinelRef, ()=>{
        if (!isLoading) {
            setSize(size + 1);
        }
    });
    const columns = (0, _react.useMemo)(()=>{
        return [
            {
                title: "Token",
                dataIndex: "token",
                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: "token",
                                name: value,
                                size: "small",
                                rounded: true
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                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: "TxID",
                dataIndex: "tx_id",
                render (value, record, index) {
                    if (!value) {
                        return /*#__PURE__*/ (0, _jsxruntime.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__*/ (0, _jsxruntime.jsx)("a", {
                        href: href,
                        target: "_blank",
                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_tooltip.Tooltip, {
                            content: value,
                            delayDuration: 0,
                            children: /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
                                className: "orderly-text-base-contrast-54 orderly-border-b-[1px] orderly-border-dashed orderly-border-base-contrast-54 orderly-cursor-pointer",
                                children: (0, _utils.formatTxID)(value)
                            })
                        })
                    });
                }
            },
            {
                title: "Status",
                dataIndex: "trans_status",
                render (value, record, index) {
                    const isEnd = [
                        "COMPLETED",
                        "CANCELED",
                        "FAILED"
                    ].includes(value);
                    return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        className: isEnd ? "orderly-text-base-contrast-54" : "orderly-text-base-contrast-98",
                        children: (0, _utils.upperFirstLetter)(value)
                    });
                }
            },
            {
                title: "Type",
                dataIndex: "side",
                render (value, record, index) {
                    return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        className: value === "DEPOSIT" ? "orderly-text-success-light" : "orderly-text-danger-light",
                        children: (0, _utils.upperFirstLetter)(value)
                    });
                }
            },
            {
                title: "Amount",
                dataIndex: "amount",
                align: "right",
                className: "orderly-w-[120px]",
                render (value, record, index) {
                    const isDeposit = record.side === "DEPOSIT";
                    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                        className: isDeposit ? "orderly-text-success-light" : "orderly-text-danger-light",
                        children: [
                            isDeposit ? "+" : "-",
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Numeral, {
                                children: Math.abs(value)
                            })
                        ]
                    });
                }
            }
        ];
    }, [
        chains
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        className: "orderly-overflow-y-auto orderly-h-[100vh] orderly-pb-[300px]",
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_table.Table, {
                dataSource: dataSource,
                columns: columns,
                loading: isLoading,
                className: "orderly-text-2xs orderly-min-h-[300px]",
                headerClassName: (0, _utils1.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 = AssetHistory;

//# sourceMappingURL=assetHistory.js.map