"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _uiIcons = require("@teamleader/ui-icons");

var _classnames = _interopRequireDefault(require("classnames"));

var _react = _interopRequireWildcard(require("react"));

var _reactResizeDetector = _interopRequireDefault(require("react-resize-detector"));

var _smoothscrollPolyfill = _interopRequireDefault(require("smoothscroll-polyfill"));

var _box = _interopRequireDefault(require("../box"));

var _iconButton = _interopRequireDefault(require("../iconButton"));

var _theme = _interopRequireDefault(require("./theme.css"));

var _excluded = ["children", "className", "size"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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) { (0, _defineProperty2.default)(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; }

var SCROLL_BUTTON_WIDTH = 48;
var SCROLL_DISTANCE = 200;

var TabGroup = function TabGroup(_ref) {
  var children = _ref.children,
      className = _ref.className,
      size = _ref.size,
      others = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var scrollContainerRef = (0, _react.useRef)(null);
  var activeTabRef = (0, _react.useRef)(null);

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      canScrollLeft = _useState2[0],
      setCanScrollLeft = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
      canScrollRight = _useState4[0],
      setCanScrollRight = _useState4[1];

  var checkForScrollPosition = function checkForScrollPosition() {
    if (scrollContainerRef.current) {
      var _scrollContainerRef$c = scrollContainerRef.current,
          scrollLeft = _scrollContainerRef$c.scrollLeft,
          scrollWidth = _scrollContainerRef$c.scrollWidth,
          clientWidth = _scrollContainerRef$c.clientWidth;
      setCanScrollLeft(scrollLeft > 0);
      setCanScrollRight(scrollLeft < scrollWidth - clientWidth);
    }
  };

  var scrollToActiveTab = function scrollToActiveTab() {
    if (activeTabRef.current) {
      var _scrollContainerRef$c2;

      var offsetLeft = activeTabRef.current.offsetLeft;
      (_scrollContainerRef$c2 = scrollContainerRef.current) === null || _scrollContainerRef$c2 === void 0 ? void 0 : _scrollContainerRef$c2.scrollTo({
        left: offsetLeft - SCROLL_BUTTON_WIDTH
      });
    }
  };

  var handleResize = function handleResize() {
    scrollToActiveTab();
    checkForScrollPosition();
  };

  var handleScroll = function handleScroll() {
    checkForScrollPosition();
  };

  var scrollContainerBy = function scrollContainerBy(distance) {
    var _scrollContainerRef$c3;

    (_scrollContainerRef$c3 = scrollContainerRef.current) === null || _scrollContainerRef$c3 === void 0 ? void 0 : _scrollContainerRef$c3.scrollBy({
      left: distance,
      behavior: 'smooth'
    });
  };

  (0, _react.useEffect)(function () {
    var _scrollContainerRef$c4;

    _smoothscrollPolyfill.default.polyfill();

    (_scrollContainerRef$c4 = scrollContainerRef.current) === null || _scrollContainerRef$c4 === void 0 ? void 0 : _scrollContainerRef$c4.addEventListener('scroll', handleScroll);
    return function () {
      var _scrollContainerRef$c5;

      return (_scrollContainerRef$c5 = scrollContainerRef.current) === null || _scrollContainerRef$c5 === void 0 ? void 0 : _scrollContainerRef$c5.removeEventListener('scroll', handleScroll);
    };
  }, []);
  var classNames = (0, _classnames.default)(_theme.default['tab-group'], className);
  return /*#__PURE__*/_react.default.createElement(_box.default, (0, _extends2.default)({
    backgroundColor: "neutral",
    backgroundTint: "lightest",
    "data-teamleader-ui": "tab-group",
    className: classNames
  }, others, {
    display: "flex",
    paddingHorizontal: size === 'small' ? 2 : 0
  }), /*#__PURE__*/_react.default.createElement(_box.default, {
    className: _theme.default['scroll-container'],
    display: "flex",
    overflowX: "scroll",
    ref: scrollContainerRef
  }, _react.default.Children.map(children, function (child) {
    return /*#__PURE__*/_react.default.isValidElement(child) ? /*#__PURE__*/_react.default.cloneElement(child, _objectSpread({
      size: size
    }, child.props.active && {
      ref: activeTabRef
    })) : child;
  })), canScrollLeft && /*#__PURE__*/_react.default.createElement(_box.default, {
    className: _theme.default['scroll-left-button-wrapper']
  }, /*#__PURE__*/_react.default.createElement(_iconButton.default, {
    className: _theme.default['scroll-button'],
    icon: /*#__PURE__*/_react.default.createElement(_uiIcons.IconChevronLeftSmallOutline, null),
    onClick: function onClick() {
      scrollContainerBy(-1 * SCROLL_DISTANCE);
    }
  })), canScrollRight && /*#__PURE__*/_react.default.createElement(_box.default, {
    className: _theme.default['scroll-right-button-wrapper']
  }, /*#__PURE__*/_react.default.createElement(_iconButton.default, {
    className: _theme.default['scroll-button'],
    icon: /*#__PURE__*/_react.default.createElement(_uiIcons.IconChevronRightSmallOutline, null),
    onClick: function onClick() {
      scrollContainerBy(SCROLL_DISTANCE);
    }
  })), /*#__PURE__*/_react.default.createElement(_reactResizeDetector.default, {
    handleWidth: true,
    onResize: handleResize
  }));
};

var _default = TabGroup;
exports.default = _default;