"use client";
var N = (e, t, s) => {
  if (t.length === 1 && t[0] === s) {
    let o = !1;
    try {
      const r = {};
      e(r) === r && (o = !0);
    } catch {
    }
    if (o) {
      let r;
      try {
        throw new Error();
      } catch (c) {
        ({ stack: r } = c);
      }
      console.warn(
        `The result function returned its own inputs without modification. e.g
\`createSelector([state => state.todos], todos => todos)\`
This could lead to inefficient memoization and unnecessary re-renders.
Ensure transformation logic is in the result function, and extraction logic is in the input selectors.`,
        { stack: r }
      );
    }
  }
}, x = (e, t, s) => {
  const { memoize: o, memoizeOptions: r } = t, { inputSelectorResults: c, inputSelectorResultsCopy: i } = e, n = o(() => ({}), ...r);
  if (!(n.apply(null, c) === n.apply(null, i))) {
    let l;
    try {
      throw new Error();
    } catch (u) {
      ({ stack: l } = u);
    }
    console.warn(
      `An input selector returned a different result when passed same arguments.
This means your output selector will likely run more frequently than intended.
Avoid returning a new reference inside your input selector, e.g.
\`createSelector([state => state.todos.map(todo => todo.id)], todoIds => todoIds.length)\``,
      {
        arguments: s,
        firstInputs: c,
        secondInputs: i,
        stack: l
      }
    );
  }
}, A = {
  inputStabilityCheck: "once",
  identityFunctionCheck: "once"
};
function F(e, t = `expected a function, instead received ${typeof e}`) {
  if (typeof e != "function")
    throw new TypeError(t);
}
function D(e, t = `expected an object, instead received ${typeof e}`) {
  if (typeof e != "object")
    throw new TypeError(t);
}
function $(e, t = "expected all items to be functions, instead received the following types: ") {
  if (!e.every((s) => typeof s == "function")) {
    const s = e.map(
      (o) => typeof o == "function" ? `function ${o.name || "unnamed"}()` : typeof o
    ).join(", ");
    throw new TypeError(`${t}[${s}]`);
  }
}
var S = (e) => Array.isArray(e) ? e : [e];
function W(e) {
  const t = Array.isArray(e[0]) ? e[0] : e;
  return $(
    t,
    "createSelector expects all input-selectors to be functions, but received the following types: "
  ), t;
}
function E(e, t) {
  const s = [], { length: o } = e;
  for (let r = 0; r < o; r++)
    s.push(e[r].apply(null, t));
  return s;
}
var q = (e, t) => {
  const { identityFunctionCheck: s, inputStabilityCheck: o } = {
    ...A,
    ...t
  };
  return {
    identityFunctionCheck: {
      shouldRun: s === "always" || s === "once" && e,
      run: N
    },
    inputStabilityCheck: {
      shouldRun: o === "always" || o === "once" && e,
      run: x
    }
  };
}, V = class {
  constructor(e) {
    this.value = e;
  }
  deref() {
    return this.value;
  }
}, K = typeof WeakRef < "u" ? WeakRef : V, P = 0, I = 1;
function m() {
  return {
    s: P,
    v: void 0,
    o: null,
    p: null
  };
}
function z(e, t = {}) {
  let s = m();
  const { resultEqualityCheck: o } = t;
  let r, c = 0;
  function i() {
    let n = s;
    const { length: f } = arguments;
    for (let a = 0, h = f; a < h; a++) {
      const d = arguments[a];
      if (typeof d == "function" || typeof d == "object" && d !== null) {
        let p = n.o;
        p === null && (n.o = p = /* @__PURE__ */ new WeakMap());
        const y = p.get(d);
        y === void 0 ? (n = m(), p.set(d, n)) : n = y;
      } else {
        let p = n.p;
        p === null && (n.p = p = /* @__PURE__ */ new Map());
        const y = p.get(d);
        y === void 0 ? (n = m(), p.set(d, n)) : n = y;
      }
    }
    const l = n;
    let u;
    if (n.s === I)
      u = n.v;
    else if (u = e.apply(null, arguments), c++, o) {
      const a = r?.deref?.() ?? r;
      a != null && o(a, u) && (u = a, c !== 0 && c--), r = typeof u == "object" && u !== null || typeof u == "function" ? new K(u) : u;
    }
    return l.s = I, l.v = u, u;
  }
  return i.clearCache = () => {
    s = m(), i.resetResultsCount();
  }, i.resultsCount = () => c, i.resetResultsCount = () => {
    c = 0;
  }, i;
}
function U(e, ...t) {
  const s = typeof e == "function" ? {
    memoize: e,
    memoizeOptions: t
  } : e, o = (...r) => {
    let c = 0, i = 0, n, f = {}, l = r.pop();
    typeof l == "object" && (f = l, l = r.pop()), F(
      l,
      `createSelector expects an output function after the inputs, but received: [${typeof l}]`
    );
    const u = {
      ...s,
      ...f
    }, {
      memoize: a,
      memoizeOptions: h = [],
      argsMemoize: d = z,
      argsMemoizeOptions: p = [],
      devModeChecks: y = {}
    } = u, w = S(h), M = S(p), v = W(r), C = a(function() {
      return c++, l.apply(
        null,
        arguments
      );
    }, ...w);
    let g = !0;
    const O = d(function() {
      i++;
      const R = E(
        v,
        arguments
      );
      if (n = C.apply(null, R), process.env.NODE_ENV !== "production") {
        const { identityFunctionCheck: k, inputStabilityCheck: b } = q(g, y);
        if (k.shouldRun && k.run(
          l,
          R,
          n
        ), b.shouldRun) {
          const j = E(
            v,
            arguments
          );
          b.run(
            { inputSelectorResults: R, inputSelectorResultsCopy: j },
            { memoize: a, memoizeOptions: w },
            arguments
          );
        }
        g && (g = !1);
      }
      return n;
    }, ...M);
    return Object.assign(O, {
      resultFunc: l,
      memoizedResultFunc: C,
      dependencies: v,
      dependencyRecomputations: () => i,
      resetDependencyRecomputations: () => {
        i = 0;
      },
      lastResult: () => n,
      recomputations: () => c,
      resetRecomputations: () => {
        c = 0;
      },
      memoize: a,
      argsMemoize: d
    });
  };
  return Object.assign(o, {
    withTypes: () => o
  }), o;
}
var _ = /* @__PURE__ */ U(z), B = Object.assign(
  (e, t = _) => {
    D(
      e,
      `createStructuredSelector expects first argument to be an object where each property is a selector, instead received a ${typeof e}`
    );
    const s = Object.keys(e), o = s.map(
      (c) => e[c]
    );
    return t(
      o,
      (...c) => c.reduce((i, n, f) => (i[s[f]] = n, i), {})
    );
  },
  { withTypes: () => B }
);
export {
  _ as createSelector,
  U as createSelectorCreator,
  B as createStructuredSelector,
  z as weakMapMemoize
};
