var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import { jsx, jsxs } from "react/jsx-runtime";
import React, { useState, useRef } from "react";
import styled, { withTheme } from "styled-components";
import { number, func, shape, bool } from "prop-types";
import { StarFilled } from "@gympass/yoga-icons";
import { max as maxPropType } from "../../shared";
const RatingWrapper = styled.div`
  display: inline-flex;

  :hover {
    cursor: pointer;
  }

  svg {
    pointer-events: none;
  }

  ${({ width, height, readOnly }) => `
    width: ${width}px;
    height: ${height}px;

    ${readOnly ? "pointer-events: none;" : ""}
  `}
`;
const noop = () => {
};
const defaultIcon = {
  type: StarFilled,
  size: 12
};
const Rating = (_a) => {
  var _b = _a, {
    value,
    max = 5,
    readOnly = true,
    onRate = noop,
    onMouseOver = noop,
    onMouseMove = noop,
    onMouseLeave = noop,
    theme: {
      yoga: {
        colors,
        components: { rating }
      }
    },
    icon: {
      type: Icon = StarFilled,
      size: iconSize = rating.icon.size
    } = defaultIcon
  } = _b, rest = __objRest(_b, [
    "value",
    "max",
    "readOnly",
    "onRate",
    "onMouseOver",
    "onMouseMove",
    "onMouseLeave",
    "theme",
    "icon"
  ]);
  const wrapperRef = useRef(null);
  const [hover, setHover] = useState(false);
  const [currentRating, setCurrentRating] = useState(0);
  const VIEW_BOX_ICON_SIZE = 12;
  return /* @__PURE__ */ jsx(
    RatingWrapper,
    __spreadProps(__spreadValues({
      width: iconSize * max,
      height: iconSize,
      mouseOver: hover,
      readOnly
    }, rest), {
      onMouseOver: (e) => {
        setHover(true);
        onMouseOver(e);
      },
      onMouseMove: (e) => {
        setCurrentRating(
          Math.ceil(
            (e.clientX - e.target.offsetLeft) / (wrapperRef.current.offsetWidth / max)
          ) || 1
        );
        onMouseMove(e);
      },
      onMouseLeave: (e) => {
        setHover(false);
        setCurrentRating(0);
        onMouseLeave(e);
      },
      onClick: () => onRate(currentRating),
      ref: wrapperRef,
      children: Array.from({ length: max }, (_, i) => {
        const diff = i + 1 - value;
        let width;
        if (currentRating >= i + 1 || !hover && diff <= 0) {
          return /* @__PURE__ */ jsx(
            Icon,
            {
              fill: rating.backgroundColor,
              width: iconSize,
              height: iconSize
            },
            `filled-${i}`
          );
        }
        if (!hover && diff > 0 && diff < 1) {
          width = (1 - diff) * iconSize;
          const dWidth = diff * iconSize;
          const wViewBox = VIEW_BOX_ICON_SIZE * (1 - diff);
          const dViewBox = VIEW_BOX_ICON_SIZE * diff;
          return /* @__PURE__ */ jsxs(React.Fragment, { children: [
            /* @__PURE__ */ jsx(
              Icon,
              {
                fill: rating.backgroundColor,
                width,
                height: iconSize,
                viewBox: `0 0 ${wViewBox} ${VIEW_BOX_ICON_SIZE}`
              }
            ),
            /* @__PURE__ */ jsx(
              Icon,
              {
                fill: colors.elements.lineAndBorders,
                width: dWidth,
                height: iconSize,
                viewBox: `${wViewBox} 0 ${dViewBox} ${VIEW_BOX_ICON_SIZE}`
              }
            )
          ] }, `half-${i}`);
        }
        return (
          // unfilled star
          /* @__PURE__ */ jsx(
            Icon,
            {
              fill: colors.elements.lineAndBorders,
              width: iconSize,
              height: iconSize
            },
            `unfilled-${i}`
          )
        );
      })
    })
  );
};
Rating.propTypes = {
  value: number,
  /** The icon to display */
  icon: shape({
    type: func,
    size: maxPropType(24)
  }),
  /** Maximum rating */
  max: number,
  /** false to make it interactable */
  readOnly: bool,
  /** Event to be fired on click */
  onRate: func,
  onMouseOver: func,
  onMouseMove: func,
  onMouseLeave: func
};
var Rating_default = withTheme(Rating);
export {
  Rating_default as default
};
