import _Tab from "@mui/material/Tab";
/*!
 * 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.
 */

import { TabContext as MuiTabContext, TabList as MuiTabList, TabPanel as MuiTabPanel } from "@mui/lab";
import { memo, useCallback, useEffect, useState } from "react";
import { Badge } from "./Badge.js";
import { Box } from "./Box.js";
import { useOdysseyDesignTokens } from "./OdysseyDesignTokensContext.js";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const TabLabel = ({
  label,
  notificationCount,
  notificationCountMax,
  tabState,
  value
}) => {
  const odysseyDesignTokens = useOdysseyDesignTokens();
  return _jsxs(_Fragment, {
    children: [label, notificationCount !== undefined && notificationCount > 0 && _jsx(Box, {
      sx: {
        marginInlineStart: notificationCount ? odysseyDesignTokens.Spacing2 : 0
      },
      children: _jsx(Badge, {
        badgeContent: notificationCount,
        badgeContentMax: notificationCountMax,
        type: value === tabState ? "attention" : "default"
      })
    })]
  });
};
const Tabs = ({
  ariaLabel,
  initialValue,
  tabs,
  value,
  onChange: onChangeProp
}) => {
  const [tabState, setTabState] = useState(initialValue ?? value ?? "0");
  const [scrollButtons, setScrollButtons] = useState(false);
  const onChange = useCallback((event, value) => {
    setTabState(value);
    onChangeProp?.(event, value);
  }, [onChangeProp]);
  useEffect(() => {
    if (value !== undefined) {
      setTabState(value);
    }
  }, [value]);
  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 = useCallback((tab, index) => {
    const {
      testId,
      isDisabled,
      label,
      startIcon,
      value,
      notificationCount,
      notificationCountMax
    } = tab;
    return _jsx(_Tab, {
      "data-se": testId,
      disabled: isDisabled,
      icon: startIcon,
      label: _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 _jsxs(MuiTabContext, {
    value: tabState,
    children: [_jsx(MuiTabList, {
      "aria-label": ariaLabel,
      onChange: onChange,
      scrollButtons: scrollButtons,
      variant: "scrollable",
      children: tabs.map((tab, index) => renderTab(tab, index))
    }), tabs.map((tab, index) => _jsx(MuiTabPanel, {
      value: tab.value ? tab.value : index.toString(),
      children: tab.children
    }, tab.value ? tab.value : index.toString()))]
  });
};
const MemoizedTabs = memo(Tabs);
MemoizedTabs.displayName = "Tabs";
export { MemoizedTabs as Tabs };
//# sourceMappingURL=Tabs.js.map