'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _Star = require('./../Star');

var _Star2 = _interopRequireDefault(_Star);

var _instance = require('../Context/instance.js');

var _instance2 = _interopRequireDefault(_instance);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var InputStar = (0, _react.forwardRef)(function (_ref, ref) {
  var _ref$min = _ref.min,
      min = _ref$min === undefined ? 0 : _ref$min,
      _ref$max = _ref.max,
      max = _ref$max === undefined ? 5 : _ref$max,
      _ref$value = _ref.value,
      value = _ref$value === undefined ? 0 : _ref$value,
      onChange = _ref.onChange,
      onError = _ref.onError,
      others = _objectWithoutProperties(_ref, ['min', 'max', 'value', 'onChange', 'onError']);

  // context
  var context = useContext(_instance2.default) || {};
  var locale = context.locale || function (key) {
    return key || '';
  };

  function changeHandler(e, argNum) {
    var num = argNum;
    if (num < min) {
      if (onError) {
        onError(e, {
          errMsg: '' + locale('不能小于', 'hint_cannot_be_less_than') + min + locale('颗星', 'star'),
          select: num,
          min: min,
          value: min,
          event: e
        });
      }
      num = min;
    }
    if (onChange) onChange(e, num);
  }
  var stars = [];
  for (var i = 1; i <= max; i++) {
    stars.push(i);
  }
  var current = value;
  if (current < min) current = min;
  return _react2.default.createElement(
    'div',
    _extends({ ref: ref }, others, { className: 'input-star' + (others.className ? ' ' + others.className : '') }),
    stars.map(function (index) {
      return _react2.default.createElement(_Star2.default, { onClick: function onClick(e) {
          changeHandler(e, index);
        }, key: index, className: index <= current ? 'active' : '' });
    })
  );
});

exports.default = InputStar;
module.exports = exports['default'];