import { inject, onScopeDispose, ref } from "vue";
const DraggableSortableGroupKey = Symbol.for("veno-ui:draggable-sortable-group");
function createDraggableSortableGroup() {
  const items = /* @__PURE__ */ new Map();
  const swapping = ref(false);
  const find = (targetId, targetEl) => {
    for (const [id, item] of items) {
      if (id !== targetId) {
        const res = item.findContains(targetEl);
        if (res) {
          return [item, id, res.key, res.el];
        }
      }
    }
    return [void 0, void 0, void 0, void 0];
  };
  return {
    register: (id, item) => items.set(id, item),
    unregister: (id) => items.delete(id),
    trySwap: async (from, to) => {
      if (swapping.value)
        return;
      const [toItem, toParentId, toParentKey] = find(from.id, to.el);
      if (from.id === toParentId || !toItem?.group.value)
        return;
      const fromItem = items.get(from.id);
      if (!fromItem?.group.value || toItem.group.value !== fromItem.group.value)
        return;
      swapping.value = true;
      const value = fromItem.clone(from.key);
      if (fromItem.put.value && toItem.put.value) {
        await fromItem.leave(from.key);
      }
      if (toItem.put.value) {
        await toItem.enter(toParentKey, value, from);
      }
      swapping.value = false;
    }
  };
}
function useDraggableSortableGroupItem(id, item) {
  const group = inject(DraggableSortableGroupKey);
  group?.register(id, item);
  onScopeDispose(() => {
    group?.unregister(id);
  });
  return group;
}
export {
  DraggableSortableGroupKey,
  createDraggableSortableGroup,
  useDraggableSortableGroupItem
};
//# sourceMappingURL=index.mjs.map
