import React, {
  createContext,
  useContext,
  Suspense
} from "react";
import { ErrorBoundary } from "react-error-boundary";
import { iconContentFetcher } from "./fetcher";
import { EmptyState } from "./components/empty-state";
import { getIconAttrs } from "./utils";
if (!globalThis.__WDSIconThemeContext__) {
  globalThis.__WDSIconThemeContext__ = createContext("default");
}
const IconThemeContext = globalThis.__WDSIconThemeContext__;
const IconThemeProvider = ({
  theme = "default",
  children
}) => /* @__PURE__ */ React.createElement(IconThemeContext.Provider, { value: theme }, children);
const icon = (params) => {
  const { name, hashes, category, width, height } = params;
  const IconContent = (props) => {
    const { size, ...restProps } = props;
    const selectedTheme = useContext(IconThemeContext);
    let themeToUse = selectedTheme;
    let hash = hashes[themeToUse];
    if (!hash) {
      themeToUse = hashes.default ? "default" : "odeditor";
      hash = hashes.default || hashes.odeditor;
    }
    const content = iconContentFetcher.fetch({
      name,
      theme: themeToUse,
      category,
      hash
    });
    const { dimensions, viewBox } = getIconAttrs({
      size,
      width,
      height
    });
    return /* @__PURE__ */ React.createElement("svg", { fill: "currentColor", viewBox, ...dimensions, ...restProps }, /* @__PURE__ */ React.createElement(
      "g",
      {
        ...{
          // eslint-disable-next-line no-useless-concat
          ["dangerouslySetInnerHTML"]: { __html: content }
        }
      }
    ));
  };
  const Icon = (props) => {
    const { size, ...restProps } = props;
    return /* @__PURE__ */ React.createElement(
      ErrorBoundary,
      {
        fallback: /* @__PURE__ */ React.createElement(
          EmptyState,
          {
            width,
            height,
            size,
            restProps
          }
        )
      },
      /* @__PURE__ */ React.createElement(
        Suspense,
        {
          fallback: /* @__PURE__ */ React.createElement(
            EmptyState,
            {
              width,
              height,
              size,
              restProps
            }
          )
        },
        /* @__PURE__ */ React.createElement(IconContent, { ...props })
      )
    );
  };
  Icon.displayName = `Icon(${params.name})`;
  return Icon;
};
export {
  IconThemeContext,
  IconThemeProvider,
  icon
};
