"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "FavoritesTabPane", {
    enumerable: true,
    get: function() {
        return FavoritesTabPane;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _listview = require("./listview");
const _hooks = require("@orderly.network/hooks");
const _useDataSource = require("../useDataSource");
const _icon = require("../../../icon");
const _lucidereact = require("lucide-react");
const _utils = require("../../../utils");
const _modal = require("../../../modal");
const FavoritesTabPane = (props)=>{
    const { activeIndex, setActiveIndex, onItemClick, fitlerKey } = props;
    const [data, { favorites, addToHistory, favoriteTabs, updateFavoriteTabs, updateSymbolFavoriteState, pinToTop, getLastSelFavTab, updateSelectedFavoriteTab }] = (0, _hooks.useMarkets)(_hooks.MarketsType.FAVORITES);
    const [currTab, setCurrTab] = (0, _react.useState)(getLastSelFavTab || favoriteTabs[0]);
    const clickTab = (tab)=>{
        updateSelectedFavoriteTab(tab);
        setCurrTab(tab);
    };
    const filterData = (0, _react.useMemo)(()=>{
        return favorites?.filter((item)=>{
            const unIgnore = item.tabs.findIndex((tab)=>tab.id === currTab.id) !== -1;
            return unIgnore;
        })?.map((item)=>{
            const index = data.findIndex((symbol)=>symbol.symbol === item.name);
            if (index !== -1) {
                return data[index];
            }
            return null;
        })?.filter((item)=>item);
    }, [
        currTab,
        data,
        favorites
    ]);
    const [dataSource, { onSearch, onSort }] = (0, _useDataSource.useDataSource)(// @ts-ignore
    filterData || []);
    (0, _react.useEffect)(()=>{
        onSearch(fitlerKey);
    }, [
        fitlerKey
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(FavoritesTabList, {
                currTab: currTab,
                setCurrTab: clickTab,
                tabs: favoriteTabs,
                updateFavoriteTabs: updateFavoriteTabs,
                updateSymbolFavoriteState: updateSymbolFavoriteState,
                favorites: favorites
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_listview.ListViewFull, {
                // @ts-ignore
                // ref={listviewRef}
                activeIndex: activeIndex,
                dataSource: dataSource,
                onSort: onSort,
                readLastSortCondition: false,
                maxHeight: props.maxHeight,
                updateActiveIndex: (index)=>setActiveIndex(index),
                // @ts-ignore
                onItemClick: (item)=>{
                    // @ts-ignore
                    onItemClick?.(item);
                    addToHistory(item);
                },
                favoriteTabs: favoriteTabs,
                suffixRender: (item)=>{
                    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                        className: "orderly-inline-flex orderly-h-full orderly-items-start orderly-justify-end",
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsx)("button", {
                                className: "orderly-p-1",
                                onClick: (e)=>{
                                    pinToTop(item);
                                    e.stopPropagation();
                                },
                                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_icon.ArrowTopIcon, {
                                    size: 16,
                                    fill: "current",
                                    fillOpacity: 1,
                                    className: "orderly-fill-white/20 hover:orderly-fill-white/80"
                                })
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)("button", {
                                className: "orderly-p-1",
                                onClick: (e)=>{
                                    updateSymbolFavoriteState(item, currTab, true);
                                    e.stopPropagation();
                                },
                                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_icon.CircleCloseIcon, {
                                    size: 16,
                                    fill: "current",
                                    fillOpacity: 1,
                                    className: "orderly-fill-white/20 hover:orderly-fill-white/80"
                                })
                            })
                        ]
                    });
                }
            })
        ]
    });
};
const FavoritesTabList = (props)=>{
    const [leadingVisible, setLeadingVisible] = (0, _react.useState)(false);
    const [tailingVisible, setTailingVisible] = (0, _react.useState)(false);
    const containerRef = (0, _react.useRef)(null);
    const leadingElementRef = (0, _react.useRef)(null);
    const tailingElementRef = (0, _react.useRef)(null);
    (0, _react.useEffect)(()=>{
        const intersectionObserver = new IntersectionObserver((entries)=>{
            for(let index = 0; index < entries.length; index++){
                const item = entries[index];
                if (item) {
                    if (item.target === leadingElementRef.current) {
                        setLeadingVisible(!item.isIntersecting);
                    }
                    if (item.target === tailingElementRef.current) {
                        setTailingVisible(!item.isIntersecting);
                    }
                }
            }
        });
        if (leadingElementRef.current) {
            intersectionObserver.observe(leadingElementRef.current);
        }
        if (tailingElementRef.current) {
            intersectionObserver.observe(tailingElementRef.current);
        }
        return ()=>{
            intersectionObserver.disconnect();
        };
    }, []);
    const onScollButtonClick = (direction, offset = 100)=>{
        if (direction === "left") containerRef.current?.scrollBy({
            left: -offset,
            behavior: "smooth"
        });
        else containerRef.current?.scrollBy({
            left: offset,
            behavior: "smooth"
        });
    };
    const onClickDeleteTab = (tab)=>{
        _modal.modal.confirm({
            maxWidth: "xs",
            closeableSize: 12,
            // title: "Are you sure you want to delete this watchlist?",
            content: /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: "orderly-pt-0 orderly-text-sm",
                children: "Are you sure you want to delete this watchlist?"
            }),
            onCancel: ()=>{
                return Promise.reject();
            },
            onOk: async ()=>{
                let index = props.tabs.findIndex((item)=>item.id == props.currTab.id);
                if (index === -1) return;
                index++;
                if (index >= props.tabs.length) {
                    index = 0;
                }
                props.updateFavoriteTabs(tab, {
                    delete: true
                });
                [
                    ...props.favorites
                ].forEach((item)=>{
                    const newTabs = item.tabs.filter((value)=>value.id === tab.id);
                    if (newTabs.length !== item.tabs.length) {
                        // @ts-ignore
                        props.updateSymbolFavoriteState({
                            symbol: item.name
                        }, newTabs);
                    }
                });
                props.setCurrTab(props.tabs[index]);
            }
        });
    };
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        className: "orderly-h-[58px] orderly-bg-base-800 orderly-w-full orderly-flex orderly-items-center",
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)("button", {
                className: "orderly-px-6 orderly-h-[58px] orderly-flex orderly-items-center orderly-fill-base-contrast-36 hover:orderly-fill-base-contrast",
                onClick: (e)=>{
                    const newTab = {
                        name: `WatchList_${props.tabs.length}`,
                        id: Date.now()
                    };
                    props.updateFavoriteTabs(newTab, {
                        add: true
                    });
                    props.setCurrTab(newTab);
                    setTimeout(()=>{
                        onScollButtonClick("right", 9999);
                    }, 100);
                },
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_icon.AddIcon, {
                    size: 14,
                    fill: "current",
                    fillOpacity: 1.0
                })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: "orderly-relative orderly-overflow-hidden orderly-h-full orderly-w-full orderly-bg-base-800",
                children: /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                    className: "orderly-flex  orderly-relative orderly-items-center orderly-h-full orderly-overflow-x-auto orderly-hide-scrollbar",
                    ref: containerRef,
                    children: [
                        /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                            ref: leadingElementRef
                        }),
                        props.tabs.map((item)=>/*#__PURE__*/ (0, _jsxruntime.jsx)(FavoriteTabItem, {
                                currTab: props.currTab,
                                setCurrTab: props.setCurrTab,
                                item: item,
                                onClickDeleteTab: onClickDeleteTab,
                                updateFavoriteTabs: props.updateFavoriteTabs
                            })),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                            ref: tailingElementRef
                        })
                    ]
                })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                className: "orderly-flex orderly-items-center orderly-w-[40px] orderly-h-full orderly-mx-2",
                children: [
                    leadingVisible && /*#__PURE__*/ (0, _jsxruntime.jsx)("button", {
                        className: "orderly-flex-1",
                        onClick: ()=>onScollButtonClick("left"),
                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_lucidereact.ChevronLeft, {
                            className: "orderly-text-base-contrast-54 hover:orderly-text-base-contrast-80"
                        })
                    }),
                    tailingVisible && /*#__PURE__*/ (0, _jsxruntime.jsx)("button", {
                        className: "orderly-flex-1",
                        onClick: ()=>onScollButtonClick("right"),
                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_lucidereact.ChevronRight, {
                            className: "orderly-text-base-contrast-54 hover:orderly-text-base-contrast-80"
                        })
                    })
                ]
            })
        ]
    });
};
const FavoriteTabItem = (props)=>{
    const { item, currTab, onClickDeleteTab } = props;
    const [editTab, setEditTab] = (0, _react.useState)(false);
    const [text, setText] = (0, _react.useState)(item.name);
    const elementRef = (0, _react.useRef)(null);
    const [itemW, setItemW] = (0, _react.useState)(null);
    const handleDoubleClick = ()=>{
        setEditTab(true);
    };
    const handleBlur = ()=>{
        setEditTab(false);
        props.updateFavoriteTabs({
            ...props.currTab,
            name: text
        }, {
            update: true
        });
        props.setCurrTab({
            ...props.currTab,
            name: text
        });
        setItemW(null);
    };
    const handleKeyDown = (event)=>{
        if (event.key === 'Enter') {
            setEditTab(false);
            props.updateFavoriteTabs({
                ...props.currTab,
                name: text
            }, {
                update: true
            });
            props.setCurrTab({
                ...props.currTab,
                name: text
            });
            setItemW(null);
        }
    };
    const handleChange = (event)=>{
        setText(event.target.value);
    };
    const canDel = item.id !== 1 && item.id === props.currTab.id;
    (0, _react.useEffect)(()=>{
        if (elementRef.current) {
            // @ts-ignore
            const width = elementRef.current.getBoundingClientRect().width;
            setItemW(width + (item.id !== 1 ? 14 : 0));
        }
    }, []);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("button", {
        ref: elementRef,
        className: (0, _utils.cn)("orderly-flex orderly-items-center orderly-text-xs orderly-pl-3 orderly-rounded-t-lg orderly-bg-base-800 orderly-h-full orderly-text-base-contrast-54", props.currTab.id === item.id && "orderly-bg-base-900 orderly-text-base-contrast"),
        onClick: (e)=>{
            props.setCurrTab(item);
        },
        onDoubleClick: handleDoubleClick,
        style: itemW ? {
            width: `${itemW}px`
        } : {},
        children: [
            editTab ? /*#__PURE__*/ (0, _jsxruntime.jsx)("input", {
                type: "text",
                value: text,
                onChange: handleChange,
                onBlur: handleBlur,
                onKeyDown: handleKeyDown,
                autoFocus: true,
                disabled: !editTab,
                className: "orderly-bg-transparent orderly-outline-none orderly-w-[calc(100%-30px)]"
            }) : item.name,
            canDel ? /*#__PURE__*/ (0, _jsxruntime.jsx)("button", {
                onClick: ()=>onClickDeleteTab(item),
                className: "orderly-pl-1 orderly-pr-1",
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_lucidereact.X, {
                    size: 14
                })
            }) : /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: (0, _utils.cn)("orderly-pl-1 orderly-pr-1 orderly-w-[14px] orderly-h-full", item.id === 1 && "orderly-w-0")
            })
        ]
    });
};

//# sourceMappingURL=favoritesTabPane.js.map