"use strict";

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

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

var _util = require("../../util");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _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 _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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

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

/**
 * slider arrow
 * 左右控制箭头
 */
var noop = _util.func.noop;

var Arrow =
/*#__PURE__*/
function (_Component) {
  _inherits(Arrow, _Component);

  function Arrow() {
    _classCallCheck(this, Arrow);

    return _possibleConstructorReturn(this, _getPrototypeOf(Arrow).apply(this, arguments));
  }

  _createClass(Arrow, [{
    key: "handleClick",
    value: function handleClick(options, e) {
      e && e.preventDefault(); // TODO hack

      if (options.message === 'prev') {
        options.message = 'previous';
      }

      this.props.clickHandler(options, e);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          prefix = _this$props.prefix,
          type = _this$props.type,
          arrowSize = _this$props.arrowSize,
          arrowPosition = _this$props.arrowPosition,
          arrowDirection = _this$props.arrowDirection,
          onMouseEnter = _this$props.onMouseEnter,
          onMouseLeave = _this$props.onMouseLeave,
          children = _this$props.children;

      var others = _util.obj.pickOthers(Arrow.propTypes, this.props);

      var iconType = Arrow.ARROW_ICON_TYPES[arrowDirection][type];
      var disabled = Arrow.isDisabled(this.props);
      var arrowClazz = (0, _classnames["default"])(["".concat(prefix, "slick-arrow"), "".concat(prefix, "slick-").concat(type), arrowPosition, arrowSize, arrowDirection], {
        disabled: disabled
      });

      var arrowProps = _objectSpread({}, others, {
        key: type,
        'data-role': 'none',
        className: arrowClazz,
        style: {
          display: 'block'
        },
        onClick: disabled ? null : this.handleClick.bind(this, {
          message: type
        }),
        onMouseEnter: disabled ? null : onMouseEnter,
        onMouseLeave: disabled ? null : onMouseLeave
      });

      if (children) {
        return _react["default"].cloneElement(_react["default"].Children.only(children), arrowProps);
      } else {
        return _react["default"].createElement("button", _extends({
          type: "button",
          role: "button"
        }, arrowProps), _react["default"].createElement(_icon["default"], {
          type: iconType
        }));
      }
    }
  }], [{
    key: "isDisabled",
    value: function isDisabled(props) {
      var infinite = props.infinite,
          type = props.type,
          centerMode = props.centerMode,
          currentSlide = props.currentSlide,
          slideCount = props.slideCount,
          slidesToShow = props.slidesToShow;

      if (infinite) {
        return false;
      } // 下一个 index 大于总数？？


      if (slideCount <= slidesToShow) {
        return true;
      } // 向前箭头：当前是第 0 个


      if (type === 'prev') {
        return currentSlide <= 0;
      }

      if (centerMode && currentSlide >= slideCount - 1) {
        // 向后箭头：居中模式，当前 index 大于最大 index
        return true;
      } else if (currentSlide >= slideCount - slidesToShow) {
        // 向后箭头：普通模式，当前 index 大于 总数 - 下一个 index ？？？
        return true;
      }

      return false;
    }
  }]);

  return Arrow;
}(_react.Component);

exports["default"] = Arrow;

_defineProperty(Arrow, "propTypes", {
  prefix: _propTypes["default"].string,
  rtl: _propTypes["default"].bool,
  type: _propTypes["default"].oneOf(['prev', 'next']).isRequired,
  centerMode: _propTypes["default"].bool,
  currentSlide: _propTypes["default"].number,
  infinite: _propTypes["default"].bool,
  clickHandler: _propTypes["default"].func,
  slideCount: _propTypes["default"].number,
  slidesToShow: _propTypes["default"].number,
  arrow: _propTypes["default"].element,
  arrowSize: _propTypes["default"].string,
  arrowPosition: _propTypes["default"].string,
  arrowDirection: _propTypes["default"].oneOf(['hoz', 'ver']),
  onMouseEnter: _propTypes["default"].func,
  onMouseLeave: _propTypes["default"].func,
  children: _propTypes["default"].node
});

_defineProperty(Arrow, "defaultProps", {
  onMouseEnter: noop,
  onMouseLeave: noop
});

_defineProperty(Arrow, "ARROW_ICON_TYPES", {
  hoz: {
    prev: 'chevron-left',
    next: 'chevron-right'
  },
  ver: {
    prev: 'chevron-up',
    next: 'chevron-down'
  }
});