"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 _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _shadowObserver = require("./shadowObserver");
var _shadowObserverClassNames = require("./shadowObserverClassNames");
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
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) {
    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 = _callSuper(this, OverflowShadow, [].concat(args));
      (0, _defineProperty2.default)(_this, "overflowContainerWidth", 0);
      (0, _defineProperty2.default)(_this, "state", {
        showLeftShadow: false,
        showRightShadow: false
      });
      (0, _defineProperty2.default)(_this, "handleScroll", function (event) {
        if (!_this.overflowContainer || event.target !== _this.overflowContainer || options.useShadowObserver) {
          return;
        }
        _this.updateShadows();
      });
      (0, _defineProperty2.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)(_this, "showLeftShadow", function (overflowContainer) {
        return !!overflowContainer && overflowContainer.scrollLeft > 0;
      });
      (0, _defineProperty2.default)(_this, "calcOverflowDiff", function () {
        if (!_this.overflowContainer) {
          return 0;
        }
        _this.diff = _this.calcScrollableWidth();
        return _this.diff - _this.overflowContainer.offsetWidth;
      });
      (0, _defineProperty2.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];
          if (isElementNode(scrollableElement)) {
            width += scrollableElement.scrollWidth;
          }
        }
        return width;
      });
      (0, _defineProperty2.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();
        // Ignored via go/ees005
        // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
        _this.overflowContainer.addEventListener('scroll', _this.handleScroll);
      });
      return _this;
    }
    (0, _inherits2.default)(OverflowShadow, _React$Component);
    return (0, _createClass2.default)(OverflowShadow, [{
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        if (options.useShadowObserver) {
          return this.shadowObserver && this.shadowObserver.dispose();
        }
        if (this.overflowContainer) {
          // Ignored via go/ees005
          // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
          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$props, _this$props2;
        var _this$state = this.state,
          showLeftShadow = _this$state.showLeftShadow,
          showRightShadow = _this$state.showRightShadow;
        var classNames = [!((_this$props = this.props) !== null && _this$props !== void 0 && _this$props.disableTableOverflowShadow) && showRightShadow && shadowClassNames.RIGHT_SHADOW, !((_this$props2 = this.props) !== null && _this$props2 !== void 0 && _this$props2.disableTableOverflowShadow) && showLeftShadow && shadowClassNames.LEFT_SHADOW, options.useShadowObserver && _shadowObserverClassNames.shadowObserverClassNames.SHADOW_CONTAINER].filter(Boolean).join(' ');

        /**
         * The widths have already been calculated to determine
         * showRightShadow and showLeftShadow. If either is true,
         * then the content is scrollable and we need to set the
         * tabIndex to 0 to allow the user to scroll the content
         * for a11y purposes.
         */
        var hasOverflowScroll = showRightShadow || showLeftShadow;
        return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
          handleRef: this.handleContainer,
          tabIndex: hasOverflowScroll ? 0 : undefined,
          shadowClassNames: classNames
          // eslint-disable-next-line react/jsx-props-no-spreading -- Spreading props to pass all component props through to wrapped generic component
        }, this.props));
      }
    }]);
  }(_react.default.Component);
}

// Helper function to check if the passed node is of Element class
function isElementNode(node) {
  return node.nodeType === 1;
}