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 } from 'react';
import styled from 'styled-components';
import { scrollStyles } from './TabsNav.styles';
import { useIntersect } from '../../common/Hooks';
import { Flexbox, TabTitle, Typography } from '../..';
import { assert, mergeRefs } from '../../common/utils';
import { useKeyboardNavigation } from '../Tabs/useKeyboardNavigation';
import { useLink } from '../../common/Links';
var DEFAULT_SCROLL_OPTIONS = {
  active: false,
  scrollBarHidden: false,
  scrollFade: false
};
export var Item = function Item(_ref) {
  var children = _ref.children;
  return React.createElement("div", null, children);
};
Item.displayName = 'TabsNav.Tab';
var Link = function Link(props) {
  var LinkComponent = useLink();
  return React.createElement(LinkComponent, props);
};
var StyledLink = styled(Link).withConfig({
  displayName: "TabsNav__StyledLink",
  componentId: "sc-1vlwnet-0"
})(["display:flex;text-decoration:none;color:inherit;"]);
var StyledFlexbox = styled(Flexbox).withConfig({
  displayName: "TabsNav__StyledFlexbox",
  componentId: "sc-1vlwnet-1"
})(["list-style-type:none;padding:0;margin-top:0;margin-bottom:0;", ""], scrollStyles);
var StyledUl = styled.ul.withConfig({
  displayName: "TabsNav__StyledUl",
  componentId: "sc-1vlwnet-2"
})(["padding:0;margin-top:0;margin-bottom:0;list-style:none;"]);
var Title = React.forwardRef(function (_ref2, ref) {
  var active = _ref2.active,
    children = _ref2.children,
    setRef = _ref2.setRef,
    to = _ref2.to,
    onKeyDown = _ref2.onKeyDown,
    onClick = _ref2.onClick,
    height = _ref2.height,
    fullServerRedirect = _ref2.fullServerRedirect;
  return React.createElement(Typography, {
    type: "primary",
    weight: "bold",
    ref: ref,
    color: function color(t) {
      return active ? t.colorTokens.neutral.text_default : t.colorTokens.neutral.text_weak;
    }
  }, React.createElement(StyledLink, {
    to: to,
    innerRef: setRef,
    "aria-current": active ? 'page' : undefined,
    onKeyDown: onKeyDown,
    onClick: onClick,
    fullServerRedirect: fullServerRedirect
  }, React.createElement(TabTitle, {
    active: active,
    height: height,
    variant: "large"
  }, children)));
});
Title.displayName = 'TabsNav.Title';
var isItemOrUndefined = function isItemOrUndefined(x) {
  if (x == null || typeof x === 'undefined') {
    return true;
  }
  return _typeof(x) === 'object' && Object.hasOwnProperty.call(x, 'type');
};
export var TabsNav = function TabsNav(_ref3) {
  var children = _ref3.children,
    _ref3$height = _ref3.height,
    height = _ref3$height === void 0 ? 11 : _ref3$height,
    className = _ref3.className,
    _ref3$scrollOptions = _ref3.scrollOptions,
    scrollOptions = _ref3$scrollOptions === void 0 ? DEFAULT_SCROLL_OPTIONS : _ref3$scrollOptions;
  var _useKeyboardNavigatio = useKeyboardNavigation({
      itemsLength: React.Children.count(children)
    }),
    setRef = _useKeyboardNavigatio.setRef,
    onKeyDown = _useKeyboardNavigatio.onKeyDown;
  var titles = [];
  var _useIntersect = useIntersect(),
    _useIntersect2 = _slicedToArray(_useIntersect, 2),
    setIntersectionLeftRef = _useIntersect2[0],
    intersectionLeftRatio = _useIntersect2[1];
  var _useIntersect3 = useIntersect(),
    _useIntersect4 = _slicedToArray(_useIntersect3, 2),
    setIntersectionRightRef = _useIntersect4[0],
    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]);
  var setIntersectionRef = function setIntersectionRef(index) {
    if (!scrollOptions.active) {
      return null;
    }
    if (index === 0) {
      return setIntersectionLeftRef;
    }
    if (index === React.Children.count(children) - 1) {
      return setIntersectionRightRef;
    }
    return null;
  };
  React.Children.forEach(children, function (c, i) {
    if (!isItemOrUndefined(c)) {
      assert(false, 'There should be only <TabsNav.Tab> children inside of <TabsNav> component');
    } else if (c) {
      titles.push(React.createElement(Flexbox, {
        ref: mergeRefs([scrollOptions.scrollFade ? setIntersectionRef(i) : null, c.props.active ? scrollToRefCallback : null]),
        item: true,
        as: "li",
        key: c.props.to
      }, React.createElement(Title, {
        active: !!c.props.active,
        onClick: c.props.onTitleClick,
        setRef: setRef(i),
        to: c.props.to,
        fullServerRedirect: c.props.fullServerRedirect,
        onKeyDown: onKeyDown,
        height: height,
        className: c.props.className
      }, c.props.title)));
    }
  });
  return React.createElement(StyledFlexbox, {
    $height: height,
    $scrollOptions: scrollOptions,
    $intersectionLeftRatio: intersectionLeftRatio || 0,
    $intersectionRightRatio: intersectionRightRatio || 0,
    className: className,
    container: true,
    ref: scrollRef
  }, React.createElement(Flexbox, {
    container: true,
    direction: "row",
    gap: 4,
    sm: {
      gap: 8
    },
    as: StyledUl
  }, titles));
};
TabsNav.displayName = 'TabsNav';
TabsNav.Tab = Item;
export default TabsNav;