import { jsx as V } from "react/jsx-runtime";
import { createContext as ee, useContext as te, useSyncExternalStore as re, useRef as W, useCallback as E } from "react";
var X = Symbol.for("immer-nothing"), K = Symbol.for("immer-draftable"), u = Symbol.for("immer-state"), ne = process.env.NODE_ENV !== "production" ? [
  // All error codes, starting by 0:
  function(e) {
    return `The plugin for '${e}' has not been loaded into Immer. To enable the plugin, import and call \`enable${e}()\` when initializing your application.`;
  },
  function(e) {
    return `produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${e}'`;
  },
  "This object has been frozen and should not be mutated",
  function(e) {
    return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + e;
  },
  "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
  "Immer forbids circular references",
  "The first or second argument to `produce` must be a function",
  "The third argument to `produce` must be a function or undefined",
  "First argument to `createDraft` must be a plain object, an array, or an immerable object",
  "First argument to `finishDraft` must be a draft returned by `createDraft`",
  function(e) {
    return `'current' expects a draft, got: ${e}`;
  },
  "Object.defineProperty() cannot be used on an Immer draft",
  "Object.setPrototypeOf() cannot be used on an Immer draft",
  "Immer only supports deleting array indices",
  "Immer only supports setting array indices and the 'length' property",
  function(e) {
    return `'original' expects a draft, got: ${e}`;
  }
  // Note: if more errors are added, the errorOffset in Patches.ts should be increased
  // See Patches.ts for additional errors
] : [];
function f(e, ...t) {
  if (process.env.NODE_ENV !== "production") {
    const r = ne[e], n = typeof r == "function" ? r.apply(null, t) : r;
    throw new Error(`[Immer] ${n}`);
  }
  throw new Error(
    `[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`
  );
}
var m = Object.getPrototypeOf;
function h(e) {
  return !!e && !!e[u];
}
function _(e) {
  var t;
  return e ? q(e) || Array.isArray(e) || !!e[K] || !!((t = e.constructor) != null && t[K]) || O(e) || D(e) : !1;
}
var ie = Object.prototype.constructor.toString();
function q(e) {
  if (!e || typeof e != "object")
    return !1;
  const t = m(e);
  if (t === null)
    return !0;
  const r = Object.hasOwnProperty.call(t, "constructor") && t.constructor;
  return r === Object ? !0 : typeof r == "function" && Function.toString.call(r) === ie;
}
function p(e, t) {
  z(e) === 0 ? Object.entries(e).forEach(([r, n]) => {
    t(r, n, e);
  }) : e.forEach((r, n) => t(n, r, e));
}
function z(e) {
  const t = e[u];
  return t ? t.type_ : Array.isArray(e) ? 1 : O(e) ? 2 : D(e) ? 3 : 0;
}
function k(e, t) {
  return z(e) === 2 ? e.has(t) : Object.prototype.hasOwnProperty.call(e, t);
}
function J(e, t, r) {
  const n = z(e);
  n === 2 ? e.set(t, r) : n === 3 ? e.add(r) : e[t] = r;
}
function oe(e, t) {
  return e === t ? e !== 0 || 1 / e === 1 / t : e !== e && t !== t;
}
function O(e) {
  return e instanceof Map;
}
function D(e) {
  return e instanceof Set;
}
function d(e) {
  return e.copy_ || e.base_;
}
function x(e, t) {
  if (O(e))
    return new Map(e);
  if (D(e))
    return new Set(e);
  if (Array.isArray(e))
    return Array.prototype.slice.call(e);
  if (!t && q(e))
    return m(e) ? { ...e } : Object.assign(/* @__PURE__ */ Object.create(null), e);
  const r = Object.getOwnPropertyDescriptors(e);
  delete r[u];
  let n = Reflect.ownKeys(r);
  for (let i = 0; i < n.length; i++) {
    const s = n[i], o = r[s];
    o.writable === !1 && (o.writable = !0, o.configurable = !0), (o.get || o.set) && (r[s] = {
      configurable: !0,
      writable: !0,
      // could live with !!desc.set as well here...
      enumerable: o.enumerable,
      value: e[s]
    });
  }
  return Object.create(m(e), r);
}
function R(e, t = !1) {
  return F(e) || h(e) || !_(e) || (z(e) > 1 && (e.set = e.add = e.clear = e.delete = se), Object.freeze(e), t && p(e, (r, n) => R(n, !0))), e;
}
function se() {
  f(2);
}
function F(e) {
  return Object.isFrozen(e);
}
var ce = {};
function y(e) {
  const t = ce[e];
  return t || f(0, e), t;
}
var P;
function Q() {
  return P;
}
function fe(e, t) {
  return {
    drafts_: [],
    parent_: e,
    immer_: t,
    // Whenever the modified draft contains a draft from another scope, we
    // need to prevent auto-freezing so the unowned draft can be finalized.
    canAutoFreeze_: !0,
    unfinalizedDrafts_: 0
  };
}
function U(e, t) {
  t && (y("Patches"), e.patches_ = [], e.inversePatches_ = [], e.patchListener_ = t);
}
function I(e) {
  T(e), e.drafts_.forEach(ue), e.drafts_ = null;
}
function T(e) {
  e === P && (P = e.parent_);
}
function B(e) {
  return P = fe(P, e);
}
function ue(e) {
  const t = e[u];
  t.type_ === 0 || t.type_ === 1 ? t.revoke_() : t.revoked_ = !0;
}
function G(e, t) {
  t.unfinalizedDrafts_ = t.drafts_.length;
  const r = t.drafts_[0];
  return e !== void 0 && e !== r ? (r[u].modified_ && (I(t), f(4)), _(e) && (e = g(t, e), t.parent_ || S(t, e)), t.patches_ && y("Patches").generateReplacementPatches_(
    r[u].base_,
    e,
    t.patches_,
    t.inversePatches_
  )) : e = g(t, r, []), I(t), t.patches_ && t.patchListener_(t.patches_, t.inversePatches_), e !== X ? e : void 0;
}
function g(e, t, r) {
  if (F(t))
    return t;
  const n = t[u];
  if (!n)
    return p(
      t,
      (i, s) => H(e, n, t, i, s, r)
    ), t;
  if (n.scope_ !== e)
    return t;
  if (!n.modified_)
    return S(e, n.base_, !0), n.base_;
  if (!n.finalized_) {
    n.finalized_ = !0, n.scope_.unfinalizedDrafts_--;
    const i = n.copy_;
    let s = i, o = !1;
    n.type_ === 3 && (s = new Set(i), i.clear(), o = !0), p(
      s,
      (c, l) => H(e, n, i, c, l, r, o)
    ), S(e, i, !1), r && e.patches_ && y("Patches").generatePatches_(
      n,
      r,
      e.patches_,
      e.inversePatches_
    );
  }
  return n.copy_;
}
function H(e, t, r, n, i, s, o) {
  if (process.env.NODE_ENV !== "production" && i === r && f(5), h(i)) {
    const c = s && t && t.type_ !== 3 && // Set objects are atomic since they have no keys.
    !k(t.assigned_, n) ? s.concat(n) : void 0, l = g(e, i, c);
    if (J(r, n, l), h(l))
      e.canAutoFreeze_ = !1;
    else
      return;
  } else
    o && r.add(i);
  if (_(i) && !F(i)) {
    if (!e.immer_.autoFreeze_ && e.unfinalizedDrafts_ < 1)
      return;
    g(e, i), (!t || !t.scope_.parent_) && S(e, i);
  }
}
function S(e, t, r = !1) {
  !e.parent_ && e.immer_.autoFreeze_ && e.canAutoFreeze_ && R(t, r);
}
function ae(e, t) {
  const r = Array.isArray(e), n = {
    type_: r ? 1 : 0,
    // Track which produce call this is associated with.
    scope_: t ? t.scope_ : Q(),
    // True for both shallow and deep changes.
    modified_: !1,
    // Used during finalization.
    finalized_: !1,
    // Track which properties have been assigned (true) or deleted (false).
    assigned_: {},
    // The parent draft state.
    parent_: t,
    // The base state.
    base_: e,
    // The base proxy.
    draft_: null,
    // set below
    // The base copy with any updated values.
    copy_: null,
    // Called by the `produce` function.
    revoke_: null,
    isManual_: !1
  };
  let i = n, s = $;
  r && (i = [n], s = b);
  const { revoke: o, proxy: c } = Proxy.revocable(i, s);
  return n.draft_ = c, n.revoke_ = o, c;
}
var $ = {
  get(e, t) {
    if (t === u)
      return e;
    const r = d(e);
    if (!k(r, t))
      return le(e, r, t);
    const n = r[t];
    return e.finalized_ || !_(n) ? n : n === A(e.base_, t) ? (N(e), e.copy_[t] = M(n, e)) : n;
  },
  has(e, t) {
    return t in d(e);
  },
  ownKeys(e) {
    return Reflect.ownKeys(d(e));
  },
  set(e, t, r) {
    const n = Y(d(e), t);
    if (n != null && n.set)
      return n.set.call(e.draft_, r), !0;
    if (!e.modified_) {
      const i = A(d(e), t), s = i == null ? void 0 : i[u];
      if (s && s.base_ === r)
        return e.copy_[t] = r, e.assigned_[t] = !1, !0;
      if (oe(r, i) && (r !== void 0 || k(e.base_, t)))
        return !0;
      N(e), j(e);
    }
    return e.copy_[t] === r && // special case: handle new props with value 'undefined'
    (r !== void 0 || t in e.copy_) || // special case: NaN
    Number.isNaN(r) && Number.isNaN(e.copy_[t]) || (e.copy_[t] = r, e.assigned_[t] = !0), !0;
  },
  deleteProperty(e, t) {
    return A(e.base_, t) !== void 0 || t in e.base_ ? (e.assigned_[t] = !1, N(e), j(e)) : delete e.assigned_[t], e.copy_ && delete e.copy_[t], !0;
  },
  // Note: We never coerce `desc.value` into an Immer draft, because we can't make
  // the same guarantee in ES5 mode.
  getOwnPropertyDescriptor(e, t) {
    const r = d(e), n = Reflect.getOwnPropertyDescriptor(r, t);
    return n && {
      writable: !0,
      configurable: e.type_ !== 1 || t !== "length",
      enumerable: n.enumerable,
      value: r[t]
    };
  },
  defineProperty() {
    f(11);
  },
  getPrototypeOf(e) {
    return m(e.base_);
  },
  setPrototypeOf() {
    f(12);
  }
}, b = {};
p($, (e, t) => {
  b[e] = function() {
    return arguments[0] = arguments[0][0], t.apply(this, arguments);
  };
});
b.deleteProperty = function(e, t) {
  return process.env.NODE_ENV !== "production" && isNaN(parseInt(t)) && f(13), b.set.call(this, e, t, void 0);
};
b.set = function(e, t, r) {
  return process.env.NODE_ENV !== "production" && t !== "length" && isNaN(parseInt(t)) && f(14), $.set.call(this, e[0], t, r, e[0]);
};
function A(e, t) {
  const r = e[u];
  return (r ? d(r) : e)[t];
}
function le(e, t, r) {
  var i;
  const n = Y(t, r);
  return n ? "value" in n ? n.value : (
    // This is a very special case, if the prop is a getter defined by the
    // prototype, we should invoke it with the draft as context!
    (i = n.get) == null ? void 0 : i.call(e.draft_)
  ) : void 0;
}
function Y(e, t) {
  if (!(t in e))
    return;
  let r = m(e);
  for (; r; ) {
    const n = Object.getOwnPropertyDescriptor(r, t);
    if (n)
      return n;
    r = m(r);
  }
}
function j(e) {
  e.modified_ || (e.modified_ = !0, e.parent_ && j(e.parent_));
}
function N(e) {
  e.copy_ || (e.copy_ = x(
    e.base_,
    e.scope_.immer_.useStrictShallowCopy_
  ));
}
var de = class {
  constructor(e) {
    this.autoFreeze_ = !0, this.useStrictShallowCopy_ = !1, this.produce = (t, r, n) => {
      if (typeof t == "function" && typeof r != "function") {
        const s = r;
        r = t;
        const o = this;
        return function(l = s, ...v) {
          return o.produce(l, (C) => r.call(this, C, ...v));
        };
      }
      typeof r != "function" && f(6), n !== void 0 && typeof n != "function" && f(7);
      let i;
      if (_(t)) {
        const s = B(this), o = M(t, void 0);
        let c = !0;
        try {
          i = r(o), c = !1;
        } finally {
          c ? I(s) : T(s);
        }
        return U(s, n), G(i, s);
      } else if (!t || typeof t != "object") {
        if (i = r(t), i === void 0 && (i = t), i === X && (i = void 0), this.autoFreeze_ && R(i, !0), n) {
          const s = [], o = [];
          y("Patches").generateReplacementPatches_(t, i, s, o), n(s, o);
        }
        return i;
      } else
        f(1, t);
    }, this.produceWithPatches = (t, r) => {
      if (typeof t == "function")
        return (o, ...c) => this.produceWithPatches(o, (l) => t(l, ...c));
      let n, i;
      return [this.produce(t, r, (o, c) => {
        n = o, i = c;
      }), n, i];
    }, typeof (e == null ? void 0 : e.autoFreeze) == "boolean" && this.setAutoFreeze(e.autoFreeze), typeof (e == null ? void 0 : e.useStrictShallowCopy) == "boolean" && this.setUseStrictShallowCopy(e.useStrictShallowCopy);
  }
  createDraft(e) {
    _(e) || f(8), h(e) && (e = _e(e));
    const t = B(this), r = M(e, void 0);
    return r[u].isManual_ = !0, T(t), r;
  }
  finishDraft(e, t) {
    const r = e && e[u];
    (!r || !r.isManual_) && f(9);
    const { scope_: n } = r;
    return U(n, t), G(void 0, n);
  }
  /**
   * Pass true to automatically freeze all copies created by Immer.
   *
   * By default, auto-freezing is enabled.
   */
  setAutoFreeze(e) {
    this.autoFreeze_ = e;
  }
  /**
   * Pass true to enable strict shallow copy.
   *
   * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.
   */
  setUseStrictShallowCopy(e) {
    this.useStrictShallowCopy_ = e;
  }
  applyPatches(e, t) {
    let r;
    for (r = t.length - 1; r >= 0; r--) {
      const i = t[r];
      if (i.path.length === 0 && i.op === "replace") {
        e = i.value;
        break;
      }
    }
    r > -1 && (t = t.slice(r + 1));
    const n = y("Patches").applyPatches_;
    return h(e) ? n(e, t) : this.produce(
      e,
      (i) => n(i, t)
    );
  }
};
function M(e, t) {
  const r = O(e) ? y("MapSet").proxyMap_(e, t) : D(e) ? y("MapSet").proxySet_(e, t) : ae(e, t);
  return (t ? t.scope_ : Q()).drafts_.push(r), r;
}
function _e(e) {
  return h(e) || f(10, e), Z(e);
}
function Z(e) {
  if (!_(e) || F(e))
    return e;
  const t = e[u];
  let r;
  if (t) {
    if (!t.modified_)
      return t.base_;
    t.finalized_ = !0, r = x(e, t.scope_.immer_.useStrictShallowCopy_);
  } else
    r = x(e, !0);
  return p(r, (n, i) => {
    J(r, n, Z(i));
  }), t && (t.finalized_ = !1), r;
}
var a = new de(), ye = a.produce;
a.produceWithPatches.bind(
  a
);
a.setAutoFreeze.bind(a);
a.setUseStrictShallowCopy.bind(a);
a.applyPatches.bind(a);
a.createDraft.bind(a);
a.finishDraft.bind(a);
function pe(e = {}) {
  function t(s) {
    const o = W(s || e), c = W(/* @__PURE__ */ new Set()), l = E(() => o.current, []), v = E((w) => {
      o.current = ye(o.current, w), c.current.forEach((L) => L());
    }, []), C = E((w) => (c.current.add(w), () => {
      c.current.delete(w);
    }), []);
    return { get: l, set: v, subscribe: C };
  }
  const r = ee(null);
  function n({ children: s, defaultValue: o }) {
    const c = t(o);
    return /* @__PURE__ */ V(r.Provider, { value: c, children: s });
  }
  function i(s) {
    const o = te(r);
    if (!o)
      throw new Error("Store not found");
    return [re(o.subscribe, () => s(o.get())), o.set];
  }
  return {
    useSelectorContext: i,
    Provider: n
  };
}
export {
  pe as default
};
