"use strict";
const jsxRuntime = require("react/jsx-runtime");
const assert = require("assert");
const dedent = require("dedent");
const styles_lib_components_Box_BackgroundContext_cjs = require("../Box/BackgroundContext.cjs");
const styles_lib_hooks_useIcon_index_cjs = require("../../hooks/useIcon/index.cjs");
const styles_lib_components_Box_Box_cjs = require("../Box/Box.cjs");
const styles_lib_components_Accordion_AccordionItem_cjs = require("../Accordion/AccordionItem.cjs");
const styles_lib_components_Rating_Rating_css_cjs = require("./Rating.css.cjs");
const _interopDefaultCompat = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const assert__default = /* @__PURE__ */ _interopDefaultCompat(assert);
const dedent__default = /* @__PURE__ */ _interopDefaultCompat(dedent);
const IconStarHalfSvg = ({ title, titleId, ...props }) => /* @__PURE__ */ jsxRuntime.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__ */ jsxRuntime.jsx("title", { id: titleId, children: title }) : null,
      /* @__PURE__ */ jsxRuntime.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 = styles_lib_components_Box_BackgroundContext_cjs.useBackground();
  const { className, ...iconProps } = styles_lib_hooks_useIcon_index_cjs.useIcon(restProps);
  let component = styles_lib_components_Accordion_AccordionItem_cjs.IconStarSvg;
  if (percent >= 25 && percent < 75) {
    component = IconStarHalfSvg;
  }
  if (percent >= 75) {
    component = styles_lib_components_Accordion_AccordionItem_cjs.IconStarActiveSvg;
  }
  return /* @__PURE__ */ jsxRuntime.jsx(
    styles_lib_components_Box_Box_cjs.Box,
    {
      component,
      ...iconProps,
      className: [
        className,
        {
          [styles_lib_components_Rating_Rating_css_cjs.lightModeStarColor]: currentBg.lightMode === "body" || currentBg.lightMode === "surface"
        },
        {
          [styles_lib_components_Rating_Rating_css_cjs.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__default.default(
    !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__default.default`
          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__default.default`
          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__ */ jsxRuntime.jsxs(styles_lib_components_Accordion_AccordionItem_cjs.Text, { size, data, weight, children: [
    /* @__PURE__ */ jsxRuntime.jsx(
      styles_lib_components_Box_Box_cjs.Box,
      {
        display: "inlineBlock",
        "aria-label": ariaLabel || `${rating.toFixed(1)} out of ${ratingArr.length}`,
        children: resolvedVariant === "minimal" ? /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Box_Box_cjs.Box, { display: "inlineBlock", "aria-hidden": true, children: /* @__PURE__ */ jsxRuntime.jsx(RatingStar, { percent: 100 }) }) : ratingArr.map((_, position) => /* @__PURE__ */ jsxRuntime.jsx(
          styles_lib_components_Box_Box_cjs.Box,
          {
            display: "inlineBlock",
            "aria-hidden": true,
            className: {
              [styles_lib_components_Rating_Rating_css_cjs.starSpacing]: position !== ratingArr.length - 1
            },
            children: /* @__PURE__ */ jsxRuntime.jsx(RatingStar, { percent: getPercent(rating, position) })
          },
          position
        ))
      }
    ),
    resolvedVariant !== "starsOnly" && /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Box_Box_cjs.Box, { component: "span", className: styles_lib_components_Rating_Rating_css_cjs.textSpacing, "aria-hidden": true, children: rating.toFixed(1) })
  ] });
};
exports.Rating = Rating;
