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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import React, { useCallback, useRef, useState } from 'react';
import styled from 'styled-components';
import { StyledButton, StyledFlexbox, StyledSeparator, StyledUl } from './Tabs.styles';
import { useIntersect } from '../../common/Hooks';
import { Flexbox, TabTitle, Typography } from '../..';
import { assert } from '../../common/utils';
import { useKeyboardNavigation } from './useKeyboardNavigation';
var DEFAULT_SCROLL_OPTIONS = {
  active: false,
  scrollBarHidden: false,
  scrollFade: false
};
export var Item = function Item(_ref) {
  var children = _ref.children;
  return React.createElement(React.Fragment, null, children);
};
Item.displayName = 'Tabs.Content';
var Title = function Title(_ref2) {
  var activeFromProps = _ref2.active,
    children = _ref2.children,
    onClick = _ref2.onClick,
    onKeyDown = _ref2.onKeyDown,
    index = _ref2.index,
    setRef = _ref2.setRef,
    height = _ref2.height,
    variant = _ref2.variant;
  var active = activeFromProps;
  return React.createElement(Typography, {
    type: "secondary",
    weight: "bold"
  }, React.createElement(StyledButton, {
    ref: setRef,
    type: "button",
    onClick: onClick,
    onKeyDown: onKeyDown,
    "aria-selected": active,
    role: "tab",
    id: "tabs-tab-".concat(index),
    tabIndex: active ? 0 : -1,
    $active: active
  }, React.createElement(TabTitle, {
    active: active,
    height: height,
    variant: variant
  }, children)));
};
Title.displayName = 'Tabs.Title';
var TabContent = styled.section.withConfig({
  displayName: "Tabs__TabContent",
  componentId: "sc-bqti0u-0"
})(["height:100%;"]);
var isItemOrUndefined = function isItemOrUndefined(x) {
  if (x == null || typeof x === 'undefined') {
    return true;
  }
  return _typeof(x) === 'object' && Object.hasOwnProperty.call(x, 'type');
};
var components = {
  TabContent: TabContent,
  TabBar: StyledFlexbox
};
export var Tabs = function Tabs(_ref3) {
  var activeTabIndex = _ref3.activeTabIndex,
    children = _ref3.children,
    className = _ref3.className,
    fullWidthSeparator = _ref3.fullWidthSeparator,
    _ref3$height = _ref3.height,
    height = _ref3$height === void 0 ? 8 : _ref3$height,
    _ref3$initialActiveTa = _ref3.initialActiveTabIndex,
    initialActiveTabIndex = _ref3$initialActiveTa === void 0 ? 0 : _ref3$initialActiveTa,
    _ref3$scrollOptions = _ref3.scrollOptions,
    scrollOptions = _ref3$scrollOptions === void 0 ? DEFAULT_SCROLL_OPTIONS : _ref3$scrollOptions,
    _ref3$variant = _ref3.variant,
    variant = _ref3$variant === void 0 ? 'normal' : _ref3$variant;
  var _useState = useState(initialActiveTabIndex),
    _useState2 = _slicedToArray(_useState, 2),
    active = _useState2[0],
    setActive = _useState2[1];
  var isControlled = typeof activeTabIndex !== 'undefined';
  var _useIntersect = useIntersect(),
    _useIntersect2 = _slicedToArray(_useIntersect, 2),
    intersectionLeftRatio = _useIntersect2[1];
  var _useIntersect3 = useIntersect(),
    _useIntersect4 = _slicedToArray(_useIntersect3, 2),
    intersectionRightRatio = _useIntersect4[1];
  var scrollRef = useRef(null);
  var scrollToRefCallback = useCallback(function (ref) {
    if (ref && scrollRef && scrollRef.current && scrollOptions.active) {
      var offsetleft = ref.offsetLeft;
      var _ref$getBoundingClien = ref.getBoundingClientRect(),
        tabWidth = _ref$getBoundingClien.width;
      var containerWidth = scrollRef.current.getBoundingClientRect().width;
      var scrollToLeft = offsetleft + tabWidth / 2 - containerWidth / 2;
      setTimeout(function () {
        var _scrollRef$current;
        (_scrollRef$current = scrollRef.current) === null || _scrollRef$current === void 0 || _scrollRef$current.scrollTo({
          left: scrollToLeft,
          behavior: 'smooth'
        });
      }, 0);
    }
  }, [scrollOptions.active]);
  if (isControlled) active = activeTabIndex;
  var handleTitleClick = function handleTitleClick(i, handler) {
    return function (e) {
      if (handler) handler(e);
      if (!isControlled) setActive(i);
    };
  };
  var _useKeyboardNavigatio = useKeyboardNavigation({
      itemsLength: React.Children.count(children)
    }),
    setRef = _useKeyboardNavigatio.setRef,
    onKeyDown = _useKeyboardNavigatio.onKeyDown;
  var titles = [];
  var contents = null;
  React.Children.forEach(children, function (c, i) {
    var isActive = i === active;
    if (!isItemOrUndefined(c)) {
      assert(false, 'There should be only <Tabs.Tab> children inside of <Tabs> component');
    } else if (c) {
      titles.push(React.createElement(Flexbox, {
        ref: isActive ? scrollToRefCallback : null,
        item: true,
        as: "li",
        role: "presentation",
        key: "tabs-".concat(i)
      }, React.createElement(Title, {
        active: isActive,
        index: i,
        onClick: handleTitleClick(i, c.props.onTitleClick),
        onKeyDown: onKeyDown,
        setRef: setRef(i),
        height: height,
        variant: variant
      }, c.props.title)));
      if (isActive) {
        contents = React.createElement(TabContent, {
          id: "tabs-tabpanel-".concat(i),
          role: "tabpanel",
          "aria-labelledby": "tabs-tab-".concat(i),
          hidden: !isActive
        }, c);
      }
    }
  });
  return React.createElement(React.Fragment, null, React.createElement(StyledFlexbox, {
    container: true,
    role: "tablist",
    className: className,
    $height: height,
    $scrollOptions: scrollOptions,
    $intersectionLeftRatio: intersectionLeftRatio || 0,
    $intersectionRightRatio: intersectionRightRatio || 0,
    ref: scrollRef
  }, React.createElement(Flexbox, {
    container: true,
    direction: "row",
    gap: 4,
    sm: {
      gap: variant === 'large' ? 8 : 4
    },
    as: StyledUl
  }, titles)), React.createElement(StyledSeparator, {
    fullWidth: fullWidthSeparator
  }), contents);
};
Tabs.displayName = 'Tabs';
Tabs.Tab = Item;
Tabs.components = components;