"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Nav = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _Styles = require("./Item/Styles");
var _Styles2 = require("./Styles");
var _utils = require("../../utils");
var _ExpandSingle = require("../Expanders/ExpandSingle");
var _Select = require("../Select/Select");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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; }
var Nav = exports.Nav = function Nav(_ref) {
  var _dimensions$width, _childDimensions$widt;
  var children = _ref.children,
    _ref$alignment = _ref.alignment,
    alignment = _ref$alignment === void 0 ? "left" : _ref$alignment,
    onSelect = _ref.onSelect,
    className = _ref.className,
    _ref$role = _ref.role,
    role = _ref$role === void 0 ? "bold" : _ref$role,
    targetId = _ref.targetId;
  var _useState = (0, _react.useState)(0),
    _useState2 = _slicedToArray(_useState, 2),
    active = _useState2[0],
    setActive = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    _useState4 = _slicedToArray(_useState3, 2),
    moreOpened = _useState4[0],
    setMoreOpened = _useState4[1];
  var _useState5 = (0, _react.useState)("inline"),
    _useState6 = _slicedToArray(_useState5, 2),
    type = _useState6[0],
    setType = _useState6[1];
  var setMoreOpen = (0, _react.useCallback)(function () {
    return setMoreOpened(true);
  }, []);
  var setMoreClose = (0, _react.useCallback)(function () {
    return setMoreOpened(false);
  }, []);
  var navItemNames = (0, _react.useMemo)(function () {
    return children ? _react.Children.toArray(children).map(function (item) {
      var _item$props;
      return item === null || item === void 0 || (_item$props = item.props) === null || _item$props === void 0 ? void 0 : _item$props.name;
    }) : [];
  }, [children]);
  var allOptions = (0, _react.useMemo)(function () {
    return navItemNames === null || navItemNames === void 0 ? void 0 : navItemNames.map(function (name, index) {
      return {
        id: index,
        name: name
      };
    });
  }, [navItemNames]);
  var navRef = (0, _react.useRef)(null);
  var wrapRef = (0, _react.useRef)(null);
  var dimensions = (0, _utils.useResizeObserver)(wrapRef);
  var childDimensions = (0, _utils.useResizeObserver)(navRef);
  var parentWidth = (_dimensions$width = dimensions === null || dimensions === void 0 ? void 0 : dimensions.width) !== null && _dimensions$width !== void 0 ? _dimensions$width : 0;
  var childWidth = (_childDimensions$widt = childDimensions === null || childDimensions === void 0 ? void 0 : childDimensions.width) !== null && _childDimensions$widt !== void 0 ? _childDimensions$widt : 0;
  var _useState7 = (0, _react.useState)(childWidth),
    _useState8 = _slicedToArray(_useState7, 2),
    childrenWidth = _useState8[0],
    setChildrenWidth = _useState8[1];
  (0, _react.useLayoutEffect)(function () {
    if (type === "inline") setChildrenWidth(childWidth);
  }, [childWidth, type]);
  var onClick = (0, _react.useCallback)(function (e) {
    if (e.metaKey || e.ctrlKey) {
      return;
    }
    if (e.target && e.currentTarget.dataset.index && !e.currentTarget.dataset.disabled) {
      var _index = Number(e.currentTarget.dataset.index);
      setActive(_index);
      if (onSelect) {
        onSelect(_index);
      }
    }
  }, [onSelect]);
  var onMoreOptionClick = (0, _react.useCallback)(function (id) {
    setActive(id);
    if (onSelect) {
      onSelect(id);
    }
  }, [onSelect]);
  (0, _react.useMemo)(function () {
    _react.Children.forEach(children, function (child, index) {
      if (child && child.props.active) {
        setActive(index);
      }
    });
  }, [children]);
  var navAsSelect = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react.default.createElement(_Styles2.StyledNavAsSelect, {
      id: targetId,
      $active: true
    }, /*#__PURE__*/_react.default.createElement("span", null, navItemNames[active]), /*#__PURE__*/_react.default.createElement(_ExpandSingle.ExpandSingle, {
      expanded: moreOpened,
      fill: "currentColor"
    }));
  }, [active, moreOpened, navItemNames, targetId]);
  (0, _react.useEffect)(function () {
    if (!navRef.current || !wrapRef.current) return;
    if (!childrenWidth || !parentWidth) return;
    if (childrenWidth < parentWidth) {
      setType("inline");
    } else {
      setType("dropdown");
    }
  }, [childrenWidth, parentWidth]);
  if (!children) return;
  return /*#__PURE__*/_react.default.createElement(_Styles2.StyledNav, {
    className: (0, _classnames.default)("c-nav", className),
    $align: alignment,
    $role: role,
    ref: wrapRef
  }, /*#__PURE__*/_react.default.createElement(_Styles2.StyledNavList, {
    ref: navRef
  }, type === "inline" && _react.Children.toArray(children).map(function (child, index) {
    var element = child;
    return /*#__PURE__*/(0, _react.cloneElement)(element, {
      active: index === active,
      role,
      onClick,
      "data-index": index,
      "data-disabled": element.props.disabled
    });
  }), type === "dropdown" ? /*#__PURE__*/_react.default.createElement(_Styles.StyledNavListItemMore, {
    name: navItemNames[active],
    role: role
  }, /*#__PURE__*/_react.default.createElement(_Select.Select, {
    selected: active,
    target: navAsSelect,
    disabledInternalSort: true,
    options: allOptions,
    type: "single",
    mode: "normal",
    forceCloseMenu: true,
    disableSearch: true,
    keepSameOptionsOrder: true,
    onChange: onMoreOptionClick,
    onSelectOpen: setMoreOpen,
    onSelectClose: setMoreClose
  })) : null));
};
Nav.displayName = "Nav";
//# sourceMappingURL=Nav.js.map