const H = {}, w = (d) => (m, a, o) => {
  const i = o.subscribe;
  return o.subscribe = (v, c, u) => {
    let s = v;
    if (c) {
      const I = u?.equalityFn || Object.is;
      let g = v(o.getState());
      s = (y) => {
        const h = v(y);
        if (!I(g, h)) {
          const S = g;
          c(g = h, S);
        }
      }, u?.fireImmediately && c(g, g);
    }
    return i(s);
  }, d(m, a, o);
}, J = w;
function R(d, m) {
  let a;
  try {
    a = d();
  } catch {
    return;
  }
  return {
    getItem: (i) => {
      var e;
      const v = (u) => u === null ? null : JSON.parse(u, void 0), c = (e = a.getItem(i)) != null ? e : null;
      return c instanceof Promise ? c.then(v) : v(c);
    },
    setItem: (i, e) => a.setItem(
      i,
      JSON.stringify(e, void 0)
    ),
    removeItem: (i) => a.removeItem(i)
  };
}
const b = (d) => (m) => {
  try {
    const a = d(m);
    return a instanceof Promise ? a : {
      then(o) {
        return b(o)(a);
      },
      catch(o) {
        return this;
      }
    };
  } catch (a) {
    return {
      then(o) {
        return this;
      },
      catch(o) {
        return b(o)(a);
      }
    };
  }
}, E = (d, m) => (a, o, i) => {
  let e = {
    getStorage: () => localStorage,
    serialize: JSON.stringify,
    deserialize: JSON.parse,
    partialize: (r) => r,
    version: 0,
    merge: (r, l) => ({
      ...l,
      ...r
    }),
    ...m
  }, v = !1;
  const c = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Set();
  let s;
  try {
    s = e.getStorage();
  } catch {
  }
  if (!s)
    return d(
      (...r) => {
        console.warn(
          `[zustand persist middleware] Unable to update item '${e.name}', the given storage is currently unavailable.`
        ), a(...r);
      },
      o,
      i
    );
  const I = b(e.serialize), g = () => {
    const r = e.partialize({ ...o() });
    let l;
    const t = I({ state: r, version: e.version }).then(
      (f) => s.setItem(e.name, f)
    ).catch((f) => {
      l = f;
    });
    if (l)
      throw l;
    return t;
  }, y = i.setState;
  i.setState = (r, l) => {
    y(r, l), g();
  };
  const h = d(
    (...r) => {
      a(...r), g();
    },
    o,
    i
  );
  let S;
  const n = () => {
    var r;
    if (!s) return;
    v = !1, c.forEach((t) => t(o()));
    const l = ((r = e.onRehydrateStorage) == null ? void 0 : r.call(e, o())) || void 0;
    return b(s.getItem.bind(s))(e.name).then((t) => {
      if (t)
        return e.deserialize(t);
    }).then((t) => {
      if (t)
        if (typeof t.version == "number" && t.version !== e.version) {
          if (e.migrate)
            return e.migrate(
              t.state,
              t.version
            );
          console.error(
            "State loaded from storage couldn't be migrated since no migrate function was provided"
          );
        } else
          return t.state;
    }).then((t) => {
      var f;
      return S = e.merge(
        t,
        (f = o()) != null ? f : h
      ), a(S, !0), g();
    }).then(() => {
      l?.(S, void 0), v = !0, u.forEach((t) => t(S));
    }).catch((t) => {
      l?.(void 0, t);
    });
  };
  return i.persist = {
    setOptions: (r) => {
      e = {
        ...e,
        ...r
      }, r.getStorage && (s = r.getStorage());
    },
    clearStorage: () => {
      s?.removeItem(e.name);
    },
    getOptions: () => e,
    rehydrate: () => n(),
    hasHydrated: () => v,
    onHydrate: (r) => (c.add(r), () => {
      c.delete(r);
    }),
    onFinishHydration: (r) => (u.add(r), () => {
      u.delete(r);
    })
  }, n(), S || h;
}, F = (d, m) => (a, o, i) => {
  let e = {
    storage: R(() => localStorage),
    partialize: (n) => n,
    version: 0,
    merge: (n, r) => ({
      ...r,
      ...n
    }),
    ...m
  }, v = !1;
  const c = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Set();
  let s = e.storage;
  if (!s)
    return d(
      (...n) => {
        console.warn(
          `[zustand persist middleware] Unable to update item '${e.name}', the given storage is currently unavailable.`
        ), a(...n);
      },
      o,
      i
    );
  const I = () => {
    const n = e.partialize({ ...o() });
    return s.setItem(e.name, {
      state: n,
      version: e.version
    });
  }, g = i.setState;
  i.setState = (n, r) => {
    g(n, r), I();
  };
  const y = d(
    (...n) => {
      a(...n), I();
    },
    o,
    i
  );
  i.getInitialState = () => y;
  let h;
  const S = () => {
    var n, r;
    if (!s) return;
    v = !1, c.forEach((t) => {
      var f;
      return t((f = o()) != null ? f : y);
    });
    const l = ((r = e.onRehydrateStorage) == null ? void 0 : r.call(e, (n = o()) != null ? n : y)) || void 0;
    return b(s.getItem.bind(s))(e.name).then((t) => {
      if (t)
        if (typeof t.version == "number" && t.version !== e.version) {
          if (e.migrate)
            return [
              !0,
              e.migrate(
                t.state,
                t.version
              )
            ];
          console.error(
            "State loaded from storage couldn't be migrated since no migrate function was provided"
          );
        } else
          return [!1, t.state];
      return [!1, void 0];
    }).then((t) => {
      var f;
      const [p, _] = t;
      if (h = e.merge(
        _,
        (f = o()) != null ? f : y
      ), a(h, !0), p)
        return I();
    }).then(() => {
      l?.(h, void 0), h = o(), v = !0, u.forEach((t) => t(h));
    }).catch((t) => {
      l?.(void 0, t);
    });
  };
  return i.persist = {
    setOptions: (n) => {
      e = {
        ...e,
        ...n
      }, n.storage && (s = n.storage);
    },
    clearStorage: () => {
      s?.removeItem(e.name);
    },
    getOptions: () => e,
    rehydrate: () => S(),
    hasHydrated: () => v,
    onHydrate: (n) => (c.add(n), () => {
      c.delete(n);
    }),
    onFinishHydration: (n) => (u.add(n), () => {
      u.delete(n);
    })
  }, e.skipHydration || S(), h || y;
}, z = (d, m) => "getStorage" in m || "serialize" in m || "deserialize" in m ? ((H ? "production" : void 0) !== "production" && console.warn(
  "[DEPRECATED] `getStorage`, `serialize` and `deserialize` options are deprecated. Use `storage` option instead."
), E(d, m)) : F(d, m), N = z;
export {
  R as createJSONStorage,
  N as persist,
  J as subscribeWithSelector
};
//# sourceMappingURL=middleware.js.map
