"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
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;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Rating_exports = {};
__export(Rating_exports, {
  default: () => Rating_default
});
module.exports = __toCommonJS(Rating_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = __toESM(require("react"));
var import_styled_components = __toESM(require("styled-components"));
var import_prop_types = require("prop-types");
var import_yoga_icons = require("@gympass/yoga-icons");
var import_shared = require("../../shared");
const RatingWrapper = import_styled_components.default.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: import_yoga_icons.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 = import_yoga_icons.StarFilled,
      size: iconSize = rating.icon.size
    } = defaultIcon
  } = _b, rest = __objRest(_b, [
    "value",
    "max",
    "readOnly",
    "onRate",
    "onMouseOver",
    "onMouseMove",
    "onMouseLeave",
    "theme",
    "icon"
  ]);
  const wrapperRef = (0, import_react.useRef)(null);
  const [hover, setHover] = (0, import_react.useState)(false);
  const [currentRating, setCurrentRating] = (0, import_react.useState)(0);
  const VIEW_BOX_ICON_SIZE = 12;
  return /* @__PURE__ */ (0, import_jsx_runtime.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__ */ (0, import_jsx_runtime.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__ */ (0, import_jsx_runtime.jsxs)(import_react.default.Fragment, { children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              Icon,
              {
                fill: rating.backgroundColor,
                width,
                height: iconSize,
                viewBox: `0 0 ${wViewBox} ${VIEW_BOX_ICON_SIZE}`
              }
            ),
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              Icon,
              {
                fill: colors.elements.lineAndBorders,
                width: dWidth,
                height: iconSize,
                viewBox: `${wViewBox} 0 ${dViewBox} ${VIEW_BOX_ICON_SIZE}`
              }
            )
          ] }, `half-${i}`);
        }
        return (
          // unfilled star
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            Icon,
            {
              fill: colors.elements.lineAndBorders,
              width: iconSize,
              height: iconSize
            },
            `unfilled-${i}`
          )
        );
      })
    })
  );
};
Rating.propTypes = {
  value: import_prop_types.number,
  /** The icon to display */
  icon: (0, import_prop_types.shape)({
    type: import_prop_types.func,
    size: (0, import_shared.max)(24)
  }),
  /** Maximum rating */
  max: import_prop_types.number,
  /** false to make it interactable */
  readOnly: import_prop_types.bool,
  /** Event to be fired on click */
  onRate: import_prop_types.func,
  onMouseOver: import_prop_types.func,
  onMouseMove: import_prop_types.func,
  onMouseLeave: import_prop_types.func
};
var Rating_default = (0, import_styled_components.withTheme)(Rating);
