"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var React = _interopRequireWildcard(require("react"));

var _one = require("remax/one");

var _icon = _interopRequireDefault(require("../icon"));

var _common = require("./../common");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

var prefixCls = (0, _common.getPrefixCls)('rate');

var Rate = function Rate(props) {
  var _props$count = props.count,
      count = _props$count === void 0 ? 5 : _props$count,
      label = props.label,
      _props$value = props.value,
      value = _props$value === void 0 ? 0 : _props$value,
      onChange = props.onChange,
      readOnly = props.readOnly,
      _props$icon = props.icon,
      icon = _props$icon === void 0 ? 'favor' : _props$icon,
      _props$iconFill = props.iconFill,
      iconFill = _props$iconFill === void 0 ? 'favorfill' : _props$iconFill,
      _props$size = props.size,
      size = _props$size === void 0 ? 28 : _props$size,
      _props$color = props.color,
      color = _props$color === void 0 ? '#FFD700' : _props$color;

  var handleClick = function handleClick(index) {
    if (readOnly) {
      return;
    }

    onChange === null || onChange === void 0 ? void 0 : onChange(index + 1);
  };

  var stars = (0, React.useMemo)(function () {
    if (!count) {
      return [];
    }

    return _toConsumableArray(new Array(count).keys());
  }, [count]);
  return /*#__PURE__*/React.createElement(_one.View, {
    className: prefixCls
  }, label ? /*#__PURE__*/React.createElement(_one.View, {
    className: "".concat(prefixCls, "-label")
  }, label) : null, /*#__PURE__*/React.createElement(_one.View, {
    className: "".concat(prefixCls, "-value")
  }, stars.map(function (item, index) {
    return /*#__PURE__*/React.createElement(_one.View, {
      key: index,
      className: "".concat(prefixCls, "-value-star"),
      onTap: function onTap() {
        handleClick(index);
      }
    }, value >= index + 1 ? /*#__PURE__*/React.createElement(_icon.default, {
      type: iconFill,
      size: size,
      color: color
    }) : /*#__PURE__*/React.createElement(_icon.default, {
      type: icon,
      size: size,
      color: "#999"
    }));
  })));
};

var _default = Rate;
exports.default = _default;