import { invariant as f } from "../../../../@react-dnd/invariant/dist/index.es.js";
import { isObject as b } from "../../utils/js_utils.es.js";
import { setClientOffset as a } from "./local/setClientOffset.es.js";
import { INIT_COORDS as d, BEGIN_DRAG as S } from "./types.es.js";
const m = {
  type: d,
  payload: {
    clientOffset: null,
    sourceClientOffset: null
  }
};
function w(e) {
  return function(n = [], t = {
    publishSource: !0
  }) {
    const { publishSource: p = !0, clientOffset: o, getSourceClientOffset: u } = t, s = e.getMonitor(), c = e.getRegistry();
    e.dispatch(a(o)), y(n, s, c);
    const i = D(n, s);
    if (i == null) {
      e.dispatch(m);
      return;
    }
    let l = null;
    if (o) {
      if (!u)
        throw new Error("getSourceClientOffset must be defined");
      C(u), l = u(i);
    }
    e.dispatch(a(o, l));
    const g = c.getSource(i).beginDrag(s, i);
    if (g == null)
      return;
    h(g), c.pinSource(i);
    const O = c.getSourceType(i);
    return {
      type: S,
      payload: {
        itemType: O,
        item: g,
        sourceId: i,
        clientOffset: o || null,
        sourceClientOffset: l || null,
        isSourcePublic: !!p
      }
    };
  };
}
function y(e, r, n) {
  f(!r.isDragging(), "Cannot call beginDrag while dragging."), e.forEach(function(t) {
    f(n.getSource(t), "Expected sourceIds to be registered.");
  });
}
function C(e) {
  f(typeof e == "function", "When clientOffset is provided, getSourceClientOffset must be a function.");
}
function h(e) {
  f(b(e), "Item must be an object.");
}
function D(e, r) {
  let n = null;
  for (let t = e.length - 1; t >= 0; t--)
    if (r.canDragSource(e[t])) {
      n = e[t];
      break;
    }
  return n;
}
export {
  w as createBeginDrag
};
//# sourceMappingURL=beginDrag.es.js.map
