import { asap as p } from "../../../@react-dnd/asap/dist/asap.es.js";
import { invariant as n } from "../../../@react-dnd/invariant/dist/index.es.js";
import { addSource as h, addTarget as l, removeSource as g, removeTarget as S } from "../actions/registry.es.js";
import { validateType as a, validateSourceContract as T, validateTargetContract as m } from "../contracts.es.js";
import { HandlerRole as s } from "../interfaces.es.js";
import { getNextUniqueId as E } from "../utils/getNextUniqueId.es.js";
function f(i) {
  const e = E().toString();
  switch (i) {
    case s.SOURCE:
      return `S${e}`;
    case s.TARGET:
      return `T${e}`;
    default:
      throw new Error(`Unknown Handler Role: ${i}`);
  }
}
function d(i) {
  switch (i[0]) {
    case "S":
      return s.SOURCE;
    case "T":
      return s.TARGET;
    default:
      throw new Error(`Cannot parse handler ID: ${i}`);
  }
}
function c(i, e) {
  const t = i.entries();
  let r = !1;
  do {
    const { done: o, value: [, u] } = t.next();
    if (u === e)
      return !0;
    r = !!o;
  } while (!r);
  return !1;
}
class y {
  addSource(e, t) {
    a(e), T(t);
    const r = this.addHandler(s.SOURCE, e, t);
    return this.store.dispatch(h(r)), r;
  }
  addTarget(e, t) {
    a(e, !0), m(t);
    const r = this.addHandler(s.TARGET, e, t);
    return this.store.dispatch(l(r)), r;
  }
  containsHandler(e) {
    return c(this.dragSources, e) || c(this.dropTargets, e);
  }
  getSource(e, t = !1) {
    return n(this.isSourceId(e), "Expected a valid source ID."), t && e === this.pinnedSourceId ? this.pinnedSource : this.dragSources.get(e);
  }
  getTarget(e) {
    return n(this.isTargetId(e), "Expected a valid target ID."), this.dropTargets.get(e);
  }
  getSourceType(e) {
    return n(this.isSourceId(e), "Expected a valid source ID."), this.types.get(e);
  }
  getTargetType(e) {
    return n(this.isTargetId(e), "Expected a valid target ID."), this.types.get(e);
  }
  isSourceId(e) {
    return d(e) === s.SOURCE;
  }
  isTargetId(e) {
    return d(e) === s.TARGET;
  }
  removeSource(e) {
    n(this.getSource(e), "Expected an existing source."), this.store.dispatch(g(e)), p(() => {
      this.dragSources.delete(e), this.types.delete(e);
    });
  }
  removeTarget(e) {
    n(this.getTarget(e), "Expected an existing target."), this.store.dispatch(S(e)), this.dropTargets.delete(e), this.types.delete(e);
  }
  pinSource(e) {
    const t = this.getSource(e);
    n(t, "Expected an existing source."), this.pinnedSourceId = e, this.pinnedSource = t;
  }
  unpinSource() {
    n(this.pinnedSource, "No source is pinned at the time."), this.pinnedSourceId = null, this.pinnedSource = null;
  }
  addHandler(e, t, r) {
    const o = f(e);
    return this.types.set(o, t), e === s.SOURCE ? this.dragSources.set(o, r) : e === s.TARGET && this.dropTargets.set(o, r), o;
  }
  constructor(e) {
    this.types = /* @__PURE__ */ new Map(), this.dragSources = /* @__PURE__ */ new Map(), this.dropTargets = /* @__PURE__ */ new Map(), this.pinnedSourceId = null, this.pinnedSource = null, this.store = e;
  }
}
export {
  y as HandlerRegistryImpl
};
//# sourceMappingURL=HandlerRegistryImpl.es.js.map
