"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

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

var _debounce = _interopRequireDefault(require("lodash/debounce"));

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

var _mediaQueryListPolyfill = require("../_util/mediaQueryListPolyfill");

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
if (typeof window !== 'undefined') {
  // const matchMediaPolyfill = (mediaQuery: string): MediaQueryList => {
  //   return {
  //     media: mediaQuery,
  //     matches: false,
  //     addListener() {
  //     },
  //     removeListener() {
  //     },
  //   };
  // };
  window.matchMedia = window.matchMedia || _mediaQueryListPolyfill.matchMediaPolifill;
} // Use require over import (will be lifted up)
// make sure matchMedia polyfill run before require('react-slick')
// eslint-disable-next-line @typescript-eslint/no-var-requires


var SlickCarousel = require('react-slick')["default"];

var Carousel = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(Carousel, _Component);

  var _super = (0, _createSuper2["default"])(Carousel);

  function Carousel() {
    var _this;

    (0, _classCallCheck2["default"])(this, Carousel);
    _this = _super.apply(this, arguments);
    _this.onWindowResized = (0, _debounce["default"])(function () {
      var autoplay = _this.props.autoplay;

      if (autoplay && _this.slick && _this.slick.innerSlider && _this.slick.innerSlider.autoPlay) {
        _this.slick.innerSlider.autoPlay();
      }
    }, 500, {
      leading: false
    });

    _this.saveSlick = function (node) {
      _this.slick = node;
    };

    return _this;
  }

  (0, _createClass2["default"])(Carousel, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var autoplay = this.props.autoplay;

      if (autoplay) {
        window.addEventListener('resize', this.onWindowResized);
      }

      this.innerSlider = this.slick && this.slick.innerSlider;
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var autoplay = this.props.autoplay;

      if (autoplay) {
        window.removeEventListener('resize', this.onWindowResized);
        this.onWindowResized.cancel();
      }
    }
  }, {
    key: "next",
    value: function next() {
      this.slick.slickNext();
    }
  }, {
    key: "prev",
    value: function prev() {
      this.slick.slickPrev();
    }
  }, {
    key: "goTo",
    value: function goTo(slide) {
      this.slick.slickGoTo(slide);
    }
  }, {
    key: "render",
    value: function render() {
      var getPrefixCls = this.context.getPrefixCls;
      var props = (0, _objectSpread2["default"])({}, this.props);

      if (props.effect === 'fade') {
        props.fade = true;
      }

      var prefixCls = getPrefixCls('carousel', props.prefixCls);
      var className = (0, _classnames["default"])(prefixCls, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-vertical"), props.vertical));
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: className
      }, /*#__PURE__*/_react["default"].createElement(SlickCarousel, (0, _extends2["default"])({
        ref: this.saveSlick
      }, props)));
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }]);
  return Carousel;
}(_react.Component);

exports["default"] = Carousel;
Carousel.displayName = 'Carousel';
Carousel.defaultProps = {
  dots: true,
  arrows: false,
  draggable: false
};
//# sourceMappingURL=index.js.map
