import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
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";
var _excluded = ["children", "className", "inverted", "size"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { createRef, PureComponent } from 'react';
import ReactResizeDetector from 'react-resize-detector';
import smoothScroll from 'smoothscroll-polyfill';
import PropTypes from 'prop-types';
import Box from '../box';
import IconButton from '../iconButton';
import { IconChevronLeftSmallOutline, IconChevronRightSmallOutline } from '@teamleader/ui-icons';
import cx from 'classnames';
import theme from './theme.css';
var SCROLL_BUTTON_WIDTH = 48;
var SCROLL_DISTANCE = 200;
/** @type {React.ComponentType<any>} */

var TabGroup = /*#__PURE__*/function (_PureComponent) {
  _inherits(TabGroup, _PureComponent);

  var _super = _createSuper(TabGroup);

  function TabGroup() {
    var _this;

    _classCallCheck(this, TabGroup);

    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), "scrollContainerRef", /*#__PURE__*/createRef());

    _defineProperty(_assertThisInitialized(_this), "activeTabRef", /*#__PURE__*/createRef());

    _defineProperty(_assertThisInitialized(_this), "state", {
      canScrollLeft: false,
      canScrollRight: false
    });

    _defineProperty(_assertThisInitialized(_this), "scrollToActiveTab", function () {
      var offsetLeft = _this.activeTabRef.current.offsetLeft;

      _this.scrollContainerRef.current.scrollTo({
        left: offsetLeft - SCROLL_BUTTON_WIDTH
      });
    });

    _defineProperty(_assertThisInitialized(_this), "checkForScrollPosition", function () {
      var _this$scrollContainer = _this.scrollContainerRef.current,
          scrollLeft = _this$scrollContainer.scrollLeft,
          scrollWidth = _this$scrollContainer.scrollWidth,
          clientWidth = _this$scrollContainer.clientWidth;

      _this.setState({
        canScrollLeft: scrollLeft > 0,
        canScrollRight: scrollLeft < scrollWidth - clientWidth
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleResize", function () {
      _this.scrollToActiveTab();

      _this.checkForScrollPosition();
    });

    _defineProperty(_assertThisInitialized(_this), "handleScroll", function () {
      _this.checkForScrollPosition();
    });

    _defineProperty(_assertThisInitialized(_this), "scrollContainerBy", function (distance) {
      _this.scrollContainerRef.current.scrollBy({
        left: distance,
        behavior: 'smooth'
      });
    });

    return _this;
  }

  _createClass(TabGroup, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      smoothScroll.polyfill();
      this.scrollContainerRef.current.addEventListener('scroll', this.handleScroll);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.scrollContainerRef.current.removeEventListener('scroll', this.handleScroll);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          children = _this$props.children,
          className = _this$props.className,
          inverted = _this$props.inverted,
          size = _this$props.size,
          others = _objectWithoutProperties(_this$props, _excluded);

      var _this$state = this.state,
          canScrollLeft = _this$state.canScrollLeft,
          canScrollRight = _this$state.canScrollRight;
      var classNames = cx(theme['tab-group'], className);
      return /*#__PURE__*/React.createElement(Box, _extends({
        backgroundColor: "neutral",
        backgroundTint: "lightest",
        "data-teamleader-ui": "tab-group",
        className: classNames
      }, others, {
        display: "flex",
        paddingHorizontal: size === 'small' ? 2 : 0
      }), /*#__PURE__*/React.createElement(Box, {
        className: theme['scroll-container'],
        display: "flex",
        overflowX: "scroll",
        ref: this.scrollContainerRef
      }, React.Children.map(children, function (child) {
        return /*#__PURE__*/React.cloneElement(child, _objectSpread({
          size: size
        }, child.props.active && {
          ref: _this2.activeTabRef
        }));
      })), canScrollLeft && /*#__PURE__*/React.createElement(Box, {
        className: theme['scroll-left-button-wrapper']
      }, /*#__PURE__*/React.createElement(IconButton, {
        className: theme['scroll-button'],
        icon: /*#__PURE__*/React.createElement(IconChevronLeftSmallOutline, null),
        onClick: function onClick() {
          _this2.scrollContainerBy(-1 * SCROLL_DISTANCE);
        }
      })), canScrollRight && /*#__PURE__*/React.createElement(Box, {
        className: theme['scroll-right-button-wrapper']
      }, /*#__PURE__*/React.createElement(IconButton, {
        className: theme['scroll-button'],
        icon: /*#__PURE__*/React.createElement(IconChevronRightSmallOutline, null),
        onClick: function onClick() {
          _this2.scrollContainerBy(SCROLL_DISTANCE);
        }
      })), /*#__PURE__*/React.createElement(ReactResizeDetector, {
        handleWidth: true,
        onResize: this.handleResize
      }));
    }
  }]);

  return TabGroup;
}(PureComponent);

export default TabGroup;