function _extends() { _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; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/*
Copyright (c) 2018-2019 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { StyledRoot, StyledEmoticon } from './styled-components.js';
import { getOverrides } from '../helpers/overrides.js';
import { ENTER_KEY_CODE, SPACE_KEY_CODE } from './utils.js';

class EmoticonRating extends React.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", {});

    _defineProperty(this, "selectItem", value => {
      const {
        onChange
      } = this.props;
      onChange && onChange({
        value
      });
      this.setState({
        previewIndex: undefined
      });
    });

    _defineProperty(this, "updatePreview", previewIndex => {
      this.setState({
        previewIndex
      });
    });

    _defineProperty(this, "renderRatingContents", () => {
      const {
        overrides = {},
        value = -1
      } = this.props;
      const {
        previewIndex
      } = this.state;
      const [Emoticon, emoticonProps] = getOverrides(overrides.Item, StyledEmoticon);
      const ratings = [];

      for (let x = 1; x <= 5; x++) {
        ratings.push(React.createElement(Emoticon, _extends({
          key: x,
          role: "radio",
          tabIndex: 0,
          "aria-setsize": 5,
          "aria-checked": x === value,
          "aria-posinset": x,
          $index: x,
          $isActive: previewIndex !== undefined ? x === previewIndex : x === value,
          $isSelected: x === previewIndex,
          onClick: () => this.selectItem(x),
          onKeyDown: e => {
            if (e.keyCode === SPACE_KEY_CODE || e.keyCode === ENTER_KEY_CODE) {
              this.selectItem(x);
            }
          },
          onFocus: () => this.updatePreview(x),
          onMouseOver: () => this.updatePreview(x),
          onBlur: () => this.updatePreview(undefined)
        }, emoticonProps)));
      }

      return ratings;
    });
  }

  render() {
    const {
      overrides = {}
    } = this.props;
    const [Root, rootProps] = getOverrides(overrides.Root, StyledRoot);
    return React.createElement(Root, _extends({
      "data-baseweb": "emoticon-rating",
      tabIndex: 0,
      role: "radiogroup",
      onBlur: () => this.updatePreview(undefined),
      onMouseLeave: () => this.updatePreview(undefined)
    }, rootProps), this.renderRatingContents());
  }

}

_defineProperty(EmoticonRating, "defaultProps", {
  overrides: {}
});

export default EmoticonRating;