import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useEffect, useMemo, useState } from "react";
import { cn } from "../../../../../../utils/css";
import { ExpandIcon } from "../../../../../../icon/icons/expland";
const timeIntervalMap = [
    [
        {
            value: "3",
            label: "3m"
        },
        {
            value: "5",
            label: "5m"
        },
        {
            value: "30",
            label: "30m"
        },
        {
            value: "120",
            label: "2h"
        }
    ],
    [
        {
            value: "360",
            label: "6h"
        },
        {
            value: "720",
            label: "12h"
        },
        {
            value: "3d",
            label: "3D"
        },
        {
            value: "1M",
            label: "1M"
        }
    ]
];
export default function TimeInterval({ changeTimeInterval, timeInterval }) {
    const [open, setOpen] = useState(false);
    const containerRef = React.useRef(null);
    useEffect(()=>{
        const onTouchStart = (event)=>{
            if (containerRef.current === event.target || containerRef.current?.contains(event.target)) {
                return;
            }
            setOpen(false);
        //   document.removeEventListener("click", onClick);
        };
        const onClick = (event)=>{
            if (containerRef.current === event.target || containerRef.current?.contains(event.target)) {
                return;
            }
            //   event.stopPropagation();
            setOpen(false);
        };
        if (open) {
            //   const onTouchMove = () => {};
            setTimeout(()=>{
                document.addEventListener("touchstart", onTouchStart, {
                    capture: true
                });
                document.addEventListener("click", onClick, {
                    capture: true
                });
            }, 100);
            return ()=>{
                //
                document.removeEventListener("touchstart", onTouchStart);
                document.removeEventListener("click", onClick);
            };
        } else {
            document.removeEventListener("touchstart", onTouchStart);
            document.removeEventListener("click", onClick);
        }
    }, [
        open
    ]);
    const currentIntervalIsInExpand = useMemo(()=>{
        for (const row of timeIntervalMap){
            for (const item of row){
                if (item.value === timeInterval) {
                    return item.label;
                }
            }
        }
        return null;
    }, [
        timeInterval
    ]);
    useEffect(()=>{}, [
        timeInterval
    ]);
    return /*#__PURE__*/ _jsxs("div", {
        ref: containerRef,
        children: [
            /*#__PURE__*/ _jsxs("button", {
                className: cn("orderly-flex orderly-items-center ", (open || currentIntervalIsInExpand) && "orderly-text-base-contrast"),
                onClick: ()=>setOpen((open)=>!open),
                children: [
                    /*#__PURE__*/ _jsx("div", {
                        children: currentIntervalIsInExpand ?? "More"
                    }),
                    /*#__PURE__*/ _jsx(ExpandIcon, {
                        size: 16,
                        className: cn("orderly-transition-transform", open && "orderly-rotate-180")
                    })
                ]
            }),
            open && /*#__PURE__*/ _jsx("div", {
                "data-state": open ? "open" : "closed",
                className: cn("orderly-z-50 orderly-min-w-[8rem] orderly-overflow-hidden orderly-rounded orderly-bg-base-800 orderly-px-4 orderly-py-3 orderly-text-popover-foreground orderly-shadow-md orderly-absolute orderly-left-0 orderly-top-full orderly-w-full orderly-space-y-1", "data-[state=open]:orderly-animate-in data-[state=closed]:orderly-animate-out data-[state=closed]:orderly-fade-out-0 data-[state=open]:orderly-fade-in-0 data-[state=closed]:orderly-zoom-out-95 data-[state=open]:orderly-zoom-in-95 data-[side=bottom]:orderly-slide-in-from-top-2 data-[side=left]:orderly-slide-in-from-right-2 data-[side=right]:orderly-slide-in-from-left-2 data-[side=top]:orderly-slide-in-from-bottom-2"),
                children: /*#__PURE__*/ _jsx("div", {
                    className: "orderly-flex orderly-flex-col orderly-gap-3",
                    children: timeIntervalMap.map((row, id)=>/*#__PURE__*/ _jsx("div", {
                            className: "orderly-flex orderly-flex-row orderly-gap-3",
                            children: row.map((item)=>/*#__PURE__*/ _jsx("div", {
                                    className: cn("orderly-text-3xs orderly-relative orderly-flex orderly-items-center orderly-justify-center orderly-w-[50%] orderly-bg-base-600 orderly-h-8 orderly-text-base-contrast-36 orderly-rounded-md", item.value === timeInterval && "orderly-text-base-contrast orderly-border orderly-border-base-contrast"),
                                    onClick: ()=>{
                                        changeTimeInterval(item.value);
                                    },
                                    children: /*#__PURE__*/ _jsx("div", {
                                        children: item.label
                                    })
                                }, item.value))
                        }, id))
                })
            })
        ]
    });
}

//# sourceMappingURL=timeInterval.js.map