"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;exports.__esModule = true;exports.ScrollContainerDataTids = exports.ScrollContainer = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _react = _interopRequireDefault(require("react"));
var _globalObject = require("@skbkontur/global-object");
var _lodash = _interopRequireDefault(require("lodash.debounce"));

var _isInstanceOf = require("../../lib/isInstanceOf");
var LayoutEvents = _interopRequireWildcard(require("../../lib/LayoutEvents"));

var _CommonWrapper = require("../../internal/CommonWrapper");

var _Emotion = require("../../lib/theming/Emotion");
var _client = require("../../lib/client");

var _rootNode = require("../../lib/rootNode");
var _getDOMRect = require("../../lib/dom/getDOMRect");
var _createPropsGetter = require("../../lib/createPropsGetter");
var _currentEnvironment = require("../../lib/currentEnvironment");
var _callChildRef = require("../../lib/callChildRef/callChildRef");

var _ScrollContainer2 = require("./ScrollContainer.styles");
var _ScrollContainer3 = require("./ScrollContainer.constants");
var _ScrollContainer4 = require("./ScrollContainer.helpers");





var _ScrollBar = require("./ScrollBar");var _class, _ScrollContainer;



// deprecated


























































var ScrollContainerDataTids = exports.ScrollContainerDataTids = {
  root: 'ScrollContainer__root',
  inner: 'ScrollContainer__inner'
};














/**
 * `ScrollContainer` используется для создания контейнера с кастомными полосами прокрутки, который обеспечивает прокрутку содержимого по горизонтали или вертикали.
 */var

