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