import { useContext } from "react";
import assert from "assert";
import clsx from "clsx";
import { TextContext } from "../../components/Accordion/AccordionItem.mjs";
import { HeadingContext } from "../../components/Heading/Heading.mjs";
import { lineHeightContainer } from "../../css/lineHeightContainer.css.mjs";
import { buildDataAttributes } from "../../components/Accordion/Accordion.mjs";
import { iconInlineSize } from "../../components/icons/IconRenderer.mjs";
import { atoms } from "../../css/atoms/atoms.mjs";
import { tone, textSizeUntrimmed } from "../../css/typography.css.mjs";
import { size, cropToTextSize, blockWidths } from "./icon.css.mjs";
const iconSize = ({
  size: size$1 = "standard",
  crop = false
} = {}) => clsx(size, textSizeUntrimmed[size$1], {
  [cropToTextSize]: crop
});
const iconContainerSize = (size2 = "standard") => clsx(blockWidths[size2], lineHeightContainer[size2]);
const defaultVerticalCorrection = {
  uppercase: "none",
  lowercase: "none"
};
const useIcon = ({ size: size2, tone: tone$1, alignY, data, title, titleId, ...restProps }, { verticalCorrection = defaultVerticalCorrection } = {}) => {
  const textContext = useContext(TextContext);
  const headingContext = useContext(HeadingContext);
  const resolvedTone = tone[tone$1 || (textContext == null ? void 0 : textContext.tone) || "neutral"];
  const toneClass = tone$1 || !headingContext && !(textContext == null ? void 0 : textContext.tone) ? resolvedTone : void 0;
  const isInline = Boolean(textContext || headingContext);
  const a11yProps = title ? { title, titleId, role: "img" } : { "aria-hidden": true };
  assert(
    !(size2 && isInline),
    `Specifying a custom \`size\` for an \`Icon\` inside the context of a \`<${textContext ? "Text" : "Heading"}>\` component is invalid. See the documentation for correct usage: https://seek-oss.github.io/braid-design-system/components/${textContext ? "Text" : "Heading"}`
  );
  assert(
    !(alignY && !isInline),
    `Specifying \`alignY\` for an \`Icon\` outside of a text component is invalid.`
  );
  if (size2 === "fill") {
    return {
      width: "full",
      height: "full",
      display: "block",
      className: toneClass,
      ...buildDataAttributes({ data, validateRestProps: restProps }),
      ...a11yProps
    };
  }
  return {
    className: [
      toneClass,
      isInline ? [
        iconInlineSize({
          alignY: alignY || "uppercase",
          verticalCorrection: verticalCorrection[alignY || "uppercase"]
        })
      ] : [
        atoms({
          display: "block"
        }),
        iconContainerSize(size2)
      ]
    ],
    ...buildDataAttributes({ data, validateRestProps: restProps }),
    ...a11yProps
  };
};
export {
  iconContainerSize,
  iconSize,
  useIcon
};