ScrollContainer = exports.ScrollContainer = (0, _rootNode.rootNode)(_class = (_ScrollContainer = /*#__PURE__*/function (_React$Component) {function ScrollContainer() {var _this;for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;_this.














    getProps = (0, _createPropsGetter.createPropsGetter)(ScrollContainer.defaultProps);_this.





    initialIsScrollBarVisible = _this.getProps().showScrollBar === 'always';_this.

    state = {
      isScrollBarXVisible: _this.initialIsScrollBarVisible,
      isScrollBarYVisible: _this.initialIsScrollBarVisible,
      isHovered: false
    };_this.





























    render = function () {
      var props = _this.props;

      if (_this.props.disabled) {
        return _this.props.children;
      }

      var innerStyle = {
        scrollBehavior: _this.getProps().scrollBehaviour,
        maxHeight: props.maxHeight,
        maxWidth: props.maxWidth
      };

      var scrollbarY = _this.renderScrollbar('y');
      var scrollbarX = _this.renderScrollbar('x');

      return /*#__PURE__*/(
        _react.default.createElement(_CommonWrapper.CommonWrapper, (0, _extends2.default)({ rootNodeRef: _this.setRootNode }, _this.props), /*#__PURE__*/
        _react.default.createElement("div", {
          "data-tid": ScrollContainerDataTids.root,
          className: _ScrollContainer2.styles.root(),
          onMouseMove: _this.handleMouseMove,
          onMouseLeave: _this.handleMouseLeave },

        scrollbarY,
        scrollbarX, /*#__PURE__*/
        _react.default.createElement("div", {
          style: innerStyle,
          ref: _this.refInner,
          className: (0, _Emotion.cx)(_ScrollContainer2.styles.inner(), _ScrollContainer2.globalClasses.inner, _client.isIE11 && _ScrollContainer2.styles.innerIE11()),
          "data-tid": ScrollContainerDataTids.inner,
          onScroll: _this.handleNativeScroll },

        props.children
        )
        )
        ));

    };_this.

































































    renderScrollbar = function (axis) {
      var _this$getProps = _this.getProps(),offsetY = _this$getProps.offsetY,offsetX = _this$getProps.offsetX,invert = _this$getProps.invert,disableAnimations = _this$getProps.disableAnimations;
      var isAxisX = axis === 'x';
      var refScrollBar = isAxisX ? _this.refScrollBarX : _this.refScrollBarY;
      var offset = isAxisX ? offsetX : offsetY;
      var isVisible = isAxisX ? _this.state.isScrollBarXVisible : _this.state.isScrollBarYVisible;

      return /*#__PURE__*/(
        _react.default.createElement(_ScrollBar.ScrollBar, {
          axis: axis,
          ref: refScrollBar,
          invert: invert,
          onScroll: _this.handleScroll,
          offset: offset,
          disableAnimations: disableAnimations,
          isVisible: isVisible }
        ));

    };_this.

    handleScrollStateChange = function (scrollState, axis) {
      if (!_this.scrollY || !_this.scrollX) {
        return;
      }

      if (axis === 'x') {
        var scrollXState = (0, _ScrollContainer4.convertScrollbarXScrollState)(scrollState);

        _this.props.onScrollStateChangeX == null || _this.props.onScrollStateChangeX(scrollXState);
        return;
      }

      var scrollYState = (0, _ScrollContainer4.convertScrollbarYScrollState)(scrollState);

      _this.props.onScrollStateChange == null || _this.props.onScrollStateChange(scrollYState);
      _this.props.onScrollStateChangeY == null || _this.props.onScrollStateChangeY(scrollYState);
    };_this.

    handleScroll = function (
    axis,
    scrollState,
    prevScrollState)
    {
      if (scrollState !== prevScrollState) {
        _this.handleScrollStateChange(scrollState, axis);
      }
      var _this$getProps2 = _this.getProps(),showScrollBar = _this$getProps2.showScrollBar;
      showScrollBar === 'scroll' && _this.showScrollBarOnMouseWheel(axis);
    };_this.

    refScrollBarY = function (scrollbar) {
      _this.scrollY = scrollbar;
    };_this.

    refScrollBarX = function (scrollbar) {
      _this.scrollX = scrollbar;
    };_this.

    refInner = function (element) {
      if (!_this.inner && element && _this.getProps().preventWindowScroll) {
        element.addEventListener('wheel', _this.handleInnerScrollWheel, { passive: false });
      }
      if (_this.inner && !element) {
        _this.inner.removeEventListener('wheel', _this.handleInnerScrollWheel);
      }
      _this.inner = element;

      if (_this.props.scrollRef) {
        (0, _callChildRef.callChildRef)(_this.props.scrollRef, element);
      }
    };_this.

    handleNativeScroll = function (event) {var _this$scrollY, _this$scrollX;
      (_this$scrollY = _this.scrollY) == null || _this$scrollY.reflow();
      (_this$scrollX = _this.scrollX) == null || _this$scrollX.reflow();

      _this.props.onScroll == null || _this.props.onScroll(event);
      if (_this.getProps().preventWindowScroll) {
        event.preventDefault();
        return;
      }
      LayoutEvents.emit();
    };_this.

    showScrollBarOnMouseWheel = function (axis) {
      var isScrollBarVisible = axis === 'x' ? _this.state.isScrollBarXVisible : _this.state.isScrollBarYVisible;
      if (!isScrollBarVisible) {
        axis === 'x' ? _this.setState({ isScrollBarXVisible: true }) : _this.setState({ isScrollBarYVisible: true });
      }
      _this.hideScrollBar(axis);
    };_this.

    hideScrollBar = (0, _lodash.default)(function (axis) {var _this$scrollX2, _this$scrollY2;
      if (_this.state.isHovered) {
        return;
      }
      var isScrollBarXHovered = (_this$scrollX2 = _this.scrollX) == null ? void 0 : _this$scrollX2.getHover();
      var isScrollBarYHovered = (_this$scrollY2 = _this.scrollY) == null ? void 0 : _this$scrollY2.getHover();
      if (axis === 'both') {
        !isScrollBarXHovered && !isScrollBarYHovered ?
        _this.setState({ isScrollBarXVisible: false, isScrollBarYVisible: false }) :
        _this.hideScrollBar('both');
      } else if (axis === 'x') {
        !isScrollBarXHovered ? _this.setState({ isScrollBarXVisible: false }) : _this.hideScrollBar('x');
      } else {
        !isScrollBarYHovered ? _this.setState({ isScrollBarYVisible: false }) : _this.hideScrollBar('y');
      }
    }, _this.getProps().hideScrollBarDelay);_this.

    handleInnerScrollWheel = function (event) {
      if (!_this.inner || !(0, _isInstanceOf.isInstanceOf)(event, _globalObject.globalObject.WheelEvent)) {
        return;
      }

      var axis = event.shiftKey ? 'x' : 'y';

      if (_this.hasScrollBar(axis)) {
        var _scrollSizeParameters = _ScrollContainer3.scrollSizeParametersNames[axis],pos = _scrollSizeParameters.pos,size = _scrollSizeParameters.size,offset = _scrollSizeParameters.offset;

        if (event.deltaY > 0 && _this.inner[size] <= _this.inner[pos] + _this.inner[offset]) {
          event.preventDefault();
          return false;
        }
        if (event.deltaY < 0 && _this.inner[pos] <= 0) {
          event.preventDefault();
          return false;
        }
      }
    };_this.

    handleMouseMove = function (event) {var _this$scrollY3, _this$scrollX3;
      var right = (0, _getDOMRect.getDOMRect)(event.currentTarget).right - event.pageX;
      var bottom = (0, _getDOMRect.getDOMRect)(event.currentTarget).bottom - event.pageY;

      (_this$scrollY3 = _this.scrollY) == null || _this$scrollY3.setHover(right <= 12);
      (_this$scrollX3 = _this.scrollX) == null || _this$scrollX3.setHover(right >= 12 && bottom <= 12);

      _this.getProps().showScrollBar === 'hover' &&
      !_this.state.isHovered &&
      _this.setState({ isScrollBarXVisible: true, isScrollBarYVisible: true, isHovered: true });
    };_this.

    handleMouseLeave = function () {var _this$scrollY4, _this$scrollX4;
      (_this$scrollY4 = _this.scrollY) == null || _this$scrollY4.setHover(false);
      (_this$scrollX4 = _this.scrollX) == null || _this$scrollX4.setHover(false);
      if (_this.getProps().showScrollBar === 'hover') {
        _this.setState({ isHovered: false });
        _this.hideScrollBar('both');
      }
    };_this.

    updateInnerElement = function () {var _this$scrollX5, _this$scrollY5;
      (_this$scrollX5 = _this.scrollX) == null || _this$scrollX5.setInnerElement(_this.inner);
      (_this$scrollY5 = _this.scrollY) == null || _this$scrollY5.setInnerElement(_this.inner);
    };return _this;}(0, _inheritsLoose2.default)(ScrollContainer, _React$Component);var _proto = ScrollContainer.prototype;_proto.componentDidMount = function componentDidMount() {this.updateInnerElement();};_proto.componentDidUpdate = function componentDidUpdate(prevProps) {var preventWindowScroll = this.getProps().preventWindowScroll;if (this.inner) {if (prevProps.preventWindowScroll && !preventWindowScroll) {this.inner.removeEventListener('wheel', this.handleInnerScrollWheel);}if (!prevProps.preventWindowScroll && preventWindowScroll) {this.inner.addEventListener('wheel', this.handleInnerScrollWheel, { passive: false });}}if (prevProps.disabled !== this.props.disabled && !this.props.disabled) {this.updateInnerElement();}if (prevProps.showScrollBar !== this.props.showScrollBar) {if (this.props.showScrollBar === 'always') {this.setState({ isScrollBarXVisible: true, isScrollBarYVisible: true });} else if (this.props.showScrollBar === 'never') {this.setState({ isScrollBarXVisible: false, isScrollBarYVisible: false });}}}; /**
   * @public
   * @param {Element} element
   */_proto.scrollTo = function scrollTo(element) {if (!element || !this.inner) {return;}this.inner.scrollLeft = element.offsetLeft;this.inner.scrollTop = (0, _ScrollContainer4.getScrollYOffset)(element, this.inner);} /**
   * @public
   */;_proto.scrollToTop = function scrollToTop() {if (!this.inner) {return;}this.inner.scrollTop = 0;} /**
   * @public
   */;_proto.scrollToBottom = function scrollToBottom() {if (!this.inner) {return;}this.inner.scrollTop = this.inner.scrollHeight - this.inner.offsetHeight;} /**
   * @public
   */;_proto.scrollToLeft = function scrollToLeft() {if (!this.inner) {return;}this.inner.scrollLeft = 0;} /**
   * @public
   */;_proto.scrollToRight = function scrollToRight() {if (!this.inner) {return;}this.inner.scrollLeft = this.inner.scrollWidth - this.inner.offsetWidth;};_proto.hasScrollBar = function hasScrollBar(axis) {if (!this.inner) {return false;}return axis === 'x' ? this.inner.offsetWidth < this.inner.scrollWidth : this.inner.offsetHeight < this.inner.scrollHeight;};return ScrollContainer;}(_react.default.Component), _ScrollContainer.__KONTUR_REACT_UI__ = 'ScrollContainer', _ScrollContainer.displayName = 'ScrollContainer', _ScrollContainer.defaultProps = { invert: false, scrollBehaviour: 'auto', preventWindowScroll: false, disableAnimations: _currentEnvironment.isTestEnv, hideScrollBarDelay: 500, showScrollBar: 'always' }, _ScrollContainer)) || _class;