import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "className", "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; }

import { IconChevronLeftSmallOutline, IconChevronRightSmallOutline } from '@teamleader/ui-icons';
import cx from 'classnames';
import React, { useEffect, useRef, useState } from 'react';
import ReactResizeDetector from 'react-resize-detector';
import smoothScroll from 'smoothscroll-polyfill';
import Box from '../box';
import IconButton from '../iconButton';
import theme from './theme.css';
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 = _objectWithoutProperties(_ref, _excluded);

  var scrollContainerRef = useRef(null);
  var activeTabRef = useRef(null);

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      canScrollLeft = _useState2[0],
      setCanScrollLeft = _useState2[1];

  var _useState3 = useState(false),
      _useState4 = _slicedToArray(_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'
    });
  };

  useEffect(function () {
    var _scrollContainerRef$c4;

    smoothScroll.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 = 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: scrollContainerRef
  }, React.Children.map(children, function (child) {
    return /*#__PURE__*/React.isValidElement(child) ? /*#__PURE__*/React.cloneElement(child, _objectSpread({
      size: size
    }, child.props.active && {
      ref: activeTabRef
    })) : child;
  })), 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() {
      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() {
      scrollContainerBy(SCROLL_DISTANCE);
    }
  })), /*#__PURE__*/React.createElement(ReactResizeDetector, {
    handleWidth: true,
    onResize: handleResize
  }));
};

export default TabGroup;