import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import React from 'react';
import { ShadowObserver, shadowObserverClassNames } from './shadowObserver';
export var shadowClassNames = {
  RIGHT_SHADOW: 'right-shadow',
  LEFT_SHADOW: 'left-shadow'
};
export default function overflowShadow(Component, options) {
  return /*#__PURE__*/function (_React$Component) {
    _inherits(OverflowShadow, _React$Component);
    var _super = _createSuper(OverflowShadow);
    function OverflowShadow() {
      var _this;
      _classCallCheck(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));
      _defineProperty(_assertThisInitialized(_this), "overflowContainerWidth", 0);
      _defineProperty(_assertThisInitialized(_this), "state", {
        showLeftShadow: false,
        showRightShadow: false
      });
      _defineProperty(_assertThisInitialized(_this), "handleScroll", function (event) {
        if (!_this.overflowContainer || event.target !== _this.overflowContainer || options.useShadowObserver) {
          return;
        }
        _this.updateShadows();
      });
      _defineProperty(_assertThisInitialized(_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;
        });
      });
      _defineProperty(_assertThisInitialized(_this), "showLeftShadow", function (overflowContainer) {
        return !!overflowContainer && overflowContainer.scrollLeft > 0;
      });
      _defineProperty(_assertThisInitialized(_this), "calcOverflowDiff", function () {
        if (!_this.overflowContainer) {
          return 0;
        }
        _this.diff = _this.calcScrollableWidth();
        return _this.diff - _this.overflowContainer.offsetWidth;
      });
      _defineProperty(_assertThisInitialized(_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;
      });
      _defineProperty(_assertThisInitialized(_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;
    }
    _createClass(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({
          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 && shadowObserverClassNames.SHADOW_CONTAINER].filter(Boolean).join(' ');
        return /*#__PURE__*/React.createElement(Component, _extends({
          handleRef: this.handleContainer,
          shadowClassNames: classNames
        }, this.props));
      }
    }]);
    return OverflowShadow;
  }(React.Component);
}