"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Tabs = void 0;
var _Tab2 = _interopRequireDefault(require("@mui/material/Tab"));
var _lab = require("@mui/lab");
var _react = require("react");
var _Badge = require("./Badge.cjs");
var _Box = require("./Box.cjs");
var _OdysseyDesignTokensContext = require("./OdysseyDesignTokensContext.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * Copyright (c) 2022-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

const TabLabel = ({
  label,
  notificationCount,
  notificationCountMax,
  tabState,
  value
}) => {
  const odysseyDesignTokens = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [label, notificationCount !== undefined && notificationCount > 0 && (0, _jsxRuntime.jsx)(_Box.Box, {
      sx: {
        marginInlineStart: notificationCount ? odysseyDesignTokens.Spacing2 : 0
      },
      children: (0, _jsxRuntime.jsx)(_Badge.Badge, {
        badgeContent: notificationCount,
        badgeContentMax: notificationCountMax,
        type: value === tabState ? "attention" : "default"
      })
    })]
  });
};
const Tabs = ({
  ariaLabel,
  initialValue,
  tabs,
  value,
  onChange: onChangeProp
}) => {
  const [tabState, setTabState] = (0, _react.useState)(initialValue ?? value ?? "0");
  const [scrollButtons, setScrollButtons] = (0, _react.useState)(false);
  const onChange = (0, _react.useCallback)((event, value) => {
    setTabState(value);
    onChangeProp?.(event, value);
  }, [onChangeProp]);
  (0, _react.useEffect)(() => {
    if (value !== undefined) {
      setTabState(value);
    }
  }, [value]);
  (0, _react.useEffect)(() => {
    let animationFrame;
    const cleanup = () => {
      cancelAnimationFrame(animationFrame);
      document.removeEventListener("visibilitychange", refreshScrollButtons);
    };
    function refreshScrollButtons() {
      animationFrame = requestAnimationFrame(() => {
        cleanup();
        setScrollButtons("auto");
      });
    }
    if (scrollButtons !== "auto") {
      document.addEventListener("visibilitychange", refreshScrollButtons);
    }
    return () => {
      cleanup();
    };
  }, [scrollButtons]);
  const renderTab = (0, _react.useCallback)((tab, index) => {
    const {
      testId,
      isDisabled,
      label,
      startIcon,
      value,
      notificationCount,
      notificationCountMax
    } = tab;
    return (0, _jsxRuntime.jsx)(_Tab2.default, {
      "data-se": testId,
      disabled: isDisabled,
      icon: startIcon,
      label: (0, _jsxRuntime.jsx)(TabLabel, {
        label: label,
        notificationCount: notificationCount,
        notificationCountMax: notificationCountMax,
        tabState: tabState,
        value: value
      }),
      tabIndex: 0,
      value: value ? value : index.toString()
    }, value ? value : index.toString());
  }, [tabState]);
  return (0, _jsxRuntime.jsxs)(_lab.TabContext, {
    value: tabState,
    children: [(0, _jsxRuntime.jsx)(_lab.TabList, {
      "aria-label": ariaLabel,
      onChange: onChange,
      scrollButtons: scrollButtons,
      variant: "scrollable",
      children: tabs.map((tab, index) => renderTab(tab, index))
    }), tabs.map((tab, index) => (0, _jsxRuntime.jsx)(_lab.TabPanel, {
      value: tab.value ? tab.value : index.toString(),
      children: tab.children
    }, tab.value ? tab.value : index.toString()))]
  });
};
const MemoizedTabs = exports.Tabs = (0, _react.memo)(Tabs);
MemoizedTabs.displayName = "Tabs";
//# sourceMappingURL=Tabs.cjs.map