import { jsx, jsxs } from "react/jsx-runtime";
import { useContext, useState } from "react";
import { observer } from "@tachybase/schema";
import { DndContext, DragOverlay, MouseSensor, useDraggable, useDroppable, useSensor, useSensors } from "@dnd-kit/core";
import { css } from "@emotion/css";
import { Button } from "antd";
import classnames from "classnames";
import { useNavigate } from "react-router";
import { useLocation } from "react-router-dom";
import { Icon } from "../../icon/index.mjs";
import { PageStyleContext } from "./PageStyle.provider.mjs";
import { useStyles } from "./TabHeader.style.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n          color: rgba(255, 255, 255, 0.88);\n        "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const Tag = (param)=>{
    let { onClick, onClose, children, active } = param;
    const { styles } = useStyles();
    return /*#__PURE__*/ jsxs("span", {
        onClick: onClick,
        className: classnames(styles.tabHeader, {
            active
        }),
        children: [
            /*#__PURE__*/ jsx("span", {
                className: "tab-text",
                children: children
            }),
            /*#__PURE__*/ jsx(Button, {
                className: css(_templateObject()),
                type: "text",
                onClick: onClose,
                icon: /*#__PURE__*/ jsx(Icon, {
                    type: "CloseOutlined"
                })
            })
        ]
    });
};
const TabHeader = ()=>{
    const navigate = useNavigate();
    const location = useLocation();
    const { items, setItems } = useContext(PageStyleContext);
    const targetKey = location.pathname;
    const { styles } = useStyles();
    return /*#__PURE__*/ jsx("div", {
        className: styles.tabWrapper,
        children: /*#__PURE__*/ jsx(DndProvider, {
            targetKey: targetKey,
            navigate: navigate,
            setItems: setItems,
            items: items,
            children: items.map((item)=>/*#__PURE__*/ jsx(TabTitle, {
                    item: item,
                    targetKey: targetKey,
                    navigate: navigate,
                    setItems: setItems
                }))
        })
    });
};
const TabTag = (props)=>{
    const { item, targetKey, navigate, setItems } = props;
    return /*#__PURE__*/ jsx(Tag, {
        active: item.key === targetKey,
        onClick: ()=>{
            navigate(item.key);
        },
        onClose: (e)=>{
            e.stopPropagation();
            setItems((items)=>items.filter((i)=>i.key !== item.key));
        },
        children: item.label
    }, item.key);
};
const DndProvider = observer((props)=>{
    const { targetKey, navigate, setItems, items } = props;
    const [activeTab, setActiveId] = useState(null);
    const onDragEnd = async (props)=>{
        const { active, over } = props;
        setTimeout(()=>{
            setActiveId(null);
        });
        if (over && over.id !== active.id) {
            const newItems = [
                ...items
            ];
            const activeIndex = newItems.findIndex((item)=>item.key === active.id);
            const overIndex = newItems.findIndex((item)=>item.key === over.id);
            const item = newItems.splice(activeIndex, 1);
            newItems.splice(overIndex, 0, ...item);
            setItems(newItems);
        }
    };
    function onDragStart(event) {
        var _event_active;
        setActiveId(null == (_event_active = event.active) ? void 0 : _event_active.data.current);
    }
    const mouseSensor = useSensor(MouseSensor, {
        activationConstraint: {
            distance: 10
        }
    });
    const sensors = useSensors(mouseSensor);
    return /*#__PURE__*/ jsxs(DndContext, {
        sensors: sensors,
        onDragEnd: onDragEnd,
        onDragStart: onDragStart,
        children: [
            props.children,
            /*#__PURE__*/ jsx(DragOverlay, {
                children: activeTab ? /*#__PURE__*/ jsx("span", {
                    style: {
                        whiteSpace: 'nowrap'
                    },
                    children: /*#__PURE__*/ jsx(TabTag, {
                        item: activeTab,
                        targetKey: targetKey,
                        navigate: navigate,
                        setItems: setItems
                    })
                }) : null
            })
        ]
    });
}, {
    displayName: 'DndProvider'
});
const Draggable = (props)=>{
    const { attributes, listeners, setNodeRef } = useDraggable({
        id: props.id,
        data: props.data
    });
    return /*#__PURE__*/ jsx("div", {
        ref: setNodeRef,
        ...listeners,
        ...attributes,
        children: /*#__PURE__*/ jsx("div", {
            children: props.children
        })
    });
};
function Droppable(props) {
    const { isOver, setNodeRef } = useDroppable({
        id: props.id,
        data: props.data
    });
    const style = isOver ? {
        color: 'green'
    } : void 0;
    return /*#__PURE__*/ jsx("div", {
        ref: setNodeRef,
        style: style,
        children: props.children
    });
}
const TabTitle = observer((props)=>{
    const { item, targetKey, navigate, setItems } = props;
    return /*#__PURE__*/ jsx(Droppable, {
        id: item.key,
        data: item,
        children: /*#__PURE__*/ jsx("div", {
            children: /*#__PURE__*/ jsx(Draggable, {
                id: item.key,
                data: item,
                children: /*#__PURE__*/ jsx(TabTag, {
                    item: item,
                    targetKey: targetKey,
                    navigate: navigate,
                    setItems: setItems
                })
            })
        })
    });
}, {
    displayName: 'TabTitle'
});
export { TabHeader, Tag };
