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, useEffect } from "react";
import { PanResponder } from "react-native";
import styled, { withTheme } from "styled-components";
import { number, func, bool, shape } from "prop-types";
import { StarFilled } from "@gympass/yoga-icons";
import { max as maxPropType } from "../../shared";
const SVG_DEFAULT_SIZE = 12;
const RatingWrapper = styled.View`
  flex-direction: row;
  justify-content: center;
  align-items: center;

  ${({ width, height }) => `
    width: ${width}px;
    height: ${height}px;
  `}
`;
const Rating = (_a) => {
  var _b = _a, {
    value = void 0,
    max = 5,
    readOnly = true,
    onRate = {},
    theme: {
      yoga: {
        colors,
        components: { rating }
      }
    },
    icon: { type: Icon = StarFilled, size: iconSize = 24 } = {
      type: StarFilled,
      size: 24
    }
  } = _b, rest = __objRest(_b, [
    "value",
    "max",
    "readOnly",
    "onRate",
    "theme",
    "icon"
  ]);
  const [panResponder, setPanResponder] = useState({});
  const [wrapperLayout, setWrapperLayout] = useState({});
  const [swipeRating, setSwipeRating] = useState(0);
  const [swipping, toggleSwipping] = useState(false);
  function getCurrentRating(swipeValue) {
    let currentRating = 1;
    if (swipeValue > wrapperLayout.width) {
      currentRating = max;
    } else if (swipeValue <= 0) {
      currentRating = 0;
    } else {
      currentRating = Math.round(swipeValue / (wrapperLayout.width / max));
    }
    return currentRating || 1;
  }
  useEffect(() => {
    const responder = PanResponder.create({
      onStartShouldSetPanResponder: () => true,
      onStartShouldSetPanResponderCapture: () => true,
      onMoveShouldSetPanResponder: () => true,
      onMoveShouldSetPanResponderCapture: () => true,
      onPanResponderTerminationRequest: () => true,
      onPanResponderGrant: ({ nativeEvent: { pageX } }) => {
        toggleSwipping(true);
        setSwipeRating(getCurrentRating(pageX - wrapperLayout.x));
      },
      onPanResponderMove: ({ nativeEvent: { pageX } }) => {
        setSwipeRating(getCurrentRating(pageX - wrapperLayout.x));
      },
      onPanResponderRelease: ({ nativeEvent: { pageX } }) => {
        setSwipeRating(0);
        toggleSwipping(false);
        onRate(getCurrentRating(pageX - wrapperLayout.x));
      }
    });
    setPanResponder(responder);
  }, [wrapperLayout]);
  return /* @__PURE__ */ jsx(
    RatingWrapper,
    __spreadProps(__spreadValues(__spreadValues({
      height: iconSize,
      width: iconSize * max,
      pointerEvents: readOnly ? "none" : "auto"
    }, rest), panResponder.panHandlers), {
      onLayout: ({ nativeEvent: { layout } }) => setWrapperLayout(layout),
      children: Array.from({ length: max }, (_, i) => {
        const diff = i + 1 - value;
        if (swipeRating >= i + 1 || !swipping && diff <= 0) {
          return /* @__PURE__ */ jsx(
            Icon,
            {
              fill: rating.backgroundColor,
              width: iconSize,
              height: iconSize
            },
            `filled-${i}`
          );
        }
        if (diff > 0 && diff < 1) {
          const width = (1 - diff) * iconSize;
          const dWidth = diff * iconSize;
          const wViewBox = SVG_DEFAULT_SIZE * (1 - diff);
          const dViewBox = SVG_DEFAULT_SIZE * diff;
          return /* @__PURE__ */ jsxs(React.Fragment, { children: [
            /* @__PURE__ */ jsx(
              Icon,
              {
                fill: rating.backgroundColor,
                width,
                height: iconSize,
                viewBox: `0 0 ${wViewBox} ${SVG_DEFAULT_SIZE}`
              }
            ),
            /* @__PURE__ */ jsx(
              Icon,
              {
                fill: colors.elements.lineAndBorders,
                width: dWidth,
                height: iconSize,
                viewBox: `${wViewBox} 0 ${dViewBox} ${SVG_DEFAULT_SIZE}`
              }
            )
          ] }, `half-${i}`);
        }
        return /* @__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
};
var Rating_default = withTheme(Rating);
export {
  Rating_default as default
};
