var _excluded = ["className", "children", "onChange", "isFullWidth", "isScrollable"],
  _excluded2 = ["children", "title", "isActive"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { Children, useState } from 'react';
import { IconChevronLeft, IconChevronRight } from '@defencedigital/icon-library';
import { v4 as uuidv4 } from 'uuid';
import { SCROLL_DIRECTION } from './constants';
import { StyledBody } from './partials/StyledBody';
import { StyledHeader } from './partials/StyledHeader';
import { StyledNavigation } from './partials/StyledNavigation';
import { StyledScrollLeft } from './partials/StyledScrollLeft';
import { StyledScrollRight } from './partials/StyledScrollRight';
import { StyledTabs } from '../TabBase/partials/StyledTabs';
import { StyledTabSet } from './partials/StyledTabSet';
import { TabSetItem, TabContent, TabItem } from '.';
import { useScrollableTabSet } from './useScrollableTabSet';
export var TabSet = function TabSet(_ref) {
  var className = _ref.className,
    children = _ref.children,
    onChange = _ref.onChange,
    isFullWidth = _ref.isFullWidth,
    isScrollable = _ref.isScrollable,
    rest = _objectWithoutProperties(_ref, _excluded);
  function getActiveIndex(tabs) {
    var activeIndex = Children.toArray(tabs).findIndex(function (item) {
      if ( /*#__PURE__*/React.isValidElement(item)) {
        return item.props.isActive;
      }
      return false;
    });
    return activeIndex === -1 ? 0 : activeIndex;
  }
  var _useState = useState(function () {
      return _toConsumableArray(Array(children.length)).map(function () {
        return "".concat(uuidv4());
      });
    }),
    _useState2 = _slicedToArray(_useState, 1),
    tabIds = _useState2[0];
  var _useState3 = useState(getActiveIndex(children)),
    _useState4 = _slicedToArray(_useState3, 2),
    activeTab = _useState4[0],
    setActiveTab = _useState4[1];
  var _useScrollableTabSet = useScrollableTabSet(children),
    scrollToNextTab = _useScrollableTabSet.scrollToNextTab,
    tabsRef = _useScrollableTabSet.tabsRef,
    itemsRef = _useScrollableTabSet.itemsRef;
  function handleClick(e, index) {
    setActiveTab(index);
    if (onChange) {
      onChange(e, index);
    }
  }
  var handleKeyDown = function handleKeyDown(event) {
    var keyMap = {
      ArrowLeft: activeTab === 0 ? children.length - 1 : activeTab - 1,
      ArrowRight: activeTab === children.length - 1 ? 0 : activeTab + 1
    };
    var key = event.key;
    if (key in keyMap) {
      var index = keyMap[key];
      handleClick(event, index);
    }
  };
  return /*#__PURE__*/React.createElement(StyledTabSet, _extends({
    className: className
  }, rest), /*#__PURE__*/React.createElement(StyledHeader, {
    $isScrollable: isScrollable
  }, isScrollable && /*#__PURE__*/React.createElement(StyledScrollLeft, {
    "aria-label": "Scroll ".concat(SCROLL_DIRECTION.LEFT),
    onClick: scrollToNextTab(function (currentTabIndex) {
      return currentTabIndex - 1;
    })
  }, /*#__PURE__*/React.createElement(IconChevronLeft, {
    size: 36
  })), /*#__PURE__*/React.createElement(StyledNavigation, {
    $isScrollable: isScrollable,
    ref: tabsRef
  }, /*#__PURE__*/React.createElement(StyledTabs, {
    role: "tablist"
  }, Children.map(children, function (_ref2, index) {
    var props = _ref2.props;
    return /*#__PURE__*/React.createElement(TabItem, {
      tabId: tabIds[index],
      onClick: function onClick(e) {
        return handleClick(e, index);
      },
      onKeyDown: handleKeyDown,
      isActive: index === activeTab,
      isFullWidth: isFullWidth,
      isScrollable: isScrollable,
      ref: function ref(el) {
        itemsRef.current[index] = el;
        return itemsRef.current[index];
      }
    }, props.title);
  }))), isScrollable && /*#__PURE__*/React.createElement(StyledScrollRight, {
    "aria-label": "Scroll ".concat(SCROLL_DIRECTION.RIGHT),
    onClick: scrollToNextTab(function (currentTabIndex) {
      return currentTabIndex + 1;
    })
  }, /*#__PURE__*/React.createElement(IconChevronRight, {
    size: 36
  }))), /*#__PURE__*/React.createElement(StyledBody, {
    $isScrollable: isScrollable
  }, Children.map(children, function (child, index) {
    var _child$props = child.props,
      tabChildren = _child$props.children,
      title = _child$props.title,
      _ = _child$props.isActive,
      tabRest = _objectWithoutProperties(_child$props, _excluded2);
    return /*#__PURE__*/React.createElement(TabContent, _extends({
      tabId: tabIds[index],
      isActive: index === activeTab
    }, tabRest), /*#__PURE__*/React.createElement(TabSetItem, {
      title: title
    }, tabChildren));
  })));
};
TabSet.displayName = 'TabSet';