import "../Toast.css";
import { jsx, jsxs } from "react/jsx-runtime";
import { notification as notification$2, App } from "antd";
import { useEffect, useRef, useLayoutEffect } from "react";
import { useTheme, UIProvider } from "./Theme/UI.js";
import "./Theme/themeUtils.js";
import "./Theme/themes.js";
import Icon from "./Icon.js";
import { findParentByClass } from "./internal/functions.js";
const message = "Toast_message_fd7a6";
const notification$1 = "Toast_notification_fd7a6";
const css = {
  message,
  notification: notification$1
};
let notification = notification$2;
const useInitApi = () => {
  const api = App.useApp();
  useEffect(() => {
    ({ notification } = api);
  }, [api]);
};
const ToastThemeProvider = ({ children }) => {
  const ref = useRef(null);
  const {
    themeSettings: { className }
  } = useTheme();
  useLayoutEffect(() => {
    if (ref.current) {
      const notificationContainer = findParentByClass(ref.current, "ant-notification");
      notificationContainer.classList.add(className);
    }
  });
  return /* @__PURE__ */ jsx("div", { ref, children });
};
const getIconName = (s) => {
  if (s === "Confirm")
    return "checkmark";
  return s.toLowerCase();
};
const makeToast = ({
  title,
  severity = "Info",
  closeable = true,
  duration = 8,
  description,
  link
}) => {
  const args = {
    className: css.notification,
    closeIcon: closeable ? /* @__PURE__ */ jsx(Icon, { decorative: true, name: "close" }) : null,
    description: description ? link ? /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx("p", { children: description }),
      link
    ] }) : description : void 0,
    duration,
    message: /* @__PURE__ */ jsx(ToastThemeProvider, { children: /* @__PURE__ */ jsxs("div", { className: css.message, children: [
      /* @__PURE__ */ jsx(Icon, { decorative: true, name: getIconName(severity) }),
      /* @__PURE__ */ jsx("span", { children: title })
    ] }) })
  };
  notification.open(args);
};
const useToast = () => {
  const {
    themeSettings: { theme, themeIsDark, className }
  } = useTheme();
  const openToast = ({
    title,
    severity = "Info",
    closeable = true,
    duration = 4.5,
    description,
    link
  }) => {
    const args = {
      className: [className, css.notification].join(" "),
      closeIcon: closeable ? /* @__PURE__ */ jsx(UIProvider, { theme, themeIsDark, children: /* @__PURE__ */ jsx(Icon, { decorative: true, name: "close" }) }) : null,
      description: description ? link ? /* @__PURE__ */ jsx(UIProvider, { theme, themeIsDark, children: /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("p", { children: description }),
        link
      ] }) }) : description : void 0,
      duration,
      message: /* @__PURE__ */ jsx(UIProvider, { theme, themeIsDark, children: /* @__PURE__ */ jsxs("div", { className: css.message, children: [
        /* @__PURE__ */ jsx(Icon, { decorative: true, name: getIconName(severity) }),
        title
      ] }) })
    };
    notification.open(args);
  };
  return { openToast };
};
export {
  makeToast,
  notification,
  useInitApi,
  useToast
};
