import { jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { observer } from "@tachybase/schema";
import { DndContext, DragOverlay, rectIntersection } from "@dnd-kit/core";
import { useTranslation } from "react-i18next";
import { useAPIClient } from "../../../index.mjs";
import { createDesignable, useDesignable } from "../../hooks/index.mjs";
const useDragEnd = (props)=>{
    const { refresh } = useDesignable();
    const api = useAPIClient();
    const { t } = useTranslation();
    return (event)=>{
        var _active_data_current, _active_data, _over_data_current, _over_data, _over_data_current1, _over_data1, _over_data_current2, _over_data2, _over_data_current3, _over_data3, _over_data_current4, _over_data4, _over_data_current5, _over_data5;
        const { active, over } = event;
        const activeSchema = null == active ? void 0 : null == (_active_data = active.data) ? void 0 : null == (_active_data_current = _active_data.current) ? void 0 : _active_data_current.schema;
        const overSchema = null == over ? void 0 : null == (_over_data = over.data) ? void 0 : null == (_over_data_current = _over_data.current) ? void 0 : _over_data_current.schema;
        const insertAdjacent = null == over ? void 0 : null == (_over_data1 = over.data) ? void 0 : null == (_over_data_current1 = _over_data1.current) ? void 0 : _over_data_current1.insertAdjacent;
        const breakRemoveOn = null == over ? void 0 : null == (_over_data2 = over.data) ? void 0 : null == (_over_data_current2 = _over_data2.current) ? void 0 : _over_data_current2.breakRemoveOn;
        const wrapSchema = null == over ? void 0 : null == (_over_data3 = over.data) ? void 0 : null == (_over_data_current3 = _over_data3.current) ? void 0 : _over_data_current3.wrapSchema;
        const onSuccess = null == over ? void 0 : null == (_over_data4 = over.data) ? void 0 : null == (_over_data_current4 = _over_data4.current) ? void 0 : _over_data_current4.onSuccess;
        var _over_data_current_removeParentsIfNoChildren;
        const removeParentsIfNoChildren = null != (_over_data_current_removeParentsIfNoChildren = null == over ? void 0 : null == (_over_data5 = over.data) ? void 0 : null == (_over_data_current5 = _over_data5.current) ? void 0 : _over_data_current5.removeParentsIfNoChildren) ? _over_data_current_removeParentsIfNoChildren : true;
        if (!activeSchema || !overSchema) {
            var _props_onDragEnd;
            null == props || null == (_props_onDragEnd = props.onDragEnd) || _props_onDragEnd.call(props, event);
            return;
        }
        if (activeSchema === overSchema) {
            var _props_onDragEnd1;
            null == props || null == (_props_onDragEnd1 = props.onDragEnd) || _props_onDragEnd1.call(props, event);
            return;
        }
        if (activeSchema.parent === overSchema && 'beforeEnd' === insertAdjacent) {
            var _props_onDragEnd2;
            null == props || null == (_props_onDragEnd2 = props.onDragEnd) || _props_onDragEnd2.call(props, event);
            return;
        }
        const dn = createDesignable({
            t,
            api,
            refresh,
            current: overSchema
        });
        dn.loadAPIClientEvents();
        if (activeSchema.parent === overSchema.parent) {
            var _props_onDragEnd3;
            dn.insertBeforeBeginOrAfterEnd(activeSchema);
            null == props || null == (_props_onDragEnd3 = props.onDragEnd) || _props_onDragEnd3.call(props, event);
            return;
        }
        if (insertAdjacent) {
            var _props_onDragEnd4;
            dn.insertAdjacent(insertAdjacent, activeSchema, {
                wrap: wrapSchema,
                breakRemoveOn,
                removeParentsIfNoChildren,
                onSuccess
            });
            null == props || null == (_props_onDragEnd4 = props.onDragEnd) || _props_onDragEnd4.call(props, event);
            return;
        }
    };
};
const dnd_context_DndContext = observer((props)=>{
    const { t } = useTranslation();
    const [visible, setVisible] = useState(true);
    return /*#__PURE__*/ jsxs(DndContext, {
        collisionDetection: rectIntersection,
        ...props,
        onDragStart: (event)=>{
            var _active_data_current, _active_data;
            const { active } = event;
            const activeSchema = null == active ? void 0 : null == (_active_data = active.data) ? void 0 : null == (_active_data_current = _active_data.current) ? void 0 : _active_data_current.schema;
            setVisible(!!activeSchema);
            if (null == props ? void 0 : props.onDragStart) {
                var _props_onDragStart;
                null == props || null == (_props_onDragStart = props.onDragStart) || _props_onDragStart.call(props, event);
            }
        },
        onDragEnd: useDragEnd(props),
        children: [
            /*#__PURE__*/ jsx(DragOverlay, {
                dropAnimation: {
                    duration: 10,
                    easing: 'cubic-bezier(0.18, 0.67, 0.6, 1.22)'
                }
            }),
            props.children
        ]
    });
}, {
    displayName: 'DndContext'
});
export { dnd_context_DndContext as DndContext };
