"use client";
import { jsx } from "react/jsx-runtime";
import * as React from "react";
import { Alert } from "../primitives/alert.js";
const flashKindToAlertStatus = {
  success: "success",
  error: "error",
  warning: "warning",
  info: "information"
};
function flash(message, type = "info") {
  if (typeof window === "undefined") return;
  const evt = new CustomEvent("flash", { detail: { message, type } });
  window.dispatchEvent(evt);
}
function useLocationKey() {
  const [locationKey, setLocationKey] = React.useState(() => {
    if (typeof window === "undefined") return "";
    return window.location.href;
  });
  const locationKeyRef = React.useRef(locationKey);
  React.useEffect(() => {
    locationKeyRef.current = locationKey;
  }, [locationKey]);
  React.useEffect(() => {
    if (typeof window === "undefined") return;
    let active = true;
    const scheduleUpdate = (href) => {
      const run = () => {
        if (!active) return;
        if (locationKeyRef.current === href) return;
        locationKeyRef.current = href;
        setLocationKey(href);
      };
      if (typeof queueMicrotask === "function") {
        queueMicrotask(run);
      } else {
        setTimeout(run, 0);
      }
    };
    const updateLocation = () => {
      if (!active) return;
      const href = window.location.href;
      if (href === locationKeyRef.current) return;
      scheduleUpdate(href);
    };
    const deferredUpdateLocation = () => {
      setTimeout(updateLocation, 0);
    };
    const originalPush = window.history.pushState.bind(window.history);
    const originalReplace = window.history.replaceState.bind(window.history);
    const pushState = (...args) => {
      originalPush(...args);
      deferredUpdateLocation();
    };
    const replaceState = (...args) => {
      originalReplace(...args);
      deferredUpdateLocation();
    };
    window.history.pushState = pushState;
    window.history.replaceState = replaceState;
    window.addEventListener("popstate", updateLocation);
    window.addEventListener("hashchange", updateLocation);
    updateLocation();
    return () => {
      active = false;
      window.history.pushState = originalPush;
      window.history.replaceState = originalReplace;
      window.removeEventListener("popstate", updateLocation);
      window.removeEventListener("hashchange", updateLocation);
    };
  }, []);
  return locationKey;
}
function FlashMessagesInner() {
  const [msg, setMsg] = React.useState(null);
  const [kind, setKind] = React.useState("info");
  const locationKey = useLocationKey();
  const dismissTimerRef = React.useRef(null);
  const clearDismissTimer = React.useCallback(() => {
    if (dismissTimerRef.current) {
      clearTimeout(dismissTimerRef.current);
      dismissTimerRef.current = null;
    }
  }, []);
  const showFlash = React.useCallback((message, type) => {
    clearDismissTimer();
    setMsg(message);
    setKind(type);
    dismissTimerRef.current = setTimeout(() => {
      dismissTimerRef.current = null;
      setMsg(null);
    }, 3e3);
  }, [clearDismissTimer]);
  React.useEffect(() => {
    return () => {
      clearDismissTimer();
    };
  }, [clearDismissTimer]);
  React.useEffect(() => {
    if (typeof window === "undefined") return;
    const url = new URL(window.location.href);
    const message = url.searchParams.get("flash");
    const type = url.searchParams.get("type") || "success";
    if (message) {
      showFlash(message, type);
      url.searchParams.delete("flash");
      url.searchParams.delete("type");
      window.history.replaceState({}, "", url.toString());
    }
  }, [locationKey, showFlash]);
  React.useEffect(() => {
    const handler = (e) => {
      const ce = e;
      const text = ce.detail?.message;
      const t = ce.detail?.type || "info";
      if (!text) return;
      showFlash(text, t);
    };
    window.addEventListener("flash", handler);
    return () => window.removeEventListener("flash", handler);
  }, [showFlash]);
  const handleDismiss = React.useCallback(() => {
    clearDismissTimer();
    setMsg(null);
  }, [clearDismissTimer]);
  if (!msg) return null;
  return /* @__PURE__ */ jsx("div", { className: "pointer-events-none fixed left-3 right-3 top-3 z-toast sm:left-auto sm:right-4 sm:w-[380px]", children: /* @__PURE__ */ jsx("div", { className: "pointer-events-auto", children: /* @__PURE__ */ jsx(
    Alert,
    {
      status: flashKindToAlertStatus[kind],
      size: "sm",
      dismissible: true,
      onDismiss: handleDismiss,
      className: "shadow-md",
      children: msg
    }
  ) }) });
}
function FlashMessages() {
  return /* @__PURE__ */ jsx(React.Suspense, { fallback: null, children: /* @__PURE__ */ jsx(FlashMessagesInner, {}) });
}
export {
  FlashMessages,
  flash
};
//# sourceMappingURL=FlashMessages.js.map
