import { WeakStore as ye, reactive as Y } from "../../../../@dnd-kit_state@0.1.21/node_modules/@dnd-kit/state/dist/index.js";
import { defaultCollisionDetection as me, closestCorners as Oe } from "../../../../@dnd-kit_collision@0.1.21/node_modules/@dnd-kit/collision/dist/index.js";
import { Droppable as we, Draggable as Ie, Scroller as Se } from "./index.js";
import { ProxiedElements as Pe, isKeyboardEvent as Z, DOMRectangle as ee, getVisibleBoundingRectangle as De, scrollIntoViewIfNeeded as Ee, getComputedStyles as ke, computeTranslate as te, animateTransform as Ge } from "./utilities.js";
import { Plugin as ce } from "../../../../@dnd-kit_abstract@0.1.21/node_modules/@dnd-kit/abstract/index.js";
import { Rectangle as re } from "../../../../@dnd-kit_geometry@0.1.21/node_modules/@dnd-kit/geometry/dist/index.js";
import { batch as O, untracked as ne, effect as Te } from "../../../../@preact_signals-core@1.12.0/node_modules/@preact/signals-core/dist/signals-core.module.js";
var je = Object.create, ue = Object.defineProperty, Me = Object.defineProperties, Ae = Object.getOwnPropertyDescriptor, Ce = Object.getOwnPropertyDescriptors, K = Object.getOwnPropertySymbols, pe = Object.prototype.hasOwnProperty, ge = Object.prototype.propertyIsEnumerable, Re = (e, t) => (t = Symbol[e]) ? t : Symbol.for("Symbol." + e), B = (e) => {
  throw TypeError(e);
}, H = (e, t, r) => t in e ? ue(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r, ze = (e, t) => {
  for (var r in t || (t = {}))
    pe.call(t, r) && H(e, r, t[r]);
  if (K)
    for (var r of K(t))
      ge.call(t, r) && H(e, r, t[r]);
  return e;
}, We = (e, t) => Me(e, Ce(t)), $e = (e, t) => {
  var r = {};
  for (var n in e)
    pe.call(e, n) && t.indexOf(n) < 0 && (r[n] = e[n]);
  if (e != null && K)
    for (var n of K(e))
      t.indexOf(n) < 0 && ge.call(e, n) && (r[n] = e[n]);
  return r;
}, Be = (e) => {
  var t;
  return [, , , je((t = void 0) != null ? t : null)];
}, fe = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"], R = (e) => e !== void 0 && typeof e != "function" ? B("Function expected") : e, Le = (e, t, r, n, i) => ({ kind: fe[e], name: t, metadata: n, addInitializer: (l) => r._ ? B("Already initialized") : i.push(R(l || null)) }), Ne = (e, t) => H(t, Re("metadata"), e[3]), N = (e, t, r, n) => {
  for (var i = 0, l = e[t >> 1], s = l && l.length; i < s; i++) t & 1 ? l[i].call(r) : n = l[i].call(r, n);
  return n;
}, he = (e, t, r, n, i, l) => {
  for (var s, o, h, g, u, a = t & 7, d = !1, p = !1, f = e.length + 1, _ = fe[a + 5], x = e[f - 1] = [], m = e[f] || (e[f] = []), y = (i = i.prototype, Ae({ get [r]() {
    return z(this, l);
  }, set [r](v) {
    return k(this, l, v);
  } }, r)), b = n.length - 1; b >= 0; b--)
    g = Le(a, r, h = {}, e[3], m), g.static = d, g.private = p, u = g.access = { has: (v) => r in v }, u.get = (v) => v[r], u.set = (v, c) => v[r] = c, o = (0, n[b])({ get: y.get, set: y.set }, g), h._ = 1, o === void 0 ? R(o) && (y[_] = o) : typeof o != "object" || o === null ? B("Object expected") : (R(s = o.get) && (y.get = s), R(s = o.set) && (y.set = s), R(s = o.init) && x.unshift(s));
  return y && ue(i, r, y), i;
}, be = (e, t, r) => t.has(e) || B("Cannot " + r), z = (e, t, r) => (be(e, t, "read from private field"), t.get(e)), C = (e, t, r) => t.has(e) ? B("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(e) : t.set(e, r), k = (e, t, r, n) => (be(e, t, "write to private field"), t.set(e, r), r);
function G(e) {
  return e instanceof X || e instanceof xe;
}
var V = 10, Ve = class extends ce {
  constructor(e) {
    super(e);
    const t = Te(() => {
      const { dragOperation: n } = e;
      if (Z(n.activatorEvent) && G(n.source) && n.status.initialized) {
        const i = e.registry.plugins.get(Se);
        if (i)
          return i.disable(), () => i.enable();
      }
    }), r = e.monitor.addEventListener(
      "dragmove",
      (n, i) => {
        queueMicrotask(() => {
          if (this.disabled || n.defaultPrevented || !n.nativeEvent)
            return;
          const { dragOperation: l } = i;
          if (!Z(n.nativeEvent) || !G(l.source) || !l.shape)
            return;
          const { actions: s, collisionObserver: o, registry: h } = i, { by: g } = n;
          if (!g)
            return;
          const u = qe(g), { source: a, target: d } = l, { center: p } = l.shape.current, f = [], _ = [];
          O(() => {
            for (const c of h.droppables) {
              const { id: E } = c;
              if (!c.accepts(a) || E === d?.id && G(c) || !c.element)
                continue;
              let I = c.shape;
              const w = new ee(c.element, {
                getBoundingClientRect: (j) => De(j, void 0, 0.2)
              });
              !w.height || !w.width || (u == "down" && p.y + V < w.center.y || u == "up" && p.y - V > w.center.y || u == "left" && p.x - V > w.center.x || u == "right" && p.x + V < w.center.x) && (f.push(c), c.shape = w, _.push(() => c.shape = I));
            }
          }), n.preventDefault(), o.disable();
          const x = o.computeCollisions(
            f,
            Oe
          );
          O(() => _.forEach((c) => c()));
          const [m] = x;
          if (!m)
            return;
          const { id: y } = m, { index: b, group: v } = a.sortable;
          s.setDropTarget(y).then(() => {
            const { source: c, target: E, shape: I } = l;
            if (!c || !G(c) || !I)
              return;
            const {
              index: w,
              group: j,
              target: P
            } = c.sortable, S = b !== w || v !== j, M = S ? P : E?.element;
            if (!M) return;
            Ee(M);
            const L = new ee(M);
            if (!L)
              return;
            const A = re.delta(
              L,
              re.from(I.current.boundingRectangle),
              c.alignment
            );
            s.move({
              by: A
            }), S ? s.setDropTarget(c.id).then(() => o.enable()) : o.enable();
          });
        });
      }
    );
    this.destroy = () => {
      r(), t();
    };
  }
};
function qe(e) {
  const { x: t, y: r } = e;
  if (t > 0)
    return "right";
  if (t < 0)
    return "left";
  if (r > 0)
    return "down";
  if (r < 0)
    return "up";
}
var Fe = Object.defineProperty, Ke = Object.defineProperties, He = Object.getOwnPropertyDescriptors, se = Object.getOwnPropertySymbols, Je = Object.prototype.hasOwnProperty, Qe = Object.prototype.propertyIsEnumerable, ie = (e, t, r) => t in e ? Fe(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r, oe = (e, t) => {
  for (var r in t || (t = {}))
    Je.call(t, r) && ie(e, r, t[r]);
  if (se)
    for (var r of se(t))
      Qe.call(t, r) && ie(e, r, t[r]);
  return e;
}, ae = (e, t) => Ke(e, He(t));
function Ue(e, t, r) {
  if (t === r)
    return e;
  const n = e.slice();
  return n.splice(r, 0, n.splice(t, 1)[0]), n;
}
function Xe(e, t, r) {
  var n, i;
  const { source: l, target: s, canceled: o } = t.operation;
  if (!l || !s || o)
    return "preventDefault" in t && t.preventDefault(), e;
  const h = (b, v) => b === v || typeof b == "object" && "id" in b && b.id === v;
  if (Array.isArray(e)) {
    const b = e.findIndex((c) => h(c, l.id)), v = e.findIndex((c) => h(c, s.id));
    if (b === -1 || v === -1)
      return e;
    if (!o && "index" in l && typeof l.index == "number") {
      const c = l.index;
      if (c !== b)
        return r(e, b, c);
    }
    return r(e, b, v);
  }
  const g = Object.entries(e);
  let u = -1, a, d = -1, p;
  for (const [b, v] of g)
    if (u === -1 && (u = v.findIndex((c) => h(c, l.id)), u !== -1 && (a = b)), d === -1 && (d = v.findIndex((c) => h(c, s.id)), d !== -1 && (p = b)), u !== -1 && d !== -1)
      break;
  if (!l.manager) return e;
  const { dragOperation: f } = l.manager, _ = (i = (n = f.shape) == null ? void 0 : n.current.center) != null ? i : f.position.current;
  if (p == null && s.id in e) {
    const b = s.shape && _.y > s.shape.center.y ? e[s.id].length : 0;
    p = s.id, d = b;
  }
  if (a == null || p == null || a === p && u === d)
    return "preventDefault" in t && t.preventDefault(), e;
  if (a === p)
    return ae(oe({}, e), {
      [a]: r(e[a], u, d)
    });
  const m = s.shape && Math.round(_.y) > Math.round(s.shape.center.y) ? 1 : 0, y = e[a][u];
  return ae(oe({}, e), {
    [a]: [
      ...e[a].slice(0, u),
      ...e[a].slice(u + 1)
    ],
    [p]: [
      ...e[p].slice(0, d + m),
      y,
      ...e[p].slice(d + m)
    ]
  });
}
function Ye(e, t) {
  return Xe(e, t, Ue);
}
var le = "__default__", Ze = class extends ce {
  constructor(e) {
    super(e);
    const t = () => {
      const n = /* @__PURE__ */ new Map();
      for (const i of e.registry.droppables)
        if (i instanceof X) {
          const { sortable: l } = i, { group: s } = l;
          let o = n.get(s);
          o || (o = /* @__PURE__ */ new Set(), n.set(s, o)), o.add(l);
        }
      for (const [i, l] of n)
        n.set(i, new Set(q(l)));
      return n;
    }, r = [
      e.monitor.addEventListener("dragover", (n, i) => {
        if (this.disabled)
          return;
        const { dragOperation: l } = i, { source: s, target: o } = l;
        if (!G(s) || !G(o) || s.sortable === o.sortable)
          return;
        const h = t(), g = s.sortable.group === o.sortable.group, u = h.get(s.sortable.group), a = g ? u : h.get(o.sortable.group);
        !u || !a || queueMicrotask(() => {
          n.defaultPrevented || i.renderer.rendering.then(() => {
            var d, p, f;
            const _ = t();
            for (const [P, S] of h.entries()) {
              const M = Array.from(S).entries();
              for (const [L, A] of M)
                if (A.index !== L || A.group !== P || !((d = _.get(P)) != null && d.has(A)))
                  return;
            }
            const x = s.sortable.element, m = o.sortable.element;
            if (!m || !x || !g && o.id === s.sortable.group)
              return;
            const y = q(u), b = g ? y : q(a), v = (p = s.sortable.group) != null ? p : le, c = (f = o.sortable.group) != null ? f : le, E = {
              [v]: y,
              [c]: b
            }, I = Ye(E, n);
            if (E === I) return;
            const w = I[c].indexOf(s.sortable), j = I[c].indexOf(o.sortable);
            i.collisionObserver.disable(), de(x, w, m, j), O(() => {
              for (const [P, S] of I[v].entries())
                S.index = P;
              if (!g)
                for (const [P, S] of I[c].entries())
                  S.group = o.sortable.group, S.index = P;
            }), i.actions.setDropTarget(s.id).then(() => i.collisionObserver.enable());
          });
        });
      }),
      e.monitor.addEventListener("dragend", (n, i) => {
        if (!n.canceled)
          return;
        const { dragOperation: l } = i, { source: s } = l;
        G(s) && (s.sortable.initialIndex === s.sortable.index && s.sortable.initialGroup === s.sortable.group || queueMicrotask(() => {
          const o = t(), h = o.get(
            s.sortable.initialGroup
          );
          h && i.renderer.rendering.then(() => {
            for (const [p, f] of o.entries()) {
              const _ = Array.from(f).entries();
              for (const [x, m] of _)
                if (m.index !== x || m.group !== p)
                  return;
            }
            const g = q(h), u = s.sortable.element, a = g[s.sortable.initialIndex], d = a?.element;
            !a || !d || !u || (de(u, a.index, d, s.index), O(() => {
              for (const [p, f] of o.entries()) {
                const _ = Array.from(f).values();
                for (const x of _)
                  x.index = x.initialIndex, x.group = x.initialGroup;
              }
            }));
          });
        }));
      })
    ];
    this.destroy = () => {
      for (const n of r)
        n();
    };
  }
};
function de(e, t, r, n) {
  const i = n < t ? "afterend" : "beforebegin";
  r.insertAdjacentElement(i, e);
}
function et(e, t) {
  return e.index - t.index;
}
function q(e) {
  return Array.from(e).sort(et);
}
var tt = [
  Ve,
  Ze
], rt = {
  duration: 250,
  easing: "cubic-bezier(0.25, 1, 0.5, 1)",
  idle: !1
}, F = new ye(), ve, _e, D, J, W, $, Q, T;
_e = [Y], ve = [Y];
var U = class {
  constructor(e, t) {
    C(this, J, N(D, 8, this)), N(D, 11, this), C(this, W), C(this, $), C(this, Q, N(D, 12, this)), N(D, 15, this), C(this, T), this.register = () => (O(() => {
      var a, d;
      (a = this.manager) == null || a.registry.register(this.droppable), (d = this.manager) == null || d.registry.register(this.draggable);
    }), () => this.unregister()), this.unregister = () => {
      O(() => {
        var a, d;
        (a = this.manager) == null || a.registry.unregister(this.droppable), (d = this.manager) == null || d.registry.unregister(this.draggable);
      });
    }, this.destroy = () => {
      O(() => {
        this.droppable.destroy(), this.draggable.destroy();
      });
    };
    var r = e, {
      effects: n = () => [],
      group: i,
      index: l,
      sensors: s,
      type: o,
      transition: h = rt,
      plugins: g = tt
    } = r, u = $e(r, [
      "effects",
      "group",
      "index",
      "sensors",
      "type",
      "transition",
      "plugins"
    ]);
    this.droppable = new X(u, t, this), this.draggable = new xe(
      We(ze({}, u), {
        effects: () => [
          () => {
            var a, d, p;
            const f = (a = this.manager) == null ? void 0 : a.dragOperation.status;
            f?.initializing && this.id === ((p = (d = this.manager) == null ? void 0 : d.dragOperation.source) == null ? void 0 : p.id) && F.clear(this.manager), f?.dragging && F.set(
              this.manager,
              this.id,
              ne(() => ({
                initialIndex: this.index,
                initialGroup: this.group
              }))
            );
          },
          () => {
            const { index: a, group: d, manager: p } = this, f = z(this, $), _ = z(this, W);
            (a !== f || d !== _) && (k(this, $, a), k(this, W, d), this.animate());
          },
          () => {
            const { target: a } = this, { feedback: d, isDragSource: p } = this.draggable;
            d == "move" && p && (this.droppable.disabled = !a);
          },
          () => {
            const { manager: a } = this;
            for (const d of g)
              a?.registry.register(d);
          },
          ...n()
        ],
        type: o,
        sensors: s
      }),
      t,
      this
    ), k(this, T, u.element), this.manager = t, this.index = l, k(this, $, l), this.group = i, k(this, W, i), this.type = o, this.transition = h;
  }
  get initialIndex() {
    var e, t;
    return (t = (e = F.get(this.manager, this.id)) == null ? void 0 : e.initialIndex) != null ? t : this.index;
  }
  get initialGroup() {
    var e, t;
    return (t = (e = F.get(this.manager, this.id)) == null ? void 0 : e.initialGroup) != null ? t : this.group;
  }
  animate() {
    ne(() => {
      const { manager: e, transition: t } = this, { shape: r } = this.droppable;
      if (!e) return;
      const { idle: n } = e.dragOperation.status;
      !r || !t || n && !t.idle || e.renderer.rendering.then(() => {
        const { element: i } = this;
        if (!i)
          return;
        const l = this.refreshShape();
        if (!l)
          return;
        const s = {
          x: r.boundingRectangle.left - l.boundingRectangle.left,
          y: r.boundingRectangle.top - l.boundingRectangle.top
        }, { translate: o } = ke(i), h = te(i, o, !1), g = te(i, o);
        (s.x || s.y) && Ge({
          element: i,
          keyframes: {
            translate: [
              `${h.x + s.x}px ${h.y + s.y}px ${h.z}`,
              `${g.x}px ${g.y}px ${g.z}`
            ]
          },
          options: t
        }).then(() => {
          e.dragOperation.status.dragging || (this.droppable.shape = void 0);
        });
      });
    });
  }
  get manager() {
    return this.draggable.manager;
  }
  set manager(e) {
    O(() => {
      this.draggable.manager = e, this.droppable.manager = e;
    });
  }
  set element(e) {
    O(() => {
      const t = z(this, T), r = this.droppable.element, n = this.draggable.element;
      (!r || r === t) && (this.droppable.element = e), (!n || n === t) && (this.draggable.element = e), k(this, T, e);
    });
  }
  get element() {
    var e, t;
    const r = z(this, T);
    if (r)
      return (t = (e = Pe.get(r)) != null ? e : r) != null ? t : this.droppable.element;
  }
  set target(e) {
    this.droppable.element = e;
  }
  get target() {
    return this.droppable.element;
  }
  set source(e) {
    this.draggable.element = e;
  }
  get source() {
    return this.draggable.element;
  }
  get disabled() {
    return this.draggable.disabled && this.droppable.disabled;
  }
  set feedback(e) {
    this.draggable.feedback = e;
  }
  set disabled(e) {
    O(() => {
      this.droppable.disabled = e, this.draggable.disabled = e;
    });
  }
  set data(e) {
    O(() => {
      this.droppable.data = e, this.draggable.data = e;
    });
  }
  set handle(e) {
    this.draggable.handle = e;
  }
  set id(e) {
    O(() => {
      this.droppable.id = e, this.draggable.id = e;
    });
  }
  get id() {
    return this.droppable.id;
  }
  set sensors(e) {
    this.draggable.sensors = e;
  }
  set modifiers(e) {
    this.draggable.modifiers = e;
  }
  set collisionPriority(e) {
    this.droppable.collisionPriority = e;
  }
  set collisionDetector(e) {
    this.droppable.collisionDetector = e ?? me;
  }
  set alignment(e) {
    this.draggable.alignment = e;
  }
  get alignment() {
    return this.draggable.alignment;
  }
  set type(e) {
    O(() => {
      this.droppable.type = e, this.draggable.type = e;
    });
  }
  get type() {
    return this.draggable.type;
  }
  set accept(e) {
    this.droppable.accept = e;
  }
  get accept() {
    return this.droppable.accept;
  }
  get isDropTarget() {
    return this.droppable.isDropTarget;
  }
  /**
   * A boolean indicating whether the sortable item is the source of a drag operation.
   */
  get isDragSource() {
    return this.draggable.isDragSource;
  }
  /**
   * A boolean indicating whether the sortable item is being dragged.
   */
  get isDragging() {
    return this.draggable.isDragging;
  }
  /**
   * A boolean indicating whether the sortable item is being dropped.
   */
  get isDropping() {
    return this.draggable.isDropping;
  }
  get status() {
    return this.draggable.status;
  }
  refreshShape() {
    return this.droppable.refreshShape();
  }
  accepts(e) {
    return this.droppable.accepts(e);
  }
};
D = Be();
J = /* @__PURE__ */ new WeakMap();
W = /* @__PURE__ */ new WeakMap();
$ = /* @__PURE__ */ new WeakMap();
Q = /* @__PURE__ */ new WeakMap();
T = /* @__PURE__ */ new WeakMap();
he(D, 4, "index", _e, U, J);
he(D, 4, "group", ve, U, Q);
Ne(D, U);
var xe = class extends Ie {
  constructor(e, t, r) {
    super(e, t), this.sortable = r;
  }
  get index() {
    return this.sortable.index;
  }
}, X = class extends we {
  constructor(e, t, r) {
    super(e, t), this.sortable = r;
  }
};
export {
  Ze as OptimisticSortingPlugin,
  U as Sortable,
  Ve as SortableKeyboardPlugin,
  rt as defaultSortableTransition,
  G as isSortable
};
//# sourceMappingURL=sortable.js.map
