import React, { useMemo, useRef, useEffect, forwardRef, useImperativeHandle, useState, useContext } from "react";
import classnames from "classnames";
import useRefs from "../hooks/use-refs";
import useScrollParent from "../hooks/use-scroll-parent";
import useWindowSize from "../hooks/use-window-size";
import Sticky from "../sticky";
import TabsTitle from "./TabsTitle";
import TabsContent from "./TabsContent";
import TabsContext from "./TabsContext";
import { addUnit, parseChildList, isHidden, isDef, unitToPx, scrollLeftTo, getElementTop, getVisibleHeight, scrollTopTo, getVisibleTop, setRootScrollTop } from "../utils";
import { callInterceptor } from "../utils/interceptor";
import { BORDER_TOP_BOTTOM } from "../utils/constant";
import { useSetState, useUpdateEffect } from "../hooks";
import useEventListener from "../hooks/use-event-listener";
import { isReachBottom } from "./utils";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
import PopupContext from "../popup/PopupContext";
const Tabs = /* @__PURE__ */ forwardRef((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const popupContext = useContext(PopupContext);
  const [bem] = createNamespace("tabs", prefixCls);
  const {
    children,
    color,
    background
  } = props;
  const root = useRef(null);
  const [wrapRef, setWrapRef] = useState(null);
  const initChange = useRef(false);
  const lockScroll = useRef(false);
  const stickyFixed = useRef(false);
  const navRef = useRef(null);
  const scroller = useScrollParent(root);
  const windowSize = useWindowSize();
  const [titleRefs, setTitleRefs] = useRefs();
  const [contentRefs, setContentRefs] = useRefs();
  const tabHeight = useMemo(() => {
    if (!wrapRef)
      return 0;
    return getVisibleHeight(wrapRef);
  }, [wrapRef]);
  const tabHeightRef = useRef(tabHeight);
  tabHeightRef.current = tabHeight;
  const [state, setState] = useSetState({
    inited: false,
    position: "",
    currentIndex: -1,
    lineStyle: {
      backgroundColor: props.color
    }
  });
  const childrenList = useMemo(() => parseChildList(props.children), [props.children]);
  const scrollable = useMemo(() => childrenList.length > props.swipeThreshold || !props.ellipsis, [childrenList.length, props.swipeThreshold, props.ellipsis]);
  const navStyle = 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 = useMemo(() => {
    const activeTab = childrenList[state.currentIndex];
    return activeTab && getTabName(activeTab, state.currentIndex);
  }, [state.currentIndex]);
  const offsetTopPx = useMemo(() => 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;
    scrollLeftTo(nav, to, immediate ? 0 : +props.duration);
  };
  const setLine = (immediate) => {
    let shouldAnimate = state.inited;
    if (immediate)
      shouldAnimate = false;
    const titles = titleRefs;
    const hidden = 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: addUnit(lineWidth),
      backgroundColor: props.color,
      transform: `translateX(${left}px) translateX(-50%)`
    };
    if (shouldAnimate) {
      lineStyle.transitionDuration = `${props.duration}ms`;
    }
    if (isDef(lineHeight)) {
      const height = 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 (!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 = getElementTop(target, scroller) - (offsetTopPx + tabHeightRef.current);
        lockScroll.current = true;
        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;
    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 = 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 && 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__ */ React.createElement(TabsTitle, {
        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__ */ React.createElement("div", {
      ref: setWrapRef,
      className: classnames([bem("wrap", {
        scrollable
      }), {
        [BORDER_TOP_BOTTOM]: type === "line" && border
      }])
    }, /* @__PURE__ */ React.createElement("div", {
      ref: navRef,
      role: "tablist",
      className: classnames(bem("nav", [type, {
        complete: scrollable
      }])),
      style: navStyle
    }, props.navRight, renderNav(), type === "line" && /* @__PURE__ */ React.createElement("div", {
      className: classnames(bem("line")),
      style: state.lineStyle
    }), props.navRight));
  };
  useUpdateEffect(() => {
    setLine();
  }, [props.color, windowSize.width]);
  useUpdateEffect(() => {
    if (props.active !== currentName) {
      setCurrentIndexByName(props.active);
    }
  }, [props.active]);
  useUpdateEffect(() => {
    if (state.inited) {
      setCurrentIndexByName(props.active || currentName);
      setLine();
      scrollIntoView(true);
    }
  }, [React.Children.count(children)]);
  useUpdateEffect(() => {
    scrollIntoView();
    setLine();
    if (stickyFixed.current && !props.scrollspy) {
      setRootScrollTop(Math.ceil(getElementTop(root.current) - offsetTopPx));
    }
  }, [state.currentIndex]);
  const init = () => {
    setCurrentIndexByName(props.active);
    scrollIntoView(true);
    setState({
      inited: true
    });
  };
  useEffect(() => {
    init();
  }, []);
  useUpdateEffect(() => {
    if (popupContext.visible) {
      setLine();
    }
  }, [popupContext.visible]);
  useEventListener("scroll", onScroll, {
    target: scroller,
    depends: [state.currentIndex]
  });
  useImperativeHandle(ref, () => ({
    resize: setLine,
    scrollTo
  }));
  const onStickyChange = () => {
    setLine(true);
  };
  return /* @__PURE__ */ React.createElement(TabsContext.Provider, {
    value: {
      props,
      currentName,
      scrollIntoView,
      setLine
    }
  }, /* @__PURE__ */ React.createElement("div", {
    ref: root,
    className: classnames(props.className, bem([props.type]))
  }, props.sticky ? /* @__PURE__ */ React.createElement(Sticky, {
    container: root,
    offsetTop: offsetTopPx,
    onChange: onStickyChange,
    onScroll: onStickyScroll
  }, renderHeader(), props.navBottom) : /* @__PURE__ */ React.createElement(React.Fragment, null, renderHeader(), props.navBottom), /* @__PURE__ */ React.createElement(TabsContent, {
    count: childrenList.length,
    inited: state.inited,
    animated: props.animated,
    duration: props.duration,
    swipeable: props.swipeable,
    lazyRender: props.lazyRender,
    currentIndex: state.currentIndex,
    onChange: setCurrentIndex
  }, React.Children.toArray(children).filter(Boolean).map((node, index) => /* @__PURE__ */ React.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;
export {
  stdin_default as default
};
