"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactSwipeable = require("react-swipeable");
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
require("./Styles.scss");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _clsx = _interopRequireDefault(require("clsx"));
var _styles = require("@material-ui/core/styles");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var styles = function styles(theme) {
  return {
    carousel: {
      tapHighlightColor: theme.tapHighlightColor
    }
  };
};
var Carousel = /*#__PURE__*/function (_React$Component) {
  _inherits(Carousel, _React$Component);
  var _super = _createSuper(Carousel);
  function Carousel(props) {
    var _this;
    _classCallCheck(this, Carousel);
    _this = _super.call(this, props);
    _defineProperty(_assertThisInitialized(_this), "init", function () {
      if (!_this.slider || !_this.slider.children) {
        return;
      }
      _this.children = _this.slider.children;
      var vertical = _this.props.vertical;
      var container = _this.refs.container,
        carousel = _this.refs.carousel,
        cc = getComputedStyle(container),
        ph = parseInt(cc.paddingLeft) + parseInt(cc.paddingRight),
        pv = parseInt(cc.paddingTop) + parseInt(cc.paddingBottom),
        cw = (container.clientWidth > 0 ? container.clientWidth : carousel.clientWidth) - ph,
        ch = (container.clientHeight > 0 ? container.clientHeight : carousel.clientHeight) - pv;
      var width = 0,
        height = 0,
        containerWidth = 0,
        containerHeight = 0,
        slidesToShow = 0,
        idx = 0,
        offsetLeft = 0,
        offsetTop = 0;
      for (var c = 0; c < _this.children.length; c++) {
        // IE 11 needs this..
        if (!_this.children[c]) {
          continue;
        }
        var item = _this.children[c];
        if (vertical) {
          height += item.offsetHeight;
          if (ch >= height) {
            slidesToShow++;
            containerHeight = height;
          }
          containerWidth = Math.max(containerWidth, item.offsetWidth);
          if (idx < _this.state.firstIndex) {
            offsetTop = -height;
          }
        } else {
          width += item.offsetWidth;
          if (cw >= width) {
            slidesToShow++;
            containerWidth = width;
          }
          containerHeight = Math.max(containerHeight, item.offsetHeight);
          if (idx < _this.state.firstIndex) {
            offsetLeft = -width;
          }
        }
        idx++;
      }
      if (_this.state.firstIndex + slidesToShow > _this.children.length) {
        _this.setState({
          firstIndex: _this.children.length - slidesToShow
        });
        return;
      }
      var nextActive = _this.state.firstIndex + slidesToShow < _this.children.length;
      var prevActive = _this.state.firstIndex > 0;
      var showArrows = _this.children.length !== slidesToShow;
      if (!showArrows) {
        offsetTop = offsetLeft = 0;
      }
      var state = _objectSpread(_objectSpread({}, _this.state), {}, {
        slidesToShow: slidesToShow,
        containerWidth: containerWidth,
        containerHeight: containerHeight,
        offsetLeft: offsetLeft,
        offsetTop: offsetTop,
        prevActive: prevActive,
        nextActive: nextActive,
        showArrows: showArrows
      });
      if (!(0, _isEqual["default"])(state, _this.state)) {
        _this.setState(state);
      }
    });
    _defineProperty(_assertThisInitialized(_this), "next", function () {
      var _this$state = _this.state,
        firstIndex = _this$state.firstIndex,
        slidesToShow = _this$state.slidesToShow;
      if (firstIndex + slidesToShow < _this.children.length) {
        _this.setState({
          firstIndex: _this.state.firstIndex + 1
        });
      }
    });
    _defineProperty(_assertThisInitialized(_this), "prev", function () {
      var firstIndex = _this.state.firstIndex;
      if (firstIndex > 0) {
        _this.setState({
          firstIndex: firstIndex - 1
        });
      }
    });
    _defineProperty(_assertThisInitialized(_this), "swiped", function (e) {
      // if it's vertical deltaY should be the bigger
      if (_this.props.vertical && Math.abs(e.deltaY) >= Math.abs(e.deltaX)) {
        if (e.deltaY > 0) {
          _this.next();
        } else {
          _this.prev();
        }
      }
      // if it's not vertical deltaX should be the bigger
      else if (!_this.props.vertical && Math.abs(e.deltaX) >= Math.abs(e.deltaY)) {
        if (e.deltaX > 0) {
          _this.next();
        } else {
          _this.prev();
        }
      }
    });
    _defineProperty(_assertThisInitialized(_this), "scrolled", function (e) {
      e.preventDefault();
      if (e.deltaX > 0 || e.deltaY < 0 || e.wheelDelta > 0) {
        _this.prev();
      } else {
        _this.next();
      }
    });
    _this.state = {
      slidesToShow: 0,
      containerWidth: 0,
      containerHeight: 0,
      firstIndex: 0,
      offsetLeft: 0,
      offsetTop: 0,
      prevActive: false,
      nextActive: false,
      showArrows: false
    };
    _this.children = [];
    return _this;
  }
  _createClass(Carousel, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.init();
      var carousel = this.refs.carousel;
      var wheelEventName = 'onwheel' in carousel ? 'wheel' : 'mousewheel';
      carousel.addEventListener(wheelEventName, this.scrolled);
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      var _this2 = this;
      // waiting for the browser to apply css changes..
      setTimeout(function () {
        return _this2.init();
      }, 100);
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;
      var _this$state2 = this.state,
        containerWidth = _this$state2.containerWidth,
        containerHeight = _this$state2.containerHeight,
        offsetLeft = _this$state2.offsetLeft,
        offsetTop = _this$state2.offsetTop,
        prevActive = _this$state2.prevActive,
        nextActive = _this$state2.nextActive,
        showArrows = _this$state2.showArrows;
      var classes = this.props.classes;
      var className = ['carousel-wrapper', this.props.className, this.props.vertical ? 'vertical' : 'horizontal', showArrows ? 'content-overflow' : 'content-fit'].join(' ');
      var prevClassName = ['carousel-nav', 'prev', prevActive ? 'active' : ''].join(' ');
      var nextClassName = ['carousel-nav', 'next', nextActive ? 'active' : ''].join(' ');
      var style = {
        height: containerHeight,
        width: containerWidth
      };
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: className,
        ref: "container"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: 'carousel',
        ref: "carousel",
        style: style
      }, showArrows && /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _clsx["default"])(classes.carousel, prevClassName),
        ref: "prev",
        onClick: this.prev
      }), /*#__PURE__*/_react["default"].createElement(_reactSwipeable.Swipeable, {
        className: 'carousel-frame',
        trackMouse: true,
        onSwiping: this.swiping,
        onSwiped: this.swiped
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "carousel-slider",
        style: {
          left: offsetLeft,
          top: offsetTop
        },
        ref: function ref(slider) {
          return _this3.slider = slider;
        }
      }, _react["default"].Children.map(this.props.children, function (element) {
        return /*#__PURE__*/_react["default"].createElement("div", {
          style: {
            display: 'inline-block'
          }
        }, element);
      }))), showArrows && /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _clsx["default"])(classes.carousel, nextClassName),
        ref: "next",
        onClick: this.next
      })));
    }
  }]);
  return Carousel;
}(_react["default"].Component);
var _default = exports["default"] = (0, _styles.withStyles)(styles, {
  name: 'Carousel'
})(Carousel);