import * as S from "react";
const A = 1, f = 1e3;
let u = 0;
function O() {
  return u = (u + 1) % Number.MAX_SAFE_INTEGER, u.toString();
}
const a = /* @__PURE__ */ new Map(), d = (t) => {
  if (a.has(t))
    return;
  const e = setTimeout(() => {
    a.delete(t), n({
      type: "REMOVE_TOAST",
      toastId: t
    });
  }, f);
  a.set(t, e);
}, E = (t, e) => {
  switch (e.type) {
    case "ADD_TOAST":
      return {
        ...t,
        toasts: [e.toast, ...t.toasts].slice(0, A)
      };
    case "UPDATE_TOAST":
      return {
        ...t,
        toasts: t.toasts.map(
          (s) => s.id === e.toast.id ? { ...s, ...e.toast } : s
        )
      };
    case "DISMISS_TOAST": {
      const { toastId: s } = e;
      return s ? d(s) : t.toasts.forEach((o) => {
        d(o.id);
      }), {
        ...t,
        toasts: t.toasts.map(
          (o) => o.id === s || s === void 0 ? {
            ...o,
            open: !1
          } : o
        )
      };
    }
    case "REMOVE_TOAST":
      return e.toastId === void 0 ? {
        ...t,
        toasts: []
      } : {
        ...t,
        toasts: t.toasts.filter((s) => s.id !== e.toastId)
      };
  }
}, r = [];
let T = { toasts: [] };
function n(t) {
  T = E(T, t), r.forEach((e) => {
    e(T);
  });
}
function _({ duration: t, ...e }) {
  const s = {
    duration: t ?? 2e3,
    ...e
  }, o = O(), p = (i) => n({
    type: "UPDATE_TOAST",
    toast: { ...i, id: o }
  }), c = () => n({ type: "DISMISS_TOAST", toastId: o });
  return n({
    type: "ADD_TOAST",
    toast: {
      ...s,
      id: o,
      open: !0,
      onOpenChange: (i) => {
        i || c();
      }
    }
  }), {
    id: o,
    dismiss: c,
    update: p
  };
}
function m() {
  const [t, e] = S.useState(T);
  return S.useEffect(() => (r.push(e), () => {
    const s = r.indexOf(e);
    s > -1 && r.splice(s, 1);
  }), [t]), {
    ...t,
    toast: _,
    dismiss: (s) => n({ type: "DISMISS_TOAST", toastId: s })
  };
}
export {
  E as reducer,
  _ as toast,
  m as useToast
};
