import { jsx } from "react/jsx-runtime";
import react, { createContext, forwardRef, useContext, useMemo } from "react";
import { observer, useField, useFieldSchema } from "@tachybase/schema";
import { TinyColor } from "@ctrl/tinycolor";
import { useDraggable, useDroppable } from "@dnd-kit/core";
import { useMergeRefs } from "@floating-ui/react";
import classnames from "classnames";
import { useToken } from "../../antd/__builtins__/index.mjs";
const DraggableContext = /*#__PURE__*/ createContext(null);
DraggableContext.displayName = 'DraggableContext';
const SortableContext = /*#__PURE__*/ createContext(null);
SortableContext.displayName = 'SortableContext';
const SortableProvider = (props)=>{
    const { id, data, children } = props;
    const draggable = useDraggable({
        id,
        data
    });
    const droppable = useDroppable({
        id,
        data
    });
    return /*#__PURE__*/ jsx(SortableContext.Provider, {
        value: {
            draggable,
            droppable
        },
        children: children
    });
};
const Sortable = /*#__PURE__*/ forwardRef((props, ref)=>{
    var _draggable_active, _droppable_over;
    const { component, overStyle, style, children, openMode, ...others } = props;
    const { token } = useToken();
    const { draggable, droppable } = useContext(SortableContext);
    const { isOver, setNodeRef } = droppable;
    const mergedRef = useMergeRefs([
        setNodeRef,
        ref
    ]);
    const droppableStyle = {
        ...style
    };
    if (isOver && (null == draggable ? void 0 : null == (_draggable_active = draggable.active) ? void 0 : _draggable_active.id) !== (null == droppable ? void 0 : null == (_droppable_over = droppable.over) ? void 0 : _droppable_over.id)) {
        droppableStyle['a' === component ? 'color' : 'background'] = new TinyColor(token.colorSettings).setAlpha(0.15).toHex8String();
        Object.assign(droppableStyle, overStyle);
    }
    return /*#__PURE__*/ react.createElement(component || 'div', {
        role: 'none',
        ...others,
        className: classnames('tb-sortable-designer', props.className),
        ref: mergedRef,
        style: droppableStyle
    }, children);
});
const useSortableItemProps = (props)=>{
    const id = useSortableItemId(props);
    const schema = useFieldSchema();
    if (props.schema) return {
        ...props,
        id
    };
    return {
        ...props,
        id,
        schema
    };
};
const useSortableItemId = (props)=>{
    const field = useField();
    if (props.id) return props.id;
    return field.address.toString();
};
const SortableItem = observer((props)=>{
    const { schema, id, eid, removeParentsIfNoChildren, ...others } = useSortableItemProps(props);
    const data = useMemo(()=>({
            insertAdjacent: 'afterEnd',
            schema: schema,
            removeParentsIfNoChildren: null != removeParentsIfNoChildren ? removeParentsIfNoChildren : true
        }), [
        schema,
        removeParentsIfNoChildren
    ]);
    return /*#__PURE__*/ jsx(SortableProvider, {
        id: id,
        data: data,
        children: /*#__PURE__*/ jsx(Sortable, {
            id: eid,
            ...others,
            ref: props.ref,
            children: props.children
        })
    });
}, {
    displayName: 'SortableItem',
    forwardRef: true
});
const DragHandler = (props)=>{
    const { draggable } = useContext(SortableContext);
    const { attributes, listeners, setNodeRef } = draggable;
    return /*#__PURE__*/ jsx("div", {
        style: {
            display: 'inline-block',
            width: 14,
            height: 14,
            lineHeight: '14px',
            textAlign: 'left'
        },
        children: /*#__PURE__*/ jsx("div", {
            ref: setNodeRef,
            style: {
                position: 'relative',
                zIndex: 1,
                lineHeight: 0,
                fontSize: 0,
                display: 'inline-block'
            },
            ...listeners,
            ...attributes,
            role: "none",
            children: /*#__PURE__*/ jsx("span", {
                style: {
                    cursor: 'move',
                    fontSize: 14
                },
                children: props.children
            })
        })
    });
};
export { DragHandler, DraggableContext, Sortable, SortableContext, SortableItem, SortableProvider };
