import { jsxs, jsx } from "react/jsx-runtime";
import assert from "assert";
import dedent from "dedent";
import { useBackground } from "../Box/BackgroundContext.mjs";
import { useIcon } from "../../hooks/useIcon/index.mjs";
import { Box } from "../Box/Box.mjs";
import { Text, IconStarSvg, IconStarActiveSvg } from "../Accordion/AccordionItem.mjs";
import { starSpacing, textSpacing, lightModeStarColor, darkModeStarColor } from "./Rating.css.mjs";
const IconStarHalfSvg = ({ title, titleId, ...props }) => /* @__PURE__ */ jsxs(
  "svg",
  {
    xmlns: "http://www.w3.org/2000/svg",
    width: 16,
    height: 16,
    viewBox: "0 0 24 24",
    focusable: "false",
    fill: "currentColor",
    "aria-labelledby": titleId,
    ...props,
    children: [
      title ? /* @__PURE__ */ jsx("title", { id: titleId, children: title }) : null,
      /* @__PURE__ */ jsx("path", { d: "M22.951 8.954a1 1 0 0 0-.806-.68l-6.391-.935-2.858-5.782a1.042 1.042 0 0 0-1.793 0L8.246 7.339l-6.39.934a1 1 0 0 0-.553 1.706l4.623 4.498-1.091 6.354a1 1 0 0 0 1.45 1.054L12 18.883l5.715 3.002a1 1 0 0 0 1.45-1.054l-1.09-6.354 4.622-4.497a1.002 1.002 0 0 0 .254-1.026Zm-6.648 4.456a1 1 0 0 0-.288.886l.837 4.877-4.387-2.305a.99.99 0 0 0-.465-.115V4.257l2.193 4.44a1 1 0 0 0 .752.545l4.905.717Z" })
    ]
  }
);
const getPercent = (rating, position) => Math.round(Math.min(Math.max(rating - position, 0), 1) * 100);
const RatingStar = ({ percent, ...restProps }) => {
  const currentBg = useBackground();
  const { className, ...iconProps } = useIcon(restProps);
  let component = IconStarSvg;
  if (percent >= 25 && percent < 75) {
    component = IconStarHalfSvg;
  }
  if (percent >= 75) {
    component = IconStarActiveSvg;
  }
  return /* @__PURE__ */ jsx(
    Box,
    {
      component,
      ...iconProps,
      className: [
        className,
        {
          [lightModeStarColor]: currentBg.lightMode === "body" || currentBg.lightMode === "surface"
        },
        {
          [darkModeStarColor]: currentBg.darkMode === "body" || currentBg.darkMode === "surface"
        }
      ]
    }
  );
};
const ratingArr = [...Array(5)];
const Rating = ({
  rating,
  size = "standard",
  weight,
  showTextRating,
  variant: variantProp,
  "aria-label": ariaLabel,
  data
}) => {
  assert(
    !rating || rating >= 0 && rating <= 5,
    "Rating must be between 0 and 5"
  );
  const variant = variantProp || "full";
  const resolvedVariant = showTextRating === false && !variantProp ? "starsOnly" : variant;
  if (process.env.NODE_ENV !== "production") {
    if (typeof showTextRating !== "undefined") {
      console.warn(
        dedent`
          The "showTextRating" prop has been deprecated and will be removed in a future version. Use \`variant="starsOnly"\` instead.
             <Rating
            %c-   showTextRating={false}
            %c+   variant="starsOnly"
             %c/>
        `,
        "color: red",
        "color: green",
        "color: inherit"
      );
    }
    if (typeof weight !== "undefined" && resolvedVariant === "starsOnly") {
      console.warn(
        dedent`
          The "weight" prop is not valid with the "starsOnly" variant as there is no visible text. Either remove the \`weight\` prop or choose an alternative \`variant\`.
             <Rating
            %c-  weight="strong"
            %c   variant="starsOnly"
             />
        `,
        "color: red",
        "color: inherit"
      );
    }
  }
  return /* @__PURE__ */ jsxs(Text, { size, data, weight, children: [
    /* @__PURE__ */ jsx(
      Box,
      {
        display: "inlineBlock",
        "aria-label": ariaLabel || `${rating.toFixed(1)} out of ${ratingArr.length}`,
        children: resolvedVariant === "minimal" ? /* @__PURE__ */ jsx(Box, { display: "inlineBlock", "aria-hidden": true, children: /* @__PURE__ */ jsx(RatingStar, { percent: 100 }) }) : ratingArr.map((_, position) => /* @__PURE__ */ jsx(
          Box,
          {
            display: "inlineBlock",
            "aria-hidden": true,
            className: {
              [starSpacing]: position !== ratingArr.length - 1
            },
            children: /* @__PURE__ */ jsx(RatingStar, { percent: getPercent(rating, position) })
          },
          position
        ))
      }
    ),
    resolvedVariant !== "starsOnly" && /* @__PURE__ */ jsx(Box, { component: "span", className: textSpacing, "aria-hidden": true, children: rating.toFixed(1) })
  ] });
};
export {
  Rating
};
