"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = overflowShadow;
exports.shadowClassNames = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _shadowObserver = require("./shadowObserver");
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var shadowClassNames = exports.shadowClassNames = {
  RIGHT_SHADOW: 'right-shadow',
  LEFT_SHADOW: 'left-shadow'
};
function overflowShadow(Component, options) {
  return /*#__PURE__*/function (_React$Component) {
    (0, _inherits2.default)(OverflowShadow, _React$Component);
    var _super = _createSuper(OverflowShadow);
    function OverflowShadow() {
      var _this;
      (0, _classCallCheck2.default)(this, OverflowShadow);
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }
      _this = _super.call.apply(_super, [this].concat(args));
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "overflowContainerWidth", 0);
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
        showLeftShadow: false,
        showRightShadow: false
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleScroll", function (event) {
        if (!_this.overflowContainer || event.target !== _this.overflowContainer || options.useShadowObserver) {
          return;
        }
        _this.updateShadows();
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "updateShadows", function () {
        if (options.useShadowObserver) {
          return;
        }
        _this.setState(function (prevState) {
          if (!_this.overflowContainer) {
            return;
          }
          var diff = _this.calcOverflowDiff();
          var showRightShadow = diff > 0 && diff > _this.overflowContainer.scrollLeft;
          var showLeftShadow = _this.showLeftShadow(_this.overflowContainer);
          if (showLeftShadow !== prevState.showLeftShadow || showRightShadow !== _this.state.showRightShadow) {
            return {
              showLeftShadow: showLeftShadow,
              showRightShadow: showRightShadow
            };
          }
          return null;
        });
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "showLeftShadow", function (overflowContainer) {
        return !!overflowContainer && overflowContainer.scrollLeft > 0;
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "calcOverflowDiff", function () {
        if (!_this.overflowContainer) {
          return 0;
        }
        _this.diff = _this.calcScrollableWidth();
        return _this.diff - _this.overflowContainer.offsetWidth;
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "calcScrollableWidth", function () {
        if (!_this.scrollable && _this.overflowContainer) {
          return _this.overflowContainer.scrollWidth;
        }
        if (!_this.scrollable) {
          return 0;
        }
        var width = 0;
        for (var i = 0; i < _this.scrollable.length; i++) {
          var scrollableElement = _this.scrollable[i];
          width += scrollableElement.scrollWidth;
        }
        return width;
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleContainer", function (container) {
        if (!container || _this.container) {
          return;
        }
        _this.container = container;
        _this.overflowContainer = container.querySelector(options.overflowSelector);
        if (!_this.overflowContainer) {
          _this.overflowContainer = container;
        }
        if (options.useShadowObserver) {
          _this.initShadowObserver();
          return;
        }
        _this.updateShadows();
        _this.overflowContainer.addEventListener('scroll', _this.handleScroll);
      });
      return _this;
    }
    (0, _createClass2.default)(OverflowShadow, [{
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        if (options.useShadowObserver) {
          return this.shadowObserver && this.shadowObserver.dispose();
        }
        if (this.overflowContainer) {
          this.overflowContainer.removeEventListener('scroll', this.handleScroll);
        }
        this.updateShadows();
      }
    }, {
      key: "componentDidUpdate",
      value: function componentDidUpdate() {
        if (!options.useShadowObserver) {
          this.updateShadows();
        }
      }
    }, {
      key: "initShadowObserver",
      value: function initShadowObserver() {
        var _this2 = this;
        if (this.shadowObserver || !this.overflowContainer) {
          return;
        }
        this.shadowObserver = new _shadowObserver.ShadowObserver({
          scrollContainer: this.overflowContainer,
          onUpdateShadows: function onUpdateShadows(shadowStates) {
            _this2.setState(function (prevState) {
              var showLeftShadow = shadowStates.showLeftShadow,
                showRightShadow = shadowStates.showRightShadow;
              if (showLeftShadow !== prevState.showLeftShadow || showRightShadow !== prevState.showRightShadow) {
                return {
                  showLeftShadow: showLeftShadow,
                  showRightShadow: showRightShadow
                };
              }
              return null;
            });
          }
        });
      }
    }, {
      key: "render",
      value: function render() {
        var _this$state = this.state,
          showLeftShadow = _this$state.showLeftShadow,
          showRightShadow = _this$state.showRightShadow;
        var classNames = [showRightShadow && shadowClassNames.RIGHT_SHADOW, showLeftShadow && shadowClassNames.LEFT_SHADOW, options.useShadowObserver && _shadowObserver.shadowObserverClassNames.SHADOW_CONTAINER].filter(Boolean).join(' ');
        return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
          handleRef: this.handleContainer,
          shadowClassNames: classNames
        }, this.props));
      }
    }]);
    return OverflowShadow;
  }(_react.default.Component);
}