'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var lodashEs = require('lodash-es');
require('../../../../hooks/index.cjs');
require('../../../../vue-components/index.cjs');
require('../../../../utils/index.cjs');
var useRaf = require('../hooks/useRaf.cjs');
var useOffsets = require('../hooks/useOffsets.cjs');
var TabContext = require('../TabContext.cjs');
var useTouchMove = require('../hooks/useTouchMove.cjs');
var useSyncState = require('../hooks/useSyncState.cjs');
var props = require('../props.cjs');
var TabNode = require('./TabNode.cjs');
var OperationNode = require('./OperationNode.cjs');
var AddButton = require('./AddButton.cjs');
var index = require('../../../../hooks/use-refs/index.cjs');
var index$1 = require('../../../../hooks/use-state/index.cjs');
var raf = require('../../../../utils/vue/raf.cjs');
var util = require('../../../../utils/vue/util.cjs');
var _class = require('../../../../utils/dom/class.cjs');
var index$2 = require('../../../../vue-components/vc-resize-observer/src/index.cjs');

const DEFAULT_SIZE = {
  width: 0,
  height: 0,
  left: 0,
  top: 0,
  right: 0
};
var TabNavList = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "TabNavList",
  inheritAttrs: false,
  props: props.tabNavListProps(),
  slots: Object,
  emits: ["tabClick", "tabScroll"],
  setup(props, {
    attrs,
    slots
  }) {
    const {
      tabs,
      prefixCls
    } = TabContext.useInjectTabs();
    const tabsWrapperRef = vue.shallowRef();
    const tabListRef = vue.shallowRef();
    const operationsRef = vue.shallowRef();
    const innerAddButtonRef = vue.shallowRef();
    const [setRef, btnRefs] = index.useRefs();
    const tabPositionTopOrBottom = vue.computed(() => props.tabPosition === "top" || props.tabPosition === "bottom");
    const [transformLeft, setTransformLeft] = useSyncState.default(0, (next, prev) => {
      if (tabPositionTopOrBottom.value && props.onTabScroll)
        props.onTabScroll({
          direction: next > prev ? "left" : "right"
        });
    });
    const [transformTop, setTransformTop] = useSyncState.default(0, (next, prev) => {
      if (!tabPositionTopOrBottom.value && props.onTabScroll)
        props.onTabScroll({
          direction: next > prev ? "top" : "bottom"
        });
    });
    const [wrapperScrollWidth, setWrapperScrollWidth] = index$1.useState(0);
    const [wrapperScrollHeight, setWrapperScrollHeight] = index$1.useState(0);
    const [wrapperWidth, setWrapperWidth] = index$1.useState(null);
    const [wrapperHeight, setWrapperHeight] = index$1.useState(null);
    const [addWidth, setAddWidth] = index$1.useState(0);
    const [addHeight, setAddHeight] = index$1.useState(0);
    const [tabSizes, setTabSizes] = useRaf.useRafState(/* @__PURE__ */ new Map());
    const tabOffsets = useOffsets.default(tabs, tabSizes);
    const operationsHiddenClassName = vue.computed(() => `${prefixCls.value}-nav-operations-hidden`);
    const transformMin = vue.shallowRef(0);
    const transformMax = vue.shallowRef(0);
    vue.watchEffect(() => {
      if (!tabPositionTopOrBottom.value) {
        transformMin.value = Math.min(0, wrapperHeight.value - wrapperScrollHeight.value);
        transformMax.value = 0;
      } else if (props.rtl) {
        transformMin.value = 0;
        transformMax.value = Math.max(0, wrapperScrollWidth.value - wrapperWidth.value);
      } else {
        transformMin.value = Math.min(0, wrapperWidth.value - wrapperScrollWidth.value);
        transformMax.value = 0;
      }
    });
    const alignInRange = (value) => {
      if (value < transformMin.value)
        return transformMin.value;
      if (value > transformMax.value)
        return transformMax.value;
      return value;
    };
    const touchMovingRef = vue.shallowRef();
    const [lockAnimation, setLockAnimation] = index$1.useState();
    const doLockAnimation = () => {
      setLockAnimation(Date.now());
    };
    const clearTouchMoving = () => {
      clearTimeout(touchMovingRef.value);
    };
    const doMove = (setState, offset) => {
      setState((value) => {
        const newValue = alignInRange(value + offset);
        return newValue;
      });
    };
    useTouchMove.default(tabsWrapperRef, (offsetX, offsetY) => {
      if (tabPositionTopOrBottom.value) {
        if (wrapperWidth.value >= wrapperScrollWidth.value)
          return false;
        doMove(setTransformLeft, offsetX);
      } else {
        if (wrapperHeight.value >= wrapperScrollHeight.value)
          return false;
        doMove(setTransformTop, offsetY);
      }
      clearTouchMoving();
      doLockAnimation();
      return true;
    });
    vue.watch(lockAnimation, () => {
      clearTouchMoving();
      if (lockAnimation.value) {
        touchMovingRef.value = setTimeout(() => {
          setLockAnimation(0);
        }, 100);
      }
    });
    const scrollToTab = (key = props.activeKey) => {
      const tabOffset = tabOffsets.value.get(key) || {
        width: 0,
        height: 0,
        left: 0,
        right: 0,
        top: 0
      };
      if (tabPositionTopOrBottom.value) {
        let newTransform = transformLeft.value;
        if (props.rtl) {
          if (tabOffset.right < transformLeft.value)
            newTransform = tabOffset.right;
          else if (tabOffset.right + tabOffset.width > transformLeft.value + wrapperWidth.value)
            newTransform = tabOffset.right + tabOffset.width - wrapperWidth.value;
        } else if (tabOffset.left < -transformLeft.value) {
          newTransform = -tabOffset.left;
        } else if (tabOffset.left + tabOffset.width > -transformLeft.value + wrapperWidth.value) {
          newTransform = -(tabOffset.left + tabOffset.width - wrapperWidth.value);
        }
        setTransformTop(0);
        setTransformLeft(alignInRange(newTransform));
      } else {
        let newTransform = transformTop.value;
        if (tabOffset.top < -transformTop.value)
          newTransform = -tabOffset.top;
        else if (tabOffset.top + tabOffset.height > -transformTop.value + wrapperHeight.value)
          newTransform = -(tabOffset.top + tabOffset.height - wrapperHeight.value);
        setTransformLeft(0);
        setTransformTop(alignInRange(newTransform));
      }
    };
    const visibleStart = vue.shallowRef(0);
    const visibleEnd = vue.shallowRef(0);
    vue.watchEffect(() => {
      let unit;
      let position;
      let transformSize;
      let basicSize;
      let tabContentSize;
      let addSize;
      const tabOffsetsValue = tabOffsets.value;
      if (["top", "bottom"].includes(props.tabPosition)) {
        unit = "width";
        basicSize = wrapperWidth.value;
        tabContentSize = wrapperScrollWidth.value;
        addSize = addWidth.value;
        position = props.rtl ? "right" : "left";
        transformSize = Math.abs(transformLeft.value);
      } else {
        unit = "height";
        basicSize = wrapperHeight.value;
        tabContentSize = wrapperScrollWidth.value;
        addSize = addHeight.value;
        position = "top";
        transformSize = -transformTop.value;
      }
      let mergedBasicSize = basicSize;
      if (tabContentSize + addSize > basicSize && tabContentSize < basicSize)
        mergedBasicSize = basicSize - addSize;
      const tabsVal = tabs.value;
      if (!tabsVal.length)
        return [visibleStart.value, visibleEnd.value] = [0, 0];
      const len = tabsVal.length;
      let endIndex = len;
      for (let i = 0; i < len; i += 1) {
        const offset = tabOffsetsValue.get(tabsVal[i].key) || DEFAULT_SIZE;
        if (offset[position] + offset[unit] > transformSize + mergedBasicSize) {
          endIndex = i - 1;
          break;
        }
      }
      let startIndex = 0;
      for (let i = len - 1; i >= 0; i -= 1) {
        const offset = tabOffsetsValue.get(tabsVal[i].key) || DEFAULT_SIZE;
        if (offset[position] < transformSize) {
          startIndex = i + 1;
          break;
        }
      }
      return [visibleStart.value, visibleEnd.value] = [startIndex, endIndex];
    });
    const onListHolderResize = () => {
      var _a, _b, _c, _d, _e;
      const offsetWidth = ((_a = tabsWrapperRef.value) == null ? void 0 : _a.offsetWidth) || 0;
      const offsetHeight = ((_b = tabsWrapperRef.value) == null ? void 0 : _b.offsetHeight) || 0;
      const addDom = ((_c = innerAddButtonRef.value) == null ? void 0 : _c.$el) || {};
      const newAddWidth = addDom.offsetWidth || 0;
      const newAddHeight = addDom.offsetHeight || 0;
      setWrapperWidth(offsetWidth);
      setWrapperHeight(offsetHeight);
      setAddWidth(newAddWidth);
      setAddHeight(newAddHeight);
      const newWrapperScrollWidth = (((_d = tabListRef.value) == null ? void 0 : _d.offsetWidth) || 0) - newAddWidth;
      const newWrapperScrollHeight = (((_e = tabListRef.value) == null ? void 0 : _e.offsetHeight) || 0) - newAddHeight;
      setWrapperScrollWidth(newWrapperScrollWidth);
      setWrapperScrollHeight(newWrapperScrollHeight);
      setTabSizes(() => {
        const newSizes = /* @__PURE__ */ new Map();
        tabs.value.forEach(({
          key
        }) => {
          const btnRef = btnRefs.value.get(key);
          const btnNode = (btnRef == null ? void 0 : btnRef.$el) || btnRef;
          if (btnNode) {
            newSizes.set(key, {
              width: btnNode.offsetWidth,
              height: btnNode.offsetHeight,
              left: btnNode.offsetLeft,
              top: btnNode.offsetTop
            });
          }
        });
        return newSizes;
      });
    };
    const hiddenTabs = vue.computed(() => [...tabs.value.slice(0, visibleStart.value), ...tabs.value.slice(visibleEnd.value + 1)]);
    const [inkStyle, setInkStyle] = index$1.useState();
    const activeTabOffset = vue.computed(() => tabOffsets.value.get(props.activeKey));
    const inkBarRafRef = vue.shallowRef();
    const cleanInkBarRaf = () => {
      raf.raf.cancel(inkBarRafRef.value);
    };
    vue.watch([activeTabOffset, tabPositionTopOrBottom, () => props.rtl], () => {
      const newInkStyle = {};
      if (activeTabOffset.value) {
        if (tabPositionTopOrBottom.value) {
          if (props.rtl)
            newInkStyle.right = util.toPx(activeTabOffset.value.right);
          else
            newInkStyle.left = util.toPx(activeTabOffset.value.left);
          newInkStyle.width = util.toPx(activeTabOffset.value.width);
        } else {
          newInkStyle.top = util.toPx(activeTabOffset.value.top);
          newInkStyle.height = util.toPx(activeTabOffset.value.height);
        }
      }
      cleanInkBarRaf();
      inkBarRafRef.value = raf.raf(() => {
        setInkStyle(newInkStyle);
      });
    });
    vue.watch([() => props.activeKey, activeTabOffset, tabOffsets, tabPositionTopOrBottom], () => {
      scrollToTab();
    }, {
      flush: "post"
    });
    vue.watch([() => props.rtl, () => props.tabBarGutter, () => props.activeKey, () => tabs.value], () => {
      onListHolderResize();
    }, {
      flush: "post"
    });
    const ExtraContent = ({
      position,
      prefixCls: prefixCls2,
      extra
    }) => {
      if (!extra)
        return null;
      const content = extra == null ? void 0 : extra({
        position
      });
      return content ? vue.createVNode("div", {
        "class": `${prefixCls2}-extra-content`
      }, [content]) : null;
    };
    vue.onBeforeUnmount(() => {
      clearTouchMoving();
      cleanInkBarRaf();
    });
    return () => {
      const {
        id,
        animated,
        activeKey,
        rtl,
        editable,
        locale,
        tabPosition,
        tabBarGutter,
        onTabClick
      } = props;
      const {
        class: className,
        style
      } = attrs;
      const pre = prefixCls.value;
      const hasDropdown = !!hiddenTabs.value.length;
      const wrapPrefix = `${pre}-nav-wrap`;
      let pingLeft;
      let pingRight;
      let pingTop;
      let pingBottom;
      if (tabPositionTopOrBottom.value) {
        if (rtl) {
          pingRight = transformLeft.value > 0;
          pingLeft = transformLeft.value + wrapperWidth.value < wrapperScrollWidth.value;
        } else {
          pingLeft = transformLeft.value < 0;
          pingRight = -transformLeft.value + wrapperWidth.value < wrapperScrollWidth.value;
        }
      } else {
        pingTop = transformTop.value < 0;
        pingBottom = -transformTop.value + wrapperHeight.value < wrapperScrollHeight.value;
      }
      const tabNodeStyle = {};
      if (tabPosition === "top" || tabPosition === "bottom") {
        tabNodeStyle[rtl ? "marginRight" : "marginLeft"] = typeof tabBarGutter === "number" ? `${tabBarGutter}px` : tabBarGutter;
      } else {
        tabNodeStyle.marginTop = typeof tabBarGutter === "number" ? `${tabBarGutter}px` : tabBarGutter;
      }
      const tabNodes = tabs.value.map((tab, i) => {
        const {
          key
        } = tab;
        return vue.createVNode(TabNode.default, {
          "id": id,
          "prefixCls": pre,
          "key": key,
          "tab": tab,
          "style": i === 0 ? void 0 : tabNodeStyle,
          "closable": tab.closable,
          "editable": editable,
          "active": key === activeKey,
          "removeAriaLabel": locale == null ? void 0 : locale.removeAriaLabel,
          "ref": setRef(key),
          "onClick": (e) => {
            onTabClick(key, e);
          },
          "onFocus": () => {
            scrollToTab(key);
            doLockAnimation();
            if (!tabsWrapperRef.value)
              return;
            if (!rtl)
              tabsWrapperRef.value.scrollLeft = 0;
            tabsWrapperRef.value.scrollTop = 0;
          }
        }, slots);
      });
      return vue.createVNode("div", {
        "role": "tablist",
        "class": _class.classNames(`${pre}-nav`, className),
        "style": style,
        "onKeydown": () => {
          doLockAnimation();
        }
      }, [vue.createVNode(ExtraContent, {
        "position": "left",
        "prefixCls": pre,
        "extra": slots.leftExtra
      }, null), vue.createVNode(index$2.default, {
        "onResize": onListHolderResize
      }, {
        default: () => [vue.createVNode("div", {
          "class": _class.classNames(wrapPrefix, {
            [`${wrapPrefix}-ping-left`]: pingLeft,
            [`${wrapPrefix}-ping-right`]: pingRight,
            [`${wrapPrefix}-ping-top`]: pingTop,
            [`${wrapPrefix}-ping-bottom`]: pingBottom
          }),
          "ref": tabsWrapperRef
        }, [vue.createVNode(index$2.default, {
          "onResize": onListHolderResize
        }, {
          default: () => [vue.createVNode("div", {
            "ref": tabListRef,
            "class": `${pre}-nav-list`,
            "style": {
              transform: `translate(${transformLeft.value}px, ${transformTop.value}px)`,
              transition: lockAnimation.value ? "none" : void 0
            }
          }, [tabNodes, vue.createVNode(AddButton.default, {
            "ref": innerAddButtonRef,
            "prefixCls": pre,
            "locale": locale,
            "editable": editable,
            "style": {
              ...tabNodes.length === 0 ? void 0 : tabNodeStyle,
              visibility: hasDropdown ? "hidden" : null
            }
          }, null), vue.createVNode("div", {
            "class": _class.classNames(`${pre}-ink-bar`, {
              [`${pre}-ink-bar-animated`]: animated.inkBar
            }),
            "style": inkStyle.value
          }, null)])]
        })])]
      }), vue.createVNode(OperationNode.default, {
        ...props,
        "removeAriaLabel": locale == null ? void 0 : locale.removeAriaLabel,
        "ref": operationsRef,
        "prefixCls": pre,
        "tabs": hiddenTabs.value,
        "class": !hasDropdown && operationsHiddenClassName.value
      }, lodashEs.pick(slots, ["moreIcon"])), vue.createVNode(ExtraContent, {
        "position": "right",
        "prefixCls": pre,
        "extra": slots.rightExtra
      }, null), vue.createVNode(ExtraContent, {
        "position": "right",
        "prefixCls": pre,
        "extra": slots.tabBarExtraContent
      }, null)]);
    };
  }
});

exports.default = TabNavList;
//# sourceMappingURL=index.cjs.map
