"use client";
import { isAction as B } from "../../../redux/dist/redux.js";
import { isPlainObject as ee } from "../../../redux/dist/redux.js";
import { produce as j, isDraftable as M, isDraft as I } from "../../../immer/dist/immer.js";
import { current as te, freeze as ne } from "../../../immer/dist/immer.js";
function S(e, t) {
  function i(...r) {
    if (t) {
      let c = t(...r);
      if (!c)
        throw new Error(process.env.NODE_ENV === "production" ? f(0) : "prepareAction did not return an object");
      return {
        type: e,
        payload: c.payload,
        ..."meta" in c && {
          meta: c.meta
        },
        ..."error" in c && {
          error: c.error
        }
      };
    }
    return {
      type: e,
      payload: r[0]
    };
  }
  return i.toString = () => `${e}`, i.type = e, i.match = (r) => B(r) && r.type === e, i;
}
function T(e) {
  return M(e) ? j(e, () => {
  }) : e;
}
function m(e, t, i) {
  return e.has(t) ? e.get(t) : e.set(t, i(t)).get(t);
}
function k(e) {
  const t = {}, i = [];
  let r;
  const c = {
    addCase(n, o) {
      if (process.env.NODE_ENV !== "production") {
        if (i.length > 0)
          throw new Error(process.env.NODE_ENV === "production" ? f(26) : "`builder.addCase` should only be called before calling `builder.addMatcher`");
        if (r)
          throw new Error(process.env.NODE_ENV === "production" ? f(27) : "`builder.addCase` should only be called before calling `builder.addDefaultCase`");
      }
      const l = typeof n == "string" ? n : n.type;
      if (!l)
        throw new Error(process.env.NODE_ENV === "production" ? f(28) : "`builder.addCase` cannot be called with an empty action type");
      if (l in t)
        throw new Error(process.env.NODE_ENV === "production" ? f(29) : `\`builder.addCase\` cannot be called with two reducers for the same action type '${l}'`);
      return t[l] = o, c;
    },
    addAsyncThunk(n, o) {
      if (process.env.NODE_ENV !== "production" && r)
        throw new Error(process.env.NODE_ENV === "production" ? f(43) : "`builder.addAsyncThunk` should only be called before calling `builder.addDefaultCase`");
      return o.pending && (t[n.pending.type] = o.pending), o.rejected && (t[n.rejected.type] = o.rejected), o.fulfilled && (t[n.fulfilled.type] = o.fulfilled), o.settled && i.push({
        matcher: n.settled,
        reducer: o.settled
      }), c;
    },
    addMatcher(n, o) {
      if (process.env.NODE_ENV !== "production" && r)
        throw new Error(process.env.NODE_ENV === "production" ? f(30) : "`builder.addMatcher` should only be called before calling `builder.addDefaultCase`");
      return i.push({
        matcher: n,
        reducer: o
      }), c;
    },
    addDefaultCase(n) {
      if (process.env.NODE_ENV !== "production" && r)
        throw new Error(process.env.NODE_ENV === "production" ? f(31) : "`builder.addDefaultCase` can only be called once");
      return r = n, c;
    }
  };
  return e(c), [t, i, r];
}
function A(e) {
  return typeof e == "function";
}
function $(e, t) {
  if (process.env.NODE_ENV !== "production" && typeof t == "object")
    throw new Error(process.env.NODE_ENV === "production" ? f(8) : "The object notation for `createReducer` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createReducer");
  let [i, r, c] = k(t), n;
  if (A(e))
    n = () => T(e());
  else {
    const l = T(e);
    n = () => l;
  }
  function o(l = n(), u) {
    let h = [i[u.type], ...r.filter(({
      matcher: d
    }) => d(u)).map(({
      reducer: d
    }) => d)];
    return h.filter((d) => !!d).length === 0 && (h = [c]), h.reduce((d, N) => {
      if (N)
        if (I(d)) {
          const b = N(d, u);
          return b === void 0 ? d : b;
        } else {
          if (M(d))
            return j(d, (D) => N(D, u));
          {
            const D = N(d, u);
            if (D === void 0) {
              if (d === null)
                return d;
              throw Error("A case reducer on a non-draftable value must not return undefined");
            }
            return D;
          }
        }
      return d;
    }, l);
  }
  return o.getInitialState = n, o;
}
var z = /* @__PURE__ */ Symbol.for("rtk-slice-createasyncthunk");
function Y(e, t) {
  return `${e}/${t}`;
}
function q({
  creators: e
} = {}) {
  const t = e?.asyncThunk?.[z];
  return function(r) {
    const {
      name: c,
      reducerPath: n = c
    } = r;
    if (!c)
      throw new Error(process.env.NODE_ENV === "production" ? f(11) : "`name` is a required option for createSlice");
    typeof process < "u" && process.env.NODE_ENV === "development" && r.initialState === void 0 && console.error("You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`");
    const o = (typeof r.reducers == "function" ? r.reducers(U()) : r.reducers) || {}, l = Object.keys(o), u = {
      sliceCaseReducersByName: {},
      sliceCaseReducersByType: {},
      actionCreators: {},
      sliceMatchers: []
    }, h = {
      addCase(a, s) {
        const p = typeof a == "string" ? a : a.type;
        if (!p)
          throw new Error(process.env.NODE_ENV === "production" ? f(12) : "`context.addCase` cannot be called with an empty action type");
        if (p in u.sliceCaseReducersByType)
          throw new Error(process.env.NODE_ENV === "production" ? f(13) : "`context.addCase` cannot be called with two reducers for the same action type: " + p);
        return u.sliceCaseReducersByType[p] = s, h;
      },
      addMatcher(a, s) {
        return u.sliceMatchers.push({
          matcher: a,
          reducer: s
        }), h;
      },
      exposeAction(a, s) {
        return u.actionCreators[a] = s, h;
      },
      exposeCaseReducer(a, s) {
        return u.sliceCaseReducersByName[a] = s, h;
      }
    };
    l.forEach((a) => {
      const s = o[a], p = {
        reducerName: a,
        type: Y(c, a),
        createNotation: typeof r.reducers == "function"
      };
      G(s) ? J(p, s, h, t) : W(p, s, h);
    });
    function d() {
      if (process.env.NODE_ENV !== "production" && typeof r.extraReducers == "object")
        throw new Error(process.env.NODE_ENV === "production" ? f(14) : "The object notation for `createSlice.extraReducers` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createSlice");
      const [a = {}, s = [], p = void 0] = typeof r.extraReducers == "function" ? k(r.extraReducers) : [r.extraReducers], w = {
        ...a,
        ...u.sliceCaseReducersByType
      };
      return $(r.initialState, (y) => {
        for (let E in w)
          y.addCase(E, w[E]);
        for (let E of u.sliceMatchers)
          y.addMatcher(E.matcher, E.reducer);
        for (let E of s)
          y.addMatcher(E.matcher, E.reducer);
        p && y.addDefaultCase(p);
      });
    }
    const N = (a) => a, D = /* @__PURE__ */ new Map(), b = /* @__PURE__ */ new WeakMap();
    let C;
    function g(a, s) {
      return C || (C = d()), C(a, s);
    }
    function _() {
      return C || (C = d()), C.getInitialState();
    }
    function R(a, s = !1) {
      function p(y) {
        let E = y[a];
        if (typeof E > "u") {
          if (s)
            E = m(b, p, _);
          else if (process.env.NODE_ENV !== "production")
            throw new Error(process.env.NODE_ENV === "production" ? f(15) : "selectSlice returned undefined for an uninjected slice reducer");
        }
        return E;
      }
      function w(y = N) {
        const E = m(D, s, () => /* @__PURE__ */ new WeakMap());
        return m(E, y, () => {
          const V = {};
          for (const [x, P] of Object.entries(r.selectors ?? {}))
            V[x] = F(P, y, () => m(b, y, _), s);
          return V;
        });
      }
      return {
        reducerPath: a,
        getSelectors: w,
        get selectors() {
          return w(p);
        },
        selectSlice: p
      };
    }
    const O = {
      name: c,
      reducer: g,
      actions: u.actionCreators,
      caseReducers: u.sliceCaseReducersByName,
      getInitialState: _,
      ...R(n),
      injectInto(a, {
        reducerPath: s,
        ...p
      } = {}) {
        const w = s ?? n;
        return a.inject({
          reducerPath: w,
          reducer: g
        }, p), {
          ...O,
          ...R(w, !0)
        };
      }
    };
    return O;
  };
}
function F(e, t, i, r) {
  function c(n, ...o) {
    let l = t(n);
    if (typeof l > "u") {
      if (r)
        l = i();
      else if (process.env.NODE_ENV !== "production")
        throw new Error(process.env.NODE_ENV === "production" ? f(16) : "selectState returned undefined for an uninjected slice reducer");
    }
    return e(l, ...o);
  }
  return c.unwrapped = e, c;
}
var Q = /* @__PURE__ */ q();
function U() {
  function e(t, i) {
    return {
      _reducerDefinitionType: "asyncThunk",
      payloadCreator: t,
      ...i
    };
  }
  return e.withTypes = () => e, {
    reducer(t) {
      return Object.assign({
        // hack so the wrapping function has the same name as the original
        // we need to create a wrapper so the `reducerDefinitionType` is not assigned to the original
        [t.name](...i) {
          return t(...i);
        }
      }[t.name], {
        _reducerDefinitionType: "reducer"
        /* reducer */
      });
    },
    preparedReducer(t, i) {
      return {
        _reducerDefinitionType: "reducerWithPrepare",
        prepare: t,
        reducer: i
      };
    },
    asyncThunk: e
  };
}
function W({
  type: e,
  reducerName: t,
  createNotation: i
}, r, c) {
  let n, o;
  if ("reducer" in r) {
    if (i && !H(r))
      throw new Error(process.env.NODE_ENV === "production" ? f(17) : "Please use the `create.preparedReducer` notation for prepared action creators with the `create` notation.");
    n = r.reducer, o = r.prepare;
  } else
    n = r;
  c.addCase(e, n).exposeCaseReducer(t, n).exposeAction(t, o ? S(e, o) : S(e));
}
function G(e) {
  return e._reducerDefinitionType === "asyncThunk";
}
function H(e) {
  return e._reducerDefinitionType === "reducerWithPrepare";
}
function J({
  type: e,
  reducerName: t
}, i, r, c) {
  if (!c)
    throw new Error(process.env.NODE_ENV === "production" ? f(18) : "Cannot use `create.asyncThunk` in the built-in `createSlice`. Use `buildCreateSlice({ creators: { asyncThunk: asyncThunkCreator } })` to create a customised version of `createSlice`.");
  const {
    payloadCreator: n,
    fulfilled: o,
    pending: l,
    rejected: u,
    settled: h,
    options: d
  } = i, N = c(e, n, d);
  r.exposeAction(t, N), o && r.addCase(N.fulfilled, o), l && r.addCase(N.pending, l), u && r.addCase(N.rejected, u), h && r.addMatcher(N.settled, h), r.exposeCaseReducer(t, {
    fulfilled: o || v,
    pending: l || v,
    rejected: u || v,
    settled: h || v
  });
}
function v() {
}
function f(e) {
  return `Minified Redux Toolkit error #${e}; visit https://redux-toolkit.js.org/Errors?code=${e} for the full message or use the non-minified dev environment for full errors. `;
}
export {
  q as buildCreateSlice,
  S as createAction,
  j as createNextState,
  $ as createReducer,
  Q as createSlice,
  te as current,
  f as formatProdErrorMessage,
  ne as freeze,
  B as isAction,
  I as isDraft,
  ee as isPlainObject
};
