"use client";
var W = Symbol.for("immer-nothing"), R = Symbol.for("immer-draftable"), u = Symbol.for("immer-state"), Q = 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 s(e, ...r) {
  if (process.env.NODE_ENV !== "production") {
    const t = Q[e], n = typeof t == "function" ? t.apply(null, r) : t;
    throw new Error(`[Immer] ${n}`);
  }
  throw new Error(
    `[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`
  );
}
var y = Object.getPrototypeOf;
function m(e) {
  return !!e && !!e[u];
}
function d(e) {
  return e ? B(e) || Array.isArray(e) || !!e[R] || !!e.constructor?.[R] || P(e) || D(e) : !1;
}
var Y = Object.prototype.constructor.toString();
function B(e) {
  if (!e || typeof e != "object")
    return !1;
  const r = y(e);
  if (r === null)
    return !0;
  const t = Object.hasOwnProperty.call(r, "constructor") && r.constructor;
  return t === Object ? !0 : typeof t == "function" && Function.toString.call(t) === Y;
}
function b(e, r) {
  O(e) === 0 ? Reflect.ownKeys(e).forEach((t) => {
    r(t, e[t], e);
  }) : e.forEach((t, n) => r(n, t, e));
}
function O(e) {
  const r = e[u];
  return r ? r.type_ : Array.isArray(e) ? 1 : P(e) ? 2 : D(e) ? 3 : 0;
}
function E(e, r) {
  return O(e) === 2 ? e.has(r) : Object.prototype.hasOwnProperty.call(e, r);
}
function G(e, r, t) {
  const n = O(e);
  n === 2 ? e.set(r, t) : n === 3 ? e.add(t) : e[r] = t;
}
function Z(e, r) {
  return e === r ? e !== 0 || 1 / e === 1 / r : e !== e && r !== r;
}
function P(e) {
  return e instanceof Map;
}
function D(e) {
  return e instanceof Set;
}
function l(e) {
  return e.copy_ || e.base_;
}
function N(e, r) {
  if (P(e))
    return new Map(e);
  if (D(e))
    return new Set(e);
  if (Array.isArray(e))
    return Array.prototype.slice.call(e);
  const t = B(e);
  if (r === !0 || r === "class_only" && !t) {
    const n = Object.getOwnPropertyDescriptors(e);
    delete n[u];
    let i = Reflect.ownKeys(n);
    for (let o = 0; o < i.length; o++) {
      const c = i[o], f = n[c];
      f.writable === !1 && (f.writable = !0, f.configurable = !0), (f.get || f.set) && (n[c] = {
        configurable: !0,
        writable: !0,
        // could live with !!desc.set as well here...
        enumerable: f.enumerable,
        value: e[c]
      });
    }
    return Object.create(y(e), n);
  } else {
    const n = y(e);
    if (n !== null && t)
      return { ...e };
    const i = Object.create(n);
    return Object.assign(i, e);
  }
}
function T(e, r = !1) {
  return v(e) || m(e) || !d(e) || (O(e) > 1 && Object.defineProperties(e, {
    set: { value: g },
    add: { value: g },
    clear: { value: g },
    delete: { value: g }
  }), Object.freeze(e), r && Object.values(e).forEach((t) => T(t, !0))), e;
}
function g() {
  s(2);
}
function v(e) {
  return Object.isFrozen(e);
}
var L = {};
function _(e) {
  const r = L[e];
  return r || s(0, e), r;
}
var h;
function U() {
  return h;
}
function V(e, r) {
  return {
    drafts_: [],
    parent_: e,
    immer_: r,
    // 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 j(e, r) {
  r && (_("Patches"), e.patches_ = [], e.inversePatches_ = [], e.patchListener_ = r);
}
function A(e) {
  I(e), e.drafts_.forEach(ee), e.drafts_ = null;
}
function I(e) {
  e === h && (h = e.parent_);
}
function x(e) {
  return h = V(h, e);
}
function ee(e) {
  const r = e[u];
  r.type_ === 0 || r.type_ === 1 ? r.revoke_() : r.revoked_ = !0;
}
function $(e, r) {
  r.unfinalizedDrafts_ = r.drafts_.length;
  const t = r.drafts_[0];
  return e !== void 0 && e !== t ? (t[u].modified_ && (A(r), s(4)), d(e) && (e = w(r, e), r.parent_ || z(r, e)), r.patches_ && _("Patches").generateReplacementPatches_(
    t[u].base_,
    e,
    r.patches_,
    r.inversePatches_
  )) : e = w(r, t, []), A(r), r.patches_ && r.patchListener_(r.patches_, r.inversePatches_), e !== W ? e : void 0;
}
function w(e, r, t) {
  if (v(r))
    return r;
  const n = r[u];
  if (!n)
    return b(
      r,
      (i, o) => K(e, n, r, i, o, t)
    ), r;
  if (n.scope_ !== e)
    return r;
  if (!n.modified_)
    return z(e, n.base_, !0), n.base_;
  if (!n.finalized_) {
    n.finalized_ = !0, n.scope_.unfinalizedDrafts_--;
    const i = n.copy_;
    let o = i, c = !1;
    n.type_ === 3 && (o = new Set(i), i.clear(), c = !0), b(
      o,
      (f, a) => K(e, n, i, f, a, t, c)
    ), z(e, i, !1), t && e.patches_ && _("Patches").generatePatches_(
      n,
      t,
      e.patches_,
      e.inversePatches_
    );
  }
  return n.copy_;
}
function K(e, r, t, n, i, o, c) {
  if (process.env.NODE_ENV !== "production" && i === t && s(5), m(i)) {
    const f = o && r && r.type_ !== 3 && // Set objects are atomic since they have no keys.
    !E(r.assigned_, n) ? o.concat(n) : void 0, a = w(e, i, f);
    if (G(t, n, a), m(a))
      e.canAutoFreeze_ = !1;
    else
      return;
  } else c && t.add(i);
  if (d(i) && !v(i)) {
    if (!e.immer_.autoFreeze_ && e.unfinalizedDrafts_ < 1)
      return;
    w(e, i), (!r || !r.scope_.parent_) && typeof n != "symbol" && (P(t) ? t.has(n) : Object.prototype.propertyIsEnumerable.call(t, n)) && z(e, i);
  }
}
function z(e, r, t = !1) {
  !e.parent_ && e.immer_.autoFreeze_ && e.canAutoFreeze_ && T(r, t);
}
function re(e, r) {
  const t = Array.isArray(e), n = {
    type_: t ? 1 : 0,
    // Track which produce call this is associated with.
    scope_: r ? r.scope_ : U(),
    // 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_: r,
    // 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, o = M;
  t && (i = [n], o = p);
  const { revoke: c, proxy: f } = Proxy.revocable(i, o);
  return n.draft_ = f, n.revoke_ = c, f;
}
var M = {
  get(e, r) {
    if (r === u)
      return e;
    const t = l(e);
    if (!E(t, r))
      return te(e, t, r);
    const n = t[r];
    return e.finalized_ || !d(n) ? n : n === F(e.base_, r) ? (S(e), e.copy_[r] = k(n, e)) : n;
  },
  has(e, r) {
    return r in l(e);
  },
  ownKeys(e) {
    return Reflect.ownKeys(l(e));
  },
  set(e, r, t) {
    const n = H(l(e), r);
    if (n?.set)
      return n.set.call(e.draft_, t), !0;
    if (!e.modified_) {
      const i = F(l(e), r), o = i?.[u];
      if (o && o.base_ === t)
        return e.copy_[r] = t, e.assigned_[r] = !1, !0;
      if (Z(t, i) && (t !== void 0 || E(e.base_, r)))
        return !0;
      S(e), C(e);
    }
    return e.copy_[r] === t && // special case: handle new props with value 'undefined'
    (t !== void 0 || r in e.copy_) || // special case: NaN
    Number.isNaN(t) && Number.isNaN(e.copy_[r]) || (e.copy_[r] = t, e.assigned_[r] = !0), !0;
  },
  deleteProperty(e, r) {
    return F(e.base_, r) !== void 0 || r in e.base_ ? (e.assigned_[r] = !1, S(e), C(e)) : delete e.assigned_[r], e.copy_ && delete e.copy_[r], !0;
  },
  // Note: We never coerce `desc.value` into an Immer draft, because we can't make
  // the same guarantee in ES5 mode.
  getOwnPropertyDescriptor(e, r) {
    const t = l(e), n = Reflect.getOwnPropertyDescriptor(t, r);
    return n && {
      writable: !0,
      configurable: e.type_ !== 1 || r !== "length",
      enumerable: n.enumerable,
      value: t[r]
    };
  },
  defineProperty() {
    s(11);
  },
  getPrototypeOf(e) {
    return y(e.base_);
  },
  setPrototypeOf() {
    s(12);
  }
}, p = {};
b(M, (e, r) => {
  p[e] = function() {
    return arguments[0] = arguments[0][0], r.apply(this, arguments);
  };
});
p.deleteProperty = function(e, r) {
  return process.env.NODE_ENV !== "production" && isNaN(parseInt(r)) && s(13), p.set.call(this, e, r, void 0);
};
p.set = function(e, r, t) {
  return process.env.NODE_ENV !== "production" && r !== "length" && isNaN(parseInt(r)) && s(14), M.set.call(this, e[0], r, t, e[0]);
};
function F(e, r) {
  const t = e[u];
  return (t ? l(t) : e)[r];
}
function te(e, r, t) {
  const n = H(r, t);
  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!
    n.get?.call(e.draft_)
  ) : void 0;
}
function H(e, r) {
  if (!(r in e))
    return;
  let t = y(e);
  for (; t; ) {
    const n = Object.getOwnPropertyDescriptor(t, r);
    if (n)
      return n;
    t = y(t);
  }
}
function C(e) {
  e.modified_ || (e.modified_ = !0, e.parent_ && C(e.parent_));
}
function S(e) {
  e.copy_ || (e.copy_ = N(
    e.base_,
    e.scope_.immer_.useStrictShallowCopy_
  ));
}
var ne = class {
  constructor(e) {
    this.autoFreeze_ = !0, this.useStrictShallowCopy_ = !1, this.produce = (r, t, n) => {
      if (typeof r == "function" && typeof t != "function") {
        const o = t;
        t = r;
        const c = this;
        return function(a = o, ...q) {
          return c.produce(a, (J) => t.call(this, J, ...q));
        };
      }
      typeof t != "function" && s(6), n !== void 0 && typeof n != "function" && s(7);
      let i;
      if (d(r)) {
        const o = x(this), c = k(r, void 0);
        let f = !0;
        try {
          i = t(c), f = !1;
        } finally {
          f ? A(o) : I(o);
        }
        return j(o, n), $(i, o);
      } else if (!r || typeof r != "object") {
        if (i = t(r), i === void 0 && (i = r), i === W && (i = void 0), this.autoFreeze_ && T(i, !0), n) {
          const o = [], c = [];
          _("Patches").generateReplacementPatches_(r, i, o, c), n(o, c);
        }
        return i;
      } else
        s(1, r);
    }, this.produceWithPatches = (r, t) => {
      if (typeof r == "function")
        return (c, ...f) => this.produceWithPatches(c, (a) => r(a, ...f));
      let n, i;
      return [this.produce(r, t, (c, f) => {
        n = c, i = f;
      }), n, i];
    }, typeof e?.autoFreeze == "boolean" && this.setAutoFreeze(e.autoFreeze), typeof e?.useStrictShallowCopy == "boolean" && this.setUseStrictShallowCopy(e.useStrictShallowCopy);
  }
  createDraft(e) {
    d(e) || s(8), m(e) && (e = ie(e));
    const r = x(this), t = k(e, void 0);
    return t[u].isManual_ = !0, I(r), t;
  }
  finishDraft(e, r) {
    const t = e && e[u];
    (!t || !t.isManual_) && s(9);
    const { scope_: n } = t;
    return j(n, r), $(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, r) {
    let t;
    for (t = r.length - 1; t >= 0; t--) {
      const i = r[t];
      if (i.path.length === 0 && i.op === "replace") {
        e = i.value;
        break;
      }
    }
    t > -1 && (r = r.slice(t + 1));
    const n = _("Patches").applyPatches_;
    return m(e) ? n(e, r) : this.produce(
      e,
      (i) => n(i, r)
    );
  }
};
function k(e, r) {
  const t = P(e) ? _("MapSet").proxyMap_(e, r) : D(e) ? _("MapSet").proxySet_(e, r) : re(e, r);
  return (r ? r.scope_ : U()).drafts_.push(t), t;
}
function ie(e) {
  return m(e) || s(10, e), X(e);
}
function X(e) {
  if (!d(e) || v(e))
    return e;
  const r = e[u];
  let t;
  if (r) {
    if (!r.modified_)
      return r.base_;
    r.finalized_ = !0, t = N(e, r.scope_.immer_.useStrictShallowCopy_);
  } else
    t = N(e, !0);
  return b(t, (n, i) => {
    G(t, n, X(i));
  }), r && (r.finalized_ = !1), t;
}
var oe = new ne(), ce = oe.produce;
function fe(e) {
  return e;
}
export {
  ne as Immer,
  fe as castDraft,
  ie as current,
  T as freeze,
  R as immerable,
  m as isDraft,
  d as isDraftable,
  W as nothing,
  ce as produce
};
