"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Tabs_exports = {};
__export(Tabs_exports, {
  default: () => Tabs
});
module.exports = __toCommonJS(Tabs_exports);
var import_react = __toESM(require("react"));
var import_styled_components = __toESM(require("styled-components"));
var import_Flex = __toESM(require("../../layout/Flex"));
var import_Tag = __toESM(require("../../Tag"));
var import_Text = __toESM(require("../../asorted/Text"));
const Container = (0, import_styled_components.default)(import_Flex.default).attrs({
  flexDirection: "column",
  justifyContent: "center",
  alignItems: "center"
})``;
const TabHeader = import_styled_components.default.div`
  display: flex;
  flex-direction: column;
  flex: 1;
  width: 100%;
`;
const TabHeaderContent = (0, import_styled_components.default)(import_Flex.default).attrs({
  flex: 1,
  alignItems: "center"
})`
  width: 100%;
`;
const TabHeaderBox = import_styled_components.default.div`
  display: flex;
  flex-grow: inherit;
  justify-content: center;
  text-align: center;
  cursor: ${(p) => p.disabled ? "default" : "pointer"};
  padding: 8px 12px;
`;
const HeaderTitle = (0, import_styled_components.default)(import_Text.default).attrs({
  fontWeight: "600"
})`
  margin-inline: 12px;
  color: ${(p) => p.selected ? p.theme.colors.neutral.c100 : p.theme.colors.neutral.c80};
`;
const HeaderBottomBarFixed = (0, import_styled_components.default)(import_Flex.default).attrs({
  flex: 1
})`
  width: 100%;
  position: relative;
  top: 3px;
  border-bottom: 1px solid ${(p) => p.theme.colors.neutral.c40};
`;
const HeaderBottomBarMoving = import_styled_components.default.div`
  position: relative;
  left: ${(p) => p.left}px;
  width: ${(p) => p.width}px;
  transition: all 400ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
  border-bottom: solid 3px;
  border-bottom-color: ${(p) => p.theme.colors.primary.c70};
`;
const Badge = (0, import_styled_components.default)(import_Tag.default).attrs((p) => ({
  borderRadius: p.theme.radii[3],
  backgroundColor: p.theme.colors.primary.c70,
  color: p.theme.colors.neutral.c00
}))`
  padding: 5px;
  min-width: 24px;
`;
const MyBottomBar = (props) => {
  const { width, left } = props;
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(HeaderBottomBarFixed, null), /* @__PURE__ */ import_react.default.createElement(HeaderBottomBarMoving, { width, left }));
};
const HeaderElement = (0, import_react.forwardRef)((props, ref) => {
  const { onClick, badge, disabled, selected, title } = props;
  return /* @__PURE__ */ import_react.default.createElement(TabHeaderBox, { ref, disabled, onClick }, /* @__PURE__ */ import_react.default.createElement(HeaderTitle, { selected }, title), (badge || badge === 0) && /* @__PURE__ */ import_react.default.createElement(Badge, null, badge));
});
const MainContent = (0, import_styled_components.default)(import_Flex.default).attrs({
  flex: 1
})`
  width: 100%;
  color: ${(p) => p.active ? p.theme.colors.neutral.c00 : p.theme.colors.neutral.c70};
  background-color: ${(p) => p.active ? p.theme.colors.neutral.c100 : p.theme.colors.neutral.c00};
`;
function Tabs(props) {
  const { tabs, onTabChange } = props;
  const [activeIndex, setActiveIndex] = (0, import_react.useState)(tabs[0].index);
  const [bottomBar, updateBottomBar] = (0, import_react.useState)({ left: 0, width: 0 });
  const mainTab = activeIndex >= 0 ? tabs[activeIndex] : null;
  const refs = tabs.map(() => (0, import_react.createRef)());
  (0, import_react.useEffect)(() => {
    var _a;
    const newIndex = props.activeIndex || activeIndex;
    setActiveIndex(newIndex);
    if (refs[0].current) {
      const refIndex = tabs.findIndex((t) => t.index === newIndex);
      const refsToHandle = refs.slice(0, refIndex);
      const width = ((_a = refs[refIndex].current) == null ? void 0 : _a.offsetWidth) || 0;
      const left = refsToHandle.reduce((total, ref) => {
        var _a2;
        return total + (((_a2 = ref.current) == null ? void 0 : _a2.offsetWidth) || 0);
      }, 0);
      updateBottomBar({
        width,
        left
      });
    }
  }, [activeIndex]);
  const onTabClick = (index) => {
    const tab = tabs.find((t) => t.index === index);
    if (tab && !tab.disabled) {
      setActiveIndex(index);
      onTabChange && onTabChange(index);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(Container, null, /* @__PURE__ */ import_react.default.createElement(TabHeader, null, /* @__PURE__ */ import_react.default.createElement(TabHeaderContent, null, tabs.map((tab, i) => /* @__PURE__ */ import_react.default.createElement(
    HeaderElement,
    {
      ref: refs[i],
      title: tab.title,
      selected: activeIndex === tab.index,
      badge: tab.badge,
      disabled: !!tab.disabled,
      onClick: () => onTabClick(tab.index)
    }
  ))), /* @__PURE__ */ import_react.default.createElement(MyBottomBar, { width: bottomBar.width, left: bottomBar.left })), /* @__PURE__ */ import_react.default.createElement(MainContent, null, mainTab && mainTab.Component));
}
//# sourceMappingURL=index.js.map
