function _typeof(obj) { 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); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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; }

import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';

function scrollTo(element, to, duration) {
  var requestAnimationFrame = window.requestAnimationFrame || function requestAnimationFrameTimeout() {
    return setTimeout(arguments.length <= 0 ? undefined : arguments[0], 10);
  };

  if (duration <= 0) {
    element.scrollTop = to;
    return;
  }

  var difference = to - element.scrollTop;
  var perTick = difference / duration * 10;
  requestAnimationFrame(function () {
    element.scrollTop = element.scrollTop + perTick;

    if (element.scrollTop === to) {
      return;
    }

    scrollTo(element, to, duration - 10);
  });
}

var noop = function noop() {};

var TimeMenu =
/*#__PURE__*/
function (_React$Component) {
  _inherits(TimeMenu, _React$Component);

  function TimeMenu() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, TimeMenu);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(TimeMenu)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "_menuRefHandler", function (ref) {
      _this.menu = ref;
    });

    return _this;
  }

  _createClass(TimeMenu, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.scrollToSelected(0);
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (prevProps.activeIndex !== this.props.activeIndex) {
        this.scrollToSelected(120);
      }
    }
  }, {
    key: "scrollToSelected",
    value: function scrollToSelected() {
      var duration = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
      var _this$props = this.props,
          activeIndex = _this$props.activeIndex,
          step = _this$props.step;
      var targetIndex = Math.floor((activeIndex || 0) / step);
      var firstItem = this.menu.children[targetIndex];
      var offsetTo = firstItem.offsetTop;
      scrollTo(this.menu, offsetTo, duration);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          prefix = _this$props2.prefix,
          title = _this$props2.title,
          mode = _this$props2.mode,
          activeIndex = _this$props2.activeIndex,
          step = _this$props2.step,
          disabled = _this$props2.disabled,
          disabledItems = _this$props2.disabledItems,
          onSelect = _this$props2.onSelect;
      var total = mode === 'hour' ? 24 : 60;
      var list = [];

      var _loop = function _loop(i) {
        if (i % step === 0) {
          var _classnames;

          var isDisabled = disabled || disabledItems(i);
          var itemCls = classnames((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "time-picker-menu-item"), true), _defineProperty(_classnames, "".concat(prefix, "disabled"), isDisabled), _defineProperty(_classnames, "".concat(prefix, "selected"), i === activeIndex), _classnames));
          var onClick = isDisabled ? noop : function () {
            return onSelect(i, mode);
          };
          list.push(React.createElement("li", {
            role: "option",
            "aria-selected": String(i === activeIndex),
            key: i,
            title: i,
            className: itemCls,
            onClick: onClick
          }, i));
        }
      };

      for (var i = 0; i < total; i++) {
        _loop(i);
      }

      var menuTitle = title ? React.createElement("div", {
        className: "".concat(prefix, "time-picker-menu-title")
      }, title) : null;
      return React.createElement("div", {
        className: "".concat(prefix, "time-picker-menu")
      }, menuTitle, React.createElement("ul", {
        role: "listbox",
        className: "".concat(prefix, "time-picker-menu-").concat(mode),
        ref: this._menuRefHandler
      }, list));
    }
  }]);

  return TimeMenu;
}(React.Component);

_defineProperty(TimeMenu, "propTypes", {
  prefix: PropTypes.string,
  title: PropTypes.node,
  mode: PropTypes.oneOf(['hour', 'minute', 'second']),
  step: PropTypes.number,
  activeIndex: PropTypes.number,
  disabledItems: PropTypes.func,
  onSelect: PropTypes.func,
  disabled: PropTypes.bool
});

_defineProperty(TimeMenu, "defaultProps", {
  step: 1,
  disabledItems: function disabledItems() {
    return false;
  },
  onSelect: function onSelect() {},
  disabled: false
});

export default TimeMenu;