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); }
var _excluded = ["children", "UNSAFE_helixHeader"],
  _excluded2 = ["as", "disabled", "role", "active", "selected", "children", "onBlur", "onFocus", "onMount", "onUnmount", "overflowing", "dropdown", "variant", "UNSAFE_helixHeader"],
  _excluded3 = ["onPress", "onClick", "value"],
  _excluded4 = ["children", "className", "dark", "qa", "UNSAFE_helixHeader"];
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import { CaretDown, CaretUp } from '@procore/core-icons/dist';
import React from 'react';
import { useButton } from '@react-aria/button';
import { MenuImperative } from '../MenuImperative/MenuImperative';
import { OverlayTrigger, useOverlayTriggerContext } from '../OverlayTrigger/OverlayTrigger';
import { useI18nContext } from '../_hooks/I18n';
import { useResize } from '../_hooks/Resize';
import { spacing } from '../_styles/spacing';
import { addSubcomponents } from '../_utils/addSubcomponents';
import { getBoundingRect } from '../_utils/dom';
import { mergeRefs } from '../_utils/mergeRefs';
import { getStyledComponents, StyledOverlay, StyledTabList, StyledTabs } from './Tabs.styles';
var ExtraTabSpacing = spacing.xxl;
var HelixHeaderExtraTabSpacing = spacing.sm;
function noop() {}
export var Link = /*#__PURE__*/React.forwardRef(function Link(_ref, ref) {
  var children = _ref.children,
    _ref$UNSAFE_helixHead = _ref.UNSAFE_helixHeader,
    UNSAFE_helixHeader = _ref$UNSAFE_helixHead === void 0 ? false : _ref$UNSAFE_helixHead,
    props = _objectWithoutProperties(_ref, _excluded);
  var Styled = getStyledComponents(UNSAFE_helixHeader);
  return /*#__PURE__*/React.createElement(Styled.Link, _extends({
    ref: ref
  }, props), children);
});
function TabInternal(_ref2, ref) {
  var as = _ref2.as,
    disabled = _ref2.disabled,
    role = _ref2.role,
    _ref2$active = _ref2.active,
    active = _ref2$active === void 0 ? false : _ref2$active,
    _ref2$selected = _ref2.selected,
    selected = _ref2$selected === void 0 ? false : _ref2$selected,
    children = _ref2.children,
    onBlur = _ref2.onBlur,
    onFocus = _ref2.onFocus,
    _ref2$onMount = _ref2.onMount,
    onMount = _ref2$onMount === void 0 ? noop : _ref2$onMount,
    _ref2$onUnmount = _ref2.onUnmount,
    onUnmount = _ref2$onUnmount === void 0 ? noop : _ref2$onUnmount,
    _ref2$overflowing = _ref2.overflowing,
    overflowing = _ref2$overflowing === void 0 ? false : _ref2$overflowing,
    _ref2$dropdown = _ref2.dropdown,
    dropdown = _ref2$dropdown === void 0 ? false : _ref2$dropdown,
    _ref2$variant = _ref2.variant,
    variant = _ref2$variant === void 0 ? '' : _ref2$variant,
    _ref2$UNSAFE_helixHea = _ref2.UNSAFE_helixHeader,
    UNSAFE_helixHeader = _ref2$UNSAFE_helixHea === void 0 ? false : _ref2$UNSAFE_helixHea,
    props = _objectWithoutProperties(_ref2, _excluded2);
  var _ref3 = props,
    onPress = _ref3.onPress,
    onClick = _ref3.onClick,
    value = _ref3.value,
    buttonPropsBase = _objectWithoutProperties(_ref3, _excluded3);
  var elementType = role === 'link' ? 'a' : role === 'button' ? 'button' : 'div';
  var _useButton = useButton(_objectSpread(_objectSpread({}, buttonPropsBase), {}, {
      onBlur: onBlur,
      onFocus: onFocus,
      elementType: elementType,
      isDisabled: disabled,
      onPress: onPress,
      type: 'button'
    }), ref),
    buttonProps = _useButton.buttonProps;
  var a11yProps = role === 'button' ? _objectSpread(_objectSpread({}, buttonProps), {}, {
    'aria-current': selected || active,
    as: as || 'button',
    $clickable: !disabled
  }) : role === 'link' ? _objectSpread(_objectSpread({}, buttonProps), {}, {
    'aria-current': selected || active,
    as: as || 'a',
    $clickable: !disabled
  }) : role === 'tab' ? {
    role: 'tab',
    onBlur: onBlur,
    onFocus: onFocus
  } : {
    onBlur: onBlur,
    onFocus: onFocus
  };
  var innerRef = React.useRef(null);
  React.useEffect(function () {
    onMount(innerRef.current);
    return onUnmount;
  }, []);
  var validChild = /*#__PURE__*/React.isValidElement(children);
  var tabSelected = active || variant === 'active' || selected;
  var Styled = getStyledComponents(UNSAFE_helixHeader);

  // For button/link roles, spread buttonPropsBase to preserve useButton's onClick
  // For other roles, spread all props
  var roleSpecificProps = role === 'button' || role === 'link' ? buttonPropsBase : props;
  return /*#__PURE__*/React.createElement(Styled.Tab, _extends({
    $selected: tabSelected,
    $dark: props.dark,
    ref: mergeRefs(ref || null, innerRef)
  }, a11yProps, roleSpecificProps), /*#__PURE__*/React.createElement(Styled.TabInner, {
    $selected: tabSelected,
    $dark: props.dark
  }, validChild ? /*#__PURE__*/React.cloneElement(children, {
    'aria-current': tabSelected ? true : undefined
  }) : children));
}
export var Tab = /*#__PURE__*/React.forwardRef(TabInternal);
export var MoreMenu = /*#__PURE__*/React.forwardRef(function MoreMenu(_ref4, ref) {
  var children = _ref4.children,
    id = _ref4.id;
  var ctx = useOverlayTriggerContext();
  var menuRef = React.useRef(null);
  React.useEffect(function () {
    var _menuRef$current, _menuRef$current$el, _menuRef$current2, _menuRef$current3;
    (_menuRef$current = menuRef.current) === null || _menuRef$current === void 0 ? void 0 : (_menuRef$current$el = _menuRef$current.el) === null || _menuRef$current$el === void 0 ? void 0 : _menuRef$current$el.focus();
    (_menuRef$current2 = menuRef.current) === null || _menuRef$current2 === void 0 ? void 0 : _menuRef$current2.highlightFirst();
    (_menuRef$current3 = menuRef.current) === null || _menuRef$current3 === void 0 ? void 0 : _menuRef$current3.highlightSelected();
  }, []);
  function onSelect(selection) {
    ctx.hide(selection.event);
  }
  return /*#__PURE__*/React.createElement(StyledOverlay, {
    ref: ref
  }, /*#__PURE__*/React.createElement(MenuImperative, {
    id: id,
    role: "menu",
    ref: menuRef,
    onSelect: onSelect
  }, /*#__PURE__*/React.createElement(MenuImperative.Options, null, React.Children.map(children, function (child, i) {
    if ( /*#__PURE__*/React.isValidElement(child)) {
      return /*#__PURE__*/React.createElement(MenuImperative.Item, {
        item: null,
        key: i,
        selected: child.props.selected || child.props.active
      }, /*#__PURE__*/React.cloneElement(child, {
        ref: null
      }));
    }
  }))));
});
var Tabs_ = /*#__PURE__*/React.forwardRef(function Tabs(_ref5, ref) {
  var children = _ref5.children,
    className = _ref5.className,
    _ref5$dark = _ref5.dark,
    dark = _ref5$dark === void 0 ? false : _ref5$dark,
    qa = _ref5.qa,
    _ref5$UNSAFE_helixHea = _ref5.UNSAFE_helixHeader,
    UNSAFE_helixHeader = _ref5$UNSAFE_helixHea === void 0 ? false : _ref5$UNSAFE_helixHea,
    props = _objectWithoutProperties(_ref5, _excluded4);
  var i18n = useI18nContext();
  var _React$useState = React.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    moreMenuIsShown = _React$useState2[0],
    setMoreMenuIsShown = _React$useState2[1];
  var _React$useState3 = React.useState([]),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    tabElements = _React$useState4[0],
    setTabElements = _React$useState4[1];
  var _React$useState5 = React.useState(0),
    _React$useState6 = _slicedToArray(_React$useState5, 2),
    cutoffIndex = _React$useState6[0],
    setCutoffIndex = _React$useState6[1];
  var containerRef = React.useRef(null);
  function recalculate() {
    var container = getBoundingRect(containerRef.current);
    var _tabElements$reduce = tabElements.reduce(function (acc, el) {
        if (!el) {
          return acc;
        }
        var rect = getBoundingRect(el);
        var extraSpace = UNSAFE_helixHeader ? HelixHeaderExtraTabSpacing : ExtraTabSpacing;
        return {
          subtotals: [].concat(_toConsumableArray(acc.subtotals), [acc.total + rect.width]),
          total: acc.total + rect.width + extraSpace
        };
      }, {
        subtotals: new Array(),
        total: 0
      }),
      subtotals = _tabElements$reduce.subtotals;
    var index = subtotals.findIndex(function (val) {
      return val >= container.width;
    });
    setCutoffIndex(index);
  }
  useResize({
    onResize: recalculate
  });
  React.useEffect(recalculate);
  var filtered = React.Children.toArray(children).filter(function (child) {
    return child;
  });
  var overflowing = filtered.slice(cutoffIndex - 1);
  var isOverflowingSelected = React.Children.toArray(overflowing).some(function (child) {
    return /*#__PURE__*/React.isValidElement(child) && (child.props.active || child.props.selected);
  });
  var Styled = getStyledComponents(UNSAFE_helixHeader);
  return /*#__PURE__*/React.createElement(StyledTabs, _extends({
    ref: ref,
    $dark: dark,
    index: cutoffIndex
  }, props), /*#__PURE__*/React.createElement(StyledTabList, {
    ref: containerRef
  }, filtered.map(function (child, i) {
    if ( /*#__PURE__*/React.isValidElement(child)) return /*#__PURE__*/React.cloneElement(child, {
      dark: dark,
      onMount: function onMount(el) {
        setTabElements(function (elements) {
          var oldElements = _toConsumableArray(elements);
          oldElements[i] = el;
          return oldElements;
        });
      },
      onUnmount: function onUnmount() {
        setTabElements(function (elements) {
          var oldElements = _toConsumableArray(elements);
          oldElements[i] = null;
          return oldElements;
        });
      }
    });
  }), /*#__PURE__*/React.createElement(OverlayTrigger, {
    autoFocus: true,
    beforeShow: function beforeShow() {
      return setMoreMenuIsShown(true);
    },
    afterHide: function afterHide() {
      return setMoreMenuIsShown(false);
    },
    trigger: "click",
    passA11yPropsToOverlay: true,
    placement: "bottom-right",
    overlay: /*#__PURE__*/React.createElement(MoreMenu, null, overflowing),
    ref: ref,
    role: "menu"
  }, /*#__PURE__*/React.createElement(Styled.DropdownTab, {
    $selected: isOverflowingSelected,
    $dark: dark,
    "aria-hidden": !overflowing.length,
    as: "button"
  }, /*#__PURE__*/React.createElement(Styled.DropdownTabInner, {
    $selected: isOverflowingSelected,
    $dark: dark
  }, /*#__PURE__*/React.createElement(Styled.Link, {
    "data-qa": qa === null || qa === void 0 ? void 0 : qa.more
  }, i18n.t('core.tabs.more')), moreMenuIsShown ? /*#__PURE__*/React.createElement(CaretUp, {
    size: "sm"
  }) : /*#__PURE__*/React.createElement(CaretDown, {
    size: "sm"
  }))))));
});
Tabs_.displayName = 'Tabs';
Link.displayName = 'Tabs.Link';

// @ts-ignore
Tab.displayName = 'Tabs.Tab';

/**

 We use tabs to navigate between different, but related content.
 Tabs are commonly used on tool landing pages and on item detail pages.

 @since 10.19.0

 @see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-tabs--demo)

 @see [Design Guidelines](https://design.procore.com/tabs)

 */
export var Tabs = addSubcomponents({
  Link: Link,
  Tab: Tab
}, Tabs_);
//# sourceMappingURL=Tabs.js.map