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(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_use_refs = __toESM(require("../hooks/use-refs"));
var import_use_scroll_parent = __toESM(require("../hooks/use-scroll-parent"));
var import_use_window_size = __toESM(require("../hooks/use-window-size"));
var import_sticky = __toESM(require("../sticky"));
var import_TabsTitle = __toESM(require("./TabsTitle"));
var import_TabsContent = __toESM(require("./TabsContent"));
var import_TabsContext = __toESM(require("./TabsContext"));
var import_utils = require("../utils");
var import_interceptor = require("../utils/interceptor");
var import_constant = require("../utils/constant");
var import_hooks = require("../hooks");
var import_use_event_listener = __toESM(require("../hooks/use-event-listener"));
var import_utils2 = require("./utils");
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_PopupContext = __toESM(require("../popup/PopupContext"));
const Tabs = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const popupContext = (0, import_react.useContext)(import_PopupContext.default);
  const [bem] = createNamespace("tabs", prefixCls);
  const {
    children,
    color,
    background
  } = props;
  const root = (0, import_react.useRef)(null);
  const [wrapRef, setWrapRef] = (0, import_react.useState)(null);
  const initChange = (0, import_react.useRef)(false);
  const lockScroll = (0, import_react.useRef)(false);
  const stickyFixed = (0, import_react.useRef)(false);
  const navRef = (0, import_react.useRef)(null);
  const scroller = (0, import_use_scroll_parent.default)(root);
  const windowSize = (0, import_use_window_size.default)();
  const [titleRefs, setTitleRefs] = (0, import_use_refs.default)();
  const [contentRefs, setContentRefs] = (0, import_use_refs.default)();
  const tabHeight = (0, import_react.useMemo)(() => {
    if (!wrapRef)
      return 0;
    return (0, import_utils.getVisibleHeight)(wrapRef);
  }, [wrapRef]);
  const tabHeightRef = (0, import_react.useRef)(tabHeight);
  tabHeightRef.current = tabHeight;
  const [state, setState] = (0, import_hooks.useSetState)({
    inited: false,
    position: "",
    currentIndex: -1,
    lineStyle: {
      backgroundColor: props.color
    }
  });
  const childrenList = (0, import_react.useMemo)(() => (0, import_utils.parseChildList)(props.children), [props.children]);
  const scrollable = (0, import_react.useMemo)(() => childrenList.length > props.swipeThreshold || !props.ellipsis, [childrenList.length, props.swipeThreshold, props.ellipsis]);
  const navStyle = (0, import_react.useMemo)(() => ({
    borderColor: color,
    background
  }), [color, background]);
  const getTabName = (tab, index) => {
    var _a;
    return (_a = tab == null ? void 0 : tab.name) != null ? _a : index;
  };
  const currentName = (0, import_react.useMemo)(() => {
    const activeTab = childrenList[state.currentIndex];
    return activeTab && getTabName(activeTab, state.currentIndex);
  }, [state.currentIndex]);
  const offsetTopPx = (0, import_react.useMemo)(() => (0, import_utils.unitToPx)(props.offsetTop), [props.offsetTop]);
  const scrollIntoView = (immediate) => {
    const nav = navRef.current;
    if (!scrollable || !nav || !titleRefs || !titleRefs[state.currentIndex]) {
      return;
    }
    const title = titleRefs[state.currentIndex];
    const to = title.offsetLeft - (nav.offsetWidth - title.offsetWidth) / 2;
    (0, import_utils.scrollLeftTo)(nav, to, immediate ? 0 : +props.duration);
  };
  const setLine = (immediate) => {
    let shouldAnimate = state.inited;
    if (immediate)
      shouldAnimate = false;
    const titles = titleRefs;
    const hidden = (0, import_utils.isHidden)(root.current);
    if (!titles || !titles[state.currentIndex] || props.type !== "line" || hidden) {
      return;
    }
    const title = titles[state.currentIndex];
    const {
      lineWidth,
      lineHeight
    } = props;
    const left = title.offsetLeft + title.offsetWidth / 2;
    const lineStyle = {
      width: (0, import_utils.addUnit)(lineWidth),
      backgroundColor: props.color,
      transform: `translateX(${left}px) translateX(-50%)`
    };
    if (shouldAnimate) {
      lineStyle.transitionDuration = `${props.duration}ms`;
    }
    if ((0, import_utils.isDef)(lineHeight)) {
      const height = (0, import_utils.addUnit)(lineHeight);
      lineStyle.height = height;
      lineStyle.borderRadius = height;
    }
    setState({
      lineStyle
    });
  };
  const findAvailableTab = (index) => {
    const diff = index < state.currentIndex ? -1 : 1;
    while (index >= 0 && index < childrenList.length) {
      if (!childrenList[index].disabled) {
        return index;
      }
      index += diff;
    }
    return null;
  };
  const setCurrentIndex = (currentIndex) => {
    var _a;
    const newIndex = findAvailableTab(currentIndex);
    if (!(0, import_utils.isDef)(newIndex)) {
      return;
    }
    const newTab = childrenList[newIndex];
    const newName = getTabName(newTab, newIndex);
    const shouldEmitChange = state.currentIndex !== null;
    setState({
      currentIndex: newIndex
    });
    if (!initChange.current) {
      initChange.current = true;
      return;
    }
    if (initChange.current) {
      if (shouldEmitChange) {
        (_a = props.onChange) == null ? void 0 : _a.call(props, newName, newTab.title);
      }
    }
  };
  const setCurrentIndexByName = (name) => {
    const currentIndex = childrenList.findIndex((tab, index) => getTabName(tab, index) === name);
    setCurrentIndex(currentIndex < 0 ? 0 : currentIndex);
  };
  const scrollToCurrentContent = (immediate, current) => {
    if (props.scrollspy) {
      const target = contentRefs[current != null ? current : state.currentIndex];
      if (target && scroller) {
        const to = (0, import_utils.getElementTop)(target, scroller) - (offsetTopPx + tabHeightRef.current);
        lockScroll.current = true;
        (0, import_utils.scrollTopTo)(scroller, to, immediate ? 0 : +props.duration, () => {
          lockScroll.current = false;
        });
      }
    }
  };
  const onClickTab = (item, index, event) => {
    var _a;
    const {
      title,
      disabled = false
    } = item;
    const name = getTabName(item, index);
    (_a = props.onClickTab) == null ? void 0 : _a.call(props, {
      name,
      title,
      event,
      disabled
    });
    if (disabled)
      return;
    (0, import_interceptor.callInterceptor)({
      interceptor: props.beforeChange,
      args: [name],
      done: () => {
        if (index !== state.currentIndex) {
          setCurrentIndex(index);
          scrollToCurrentContent(false, index);
        }
      }
    });
  };
  const scrollTo = (name) => {
    setCurrentIndexByName(name);
    scrollToCurrentContent(true);
  };
  const getCurrentIndexOnScroll = () => {
    const scrollOffset = offsetTopPx + tabHeightRef.current;
    for (let index = 0; index < contentRefs.length; index++) {
      const $el = contentRefs[index];
      const top = (0, import_utils.getVisibleTop)($el);
      if (top > scrollOffset) {
        return index === 0 ? 0 : index - 1;
      }
    }
    return titleRefs.length - 1;
  };
  const onScroll = () => {
    if (props.scrollspy && !lockScroll.current) {
      let index = getCurrentIndexOnScroll();
      if (typeof props.scrollspy === "object") {
        if (props.scrollspy.autoFocusLast && (0, import_utils2.isReachBottom)(props.scrollspy.reachBottomThreshold)) {
          index = titleRefs.length - 1;
        }
      }
      if (index !== state.currentIndex) {
        setCurrentIndex(index);
      }
    }
  };
  const onStickyScroll = (params) => {
    var _a;
    stickyFixed.current = params.isFixed;
    (_a = props.onScroll) == null ? void 0 : _a.call(props, params);
  };
  const renderNav = () => {
    return childrenList.map((item, index) => {
      return /* @__PURE__ */ import_react.default.createElement(import_TabsTitle.default, {
        ref: setTitleRefs(index),
        key: item.key,
        dot: item.dot,
        type: props.type,
        badge: item.badge,
        title: item.title,
        color: props.color,
        style: item.titleStyle,
        className: item.titleClass,
        isActive: index === state.currentIndex,
        disabled: item.disabled,
        scrollable,
        renderTitle: item.renderTitle,
        activeColor: props.titleActiveColor,
        inactiveColor: props.titleInactiveColor,
        showZeroBadge: item.showZeroBadge,
        onClick: (event) => {
          onClickTab(item, index, event);
        }
      });
    });
  };
  const renderHeader = () => {
    const {
      type,
      border
    } = props;
    return /* @__PURE__ */ import_react.default.createElement("div", {
      ref: setWrapRef,
      className: (0, import_classnames.default)([bem("wrap", {
        scrollable
      }), {
        [import_constant.BORDER_TOP_BOTTOM]: type === "line" && border
      }])
    }, /* @__PURE__ */ import_react.default.createElement("div", {
      ref: navRef,
      role: "tablist",
      className: (0, import_classnames.default)(bem("nav", [type, {
        complete: scrollable
      }])),
      style: navStyle
    }, props.navRight, renderNav(), type === "line" && /* @__PURE__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(bem("line")),
      style: state.lineStyle
    }), props.navRight));
  };
  (0, import_hooks.useUpdateEffect)(() => {
    setLine();
  }, [props.color, windowSize.width]);
  (0, import_hooks.useUpdateEffect)(() => {
    if (props.active !== currentName) {
      setCurrentIndexByName(props.active);
    }
  }, [props.active]);
  (0, import_hooks.useUpdateEffect)(() => {
    if (state.inited) {
      setCurrentIndexByName(props.active || currentName);
      setLine();
      scrollIntoView(true);
    }
  }, [import_react.default.Children.count(children)]);
  (0, import_hooks.useUpdateEffect)(() => {
    scrollIntoView();
    setLine();
    if (stickyFixed.current && !props.scrollspy) {
      (0, import_utils.setRootScrollTop)(Math.ceil((0, import_utils.getElementTop)(root.current) - offsetTopPx));
    }
  }, [state.currentIndex]);
  const init = () => {
    setCurrentIndexByName(props.active);
    scrollIntoView(true);
    setState({
      inited: true
    });
  };
  (0, import_react.useEffect)(() => {
    init();
  }, []);
  (0, import_hooks.useUpdateEffect)(() => {
    if (popupContext.visible) {
      setLine();
    }
  }, [popupContext.visible]);
  (0, import_use_event_listener.default)("scroll", onScroll, {
    target: scroller,
    depends: [state.currentIndex]
  });
  (0, import_react.useImperativeHandle)(ref, () => ({
    resize: setLine,
    scrollTo
  }));
  const onStickyChange = () => {
    setLine(true);
  };
  return /* @__PURE__ */ import_react.default.createElement(import_TabsContext.default.Provider, {
    value: {
      props,
      currentName,
      scrollIntoView,
      setLine
    }
  }, /* @__PURE__ */ import_react.default.createElement("div", {
    ref: root,
    className: (0, import_classnames.default)(props.className, bem([props.type]))
  }, props.sticky ? /* @__PURE__ */ import_react.default.createElement(import_sticky.default, {
    container: root,
    offsetTop: offsetTopPx,
    onChange: onStickyChange,
    onScroll: onStickyScroll
  }, renderHeader(), props.navBottom) : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, renderHeader(), props.navBottom), /* @__PURE__ */ import_react.default.createElement(import_TabsContent.default, {
    count: childrenList.length,
    inited: state.inited,
    animated: props.animated,
    duration: props.duration,
    swipeable: props.swipeable,
    lazyRender: props.lazyRender,
    currentIndex: state.currentIndex,
    onChange: setCurrentIndex
  }, import_react.default.Children.toArray(children).filter(Boolean).map((node, index) => /* @__PURE__ */ import_react.default.cloneElement(node, {
    index,
    ref: setContentRefs(index)
  })))));
});
Tabs.defaultProps = {
  type: "line",
  duration: 300,
  swipeThreshold: 5,
  offsetTop: 0,
  ellipsis: true,
  lazyRender: true,
  active: 0
};
var stdin_default = Tabs;
