import { jsxs, jsx } from 'react/jsx-runtime';
import { O as OctagonX, C as CircleCheck, t as toast } from './index-CDo_esrR.js';
import { c as cn } from './utils-BJnb9o5c.js';
import { T as TriangleAlert } from './triangle-alert-C434HydH.js';
import { I as Info } from './info-BlwkmMZr.js';
import { X } from './x-_o2T3n6D.js';

const ICONS = {
  success: CircleCheck,
  info: Info,
  warning: TriangleAlert,
  error: OctagonX
};
const VARIANT_CLASSES = {
  success: "bg-success-solid text-success-solid-foreground border-success-solid/30",
  error: "bg-error-solid text-error-solid-foreground border-error-solid/30",
  warning: "bg-warning-solid text-warning-solid-foreground border-warning-solid/30",
  info: "bg-info-solid text-info-solid-foreground border-info-solid/30"
};
const ToastrBody = ({
  type,
  message,
  showIcon = true,
  dismissible = true,
  buttonLabel,
  onAction,
  onDismiss
}) => {
  const Icon = ICONS[type];
  return /* @__PURE__ */ jsxs(
    "div",
    {
      role: type === "error" || type === "warning" ? "alert" : "status",
      className: cn(
        "flex w-full items-center gap-3 rounded-md border px-4 py-3 text-sm shadow-lg",
        VARIANT_CLASSES[type]
      ),
      children: [
        showIcon && /* @__PURE__ */ jsx(Icon, { "aria-hidden": true, className: "size-4 shrink-0" }),
        /* @__PURE__ */ jsx("span", { className: "flex-1", children: message }),
        buttonLabel && /* @__PURE__ */ jsx(
          "button",
          {
            type: "button",
            onClick: onAction,
            className: "shrink-0 text-sm font-medium underline-offset-2 hover:underline",
            children: buttonLabel
          }
        ),
        dismissible && /* @__PURE__ */ jsx(
          "button",
          {
            type: "button",
            "aria-label": "Dismiss",
            onClick: onDismiss,
            className: "shrink-0 rounded-sm opacity-70 transition-opacity hover:opacity-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2",
            children: /* @__PURE__ */ jsx(X, { "aria-hidden": true, className: "size-4" })
          }
        )
      ]
    }
  );
};

const activeToasts = /* @__PURE__ */ new Set();
function toastKey(type, message, buttonLabel) {
  return `${type}::${message}::${buttonLabel ?? ""}`;
}
function getContentDir() {
  if (typeof document === "undefined") return "ltr";
  return document.documentElement.getAttribute("dir") ?? document.body.getAttribute("dir") ?? "ltr";
}
function resolveMessage(message) {
  if (typeof message === "string") return message;
  if (message && typeof message === "object") {
    if (message.customMessage) return message.customMessage;
    if (message.notice) return message.notice;
    if (message.noticeCode) return message.noticeCode;
  }
  return String(message);
}
function resolveErrorMessage(errorObject) {
  if (typeof errorObject === "string") return errorObject;
  if (errorObject && typeof errorObject === "object") {
    const axiosLike = errorObject;
    if (axiosLike.response?.data) {
      const data = axiosLike.response.data;
      if (data.customMessage) return String(data.customMessage);
      if (data.error) return String(data.error);
      if (Array.isArray(data.errors)) return data.errors.join("\n");
      if (data.noticeCode) return String(data.noticeCode);
    }
    if (axiosLike.isAxiosError && axiosLike.message) return axiosLike.message;
    if (errorObject instanceof Error) return errorObject.message;
    return resolveMessage(errorObject);
  }
  return "Something went wrong.";
}
function parseConfig(args) {
  if (args.length === 0) return {};
  const first = args[0];
  if (typeof first === "object" && first !== null && !Array.isArray(first)) {
    return first;
  }
  const [buttonLabel, onClick, customConfig, showIcon] = args;
  return {
    ...buttonLabel ? { buttonLabel } : {},
    ...onClick ? { onClick } : {},
    ...showIcon !== void 0 ? { showIcon } : {},
    ...customConfig ?? {}
  };
}

function showToast(type, message, config) {
  const {
    buttonLabel,
    onClick,
    showIcon,
    dismissible = true,
    onDismiss: userOnDismiss,
    onAutoClose: userOnAutoClose,
    ...restConfig
  } = config ?? {};
  const key = toastKey(type, message, buttonLabel);
  if (activeToasts.has(key)) return void 0;
  activeToasts.add(key);
  const options = {
    ...restConfig,
    position: getContentDir() === "rtl" ? "bottom-right" : "bottom-left",
    dismissible,
    onDismiss: (t) => {
      activeToasts.delete(key);
      userOnDismiss?.(t);
    },
    onAutoClose: (t) => {
      activeToasts.delete(key);
      userOnAutoClose?.(t);
    }
  };
  return toast.custom(
    (id) => /* @__PURE__ */ jsx(
      ToastrBody,
      {
        buttonLabel,
        dismissible,
        message,
        showIcon,
        type,
        onAction: () => {
          try {
            onClick?.();
          } finally {
            toast.dismiss(id);
          }
        },
        onDismiss: () => toast.dismiss(id)
      }
    ),
    options
  );
}
const Toastr = {
  success(message, ...args) {
    return showToast("success", resolveMessage(message), parseConfig(args));
  },
  info(message, ...args) {
    return showToast("info", resolveMessage(message), parseConfig(args));
  },
  show(message, ...args) {
    return showToast("info", resolveMessage(message), parseConfig(args));
  },
  warning(message, ...args) {
    return showToast("warning", resolveMessage(message), parseConfig(args));
  },
  error(errorObject, ...args) {
    const msg = resolveErrorMessage(errorObject);
    return showToast("error", msg, parseConfig(args));
  }
};

export { Toastr as T };
//# sourceMappingURL=Toastr-tmAtpmwv.js.map
