'use strict';

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

var vue = require('vue');
var CloseOutlined = require('@ant-design/icons-vue');
var pick = require('lodash-es/pick');
var index$2 = require('../style/index.js');
var index$1 = require('../../_util/props-util/index.js');
var useConfigInject = require('../../config-provider/hooks/useConfigInject.js');
require('../../hooks/index.js');
var isMobile = require('../../vc-util/isMobile.js');
var classNames = require('../../_util/classNames.js');
var devWarning = require('../../vc-util/devWarning.js');
var type = require('../../_util/type.js');
var index = require('../../_util/vue-types/index.js');
var omit = require('../../_util/omit.js');
var index$3 = require('./TabNavList/index.js');
var index$4 = require('./TabPanelList/index.js');
var TabContext = require('./TabContext.js');
var util = require('../../_util/util.js');
var initDefaultProps = require('../../_util/props-util/initDefaultProps.js');
var useState = require('../../hooks/useState.js');
var useMergedState = require('../../hooks/useMergedState.js');

let uuid = 0;
const tabsProps = () => {
  return {
    "prefixCls": {
      type: String
    },
    "id": {
      type: String
    },
    "popupClassName": String,
    "getPopupContainer": type.functionType(),
    "activeKey": {
      type: [String, Number]
    },
    "defaultActiveKey": {
      type: [String, Number]
    },
    "direction": type.stringType(),
    "animated": type.someType([Boolean, Object]),
    "renderTabBar": type.functionType(),
    "tabBarGutter": {
      type: Number
    },
    "tabBarStyle": type.objectType(),
    "tabPosition": type.stringType(),
    "destroyInactiveTabPane": type.booleanType(),
    "hideAdd": Boolean,
    "type": type.stringType(),
    "size": type.stringType(),
    "centered": Boolean,
    "onEdit": type.functionType(),
    "onChange": type.functionType(),
    "onTabClick": type.functionType(),
    "onTabScroll": type.functionType(),
    "onUpdate:activeKey": type.functionType(),
    // Accessibility
    "locale": type.objectType(),
    "onPrevClick": type.functionType(),
    "onNextClick": type.functionType(),
    "tabBarExtraContent": index.default.any
  };
};
function parseTabList(children) {
  return children.map((node) => {
    if (index$1.isValidElement(node)) {
      const props = {
        ...node.props || {}
      };
      for (const [k, v] of Object.entries(props)) {
        delete props[k];
        props[util.camelize(k)] = v;
      }
      const slots = node.children || {};
      const key = node.key !== void 0 ? node.key : void 0;
      const {
        tab = slots.tab,
        disabled,
        forceRender,
        closable,
        animated,
        active,
        destroyInactiveTabPane
      } = props;
      return {
        key,
        ...props,
        node,
        closeIcon: slots.closeIcon,
        tab,
        disabled: disabled === "" || disabled,
        forceRender: forceRender === "" || forceRender,
        closable: closable === "" || closable,
        animated: animated === "" || animated,
        active: active === "" || active,
        destroyInactiveTabPane: destroyInactiveTabPane === "" || destroyInactiveTabPane
      };
    }
    return null;
  }).filter((tab) => tab);
}
const InternalTabs = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "InternalTabs",
  inheritAttrs: false,
  props: {
    ...initDefaultProps.default(tabsProps(), {
      tabPosition: "top",
      animated: {
        inkBar: true,
        tabPane: false
      }
    }),
    tabs: type.arrayType()
  },
  slots: Object,
  // emits: ['tabClick', 'tabScroll', 'change', 'update:activeKey'],
  setup(props, {
    attrs,
    slots
  }) {
    devWarning.default(!(props.onPrevClick !== void 0) && !(props.onNextClick !== void 0), "Tabs", "`onPrevClick / @prevClick` and `onNextClick / @nextClick` has been removed. Please use `onTabScroll / @tabScroll` instead.");
    devWarning.default(!(props.tabBarExtraContent !== void 0), "Tabs", "`tabBarExtraContent` prop has been removed. Please use `rightExtra` slot instead.");
    devWarning.default(!(slots.tabBarExtraContent !== void 0), "Tabs", "`tabBarExtraContent` slot is deprecated. Please use `rightExtra` slot instead.");
    const {
      prefixCls,
      direction,
      size,
      rootPrefixCls,
      getPopupContainer
    } = useConfigInject.default("tabs", props);
    const [wrapSSR, hashId] = index$2.default(prefixCls);
    const rtl = vue.computed(() => direction.value === "rtl");
    const mergedAnimated = vue.computed(() => {
      const {
        animated,
        tabPosition
      } = props;
      if (animated === false || ["left", "right"].includes(tabPosition)) {
        return {
          inkBar: false,
          tabPane: false
        };
      } else if (animated === true) {
        return {
          inkBar: true,
          tabPane: true
        };
      } else {
        return {
          inkBar: true,
          tabPane: false,
          ...typeof animated === "object" ? animated : {}
        };
      }
    });
    const [mobile, setMobile] = useState.useState(false);
    vue.onMounted(() => {
      setMobile(isMobile.default());
    });
    const [mergedActiveKey, setMergedActiveKey] = useMergedState.useMergedState(() => {
      var _a;
      return (_a = props.tabs[0]) == null ? void 0 : _a.key;
    }, {
      value: vue.computed(() => props.activeKey),
      defaultValue: props.defaultActiveKey
    });
    const [activeIndex, setActiveIndex] = useState.useState(() => props.tabs.findIndex((tab) => tab.key === mergedActiveKey.value));
    vue.watchEffect(() => {
      var _a;
      let newActiveIndex = props.tabs.findIndex((tab) => tab.key === mergedActiveKey.value);
      if (newActiveIndex === -1) {
        newActiveIndex = Math.max(0, Math.min(activeIndex.value, props.tabs.length - 1));
        setMergedActiveKey((_a = props.tabs[newActiveIndex]) == null ? void 0 : _a.key);
      }
      setActiveIndex(newActiveIndex);
    });
    const [mergedId, setMergedId] = useMergedState.useMergedState(null, {
      value: vue.computed(() => props.id)
    });
    const mergedTabPosition = vue.computed(() => {
      if (mobile.value && !["left", "right"].includes(props.tabPosition))
        return "top";
      else
        return props.tabPosition;
    });
    vue.onMounted(() => {
      if (!props.id) {
        setMergedId(`rc-tabs-${process.env.NODE_ENV === "test" ? "test" : uuid}`);
        uuid += 1;
      }
    });
    const onInternalTabClick = (key, e) => {
      var _a, _b;
      (_a = props.onTabClick) == null ? void 0 : _a.call(props, key, e);
      const isActiveChanged = key !== mergedActiveKey.value;
      setMergedActiveKey(key);
      if (isActiveChanged)
        (_b = props.onChange) == null ? void 0 : _b.call(props, key);
    };
    TabContext.useProvideTabs({
      tabs: vue.computed(() => props.tabs),
      prefixCls
    });
    return () => {
      const {
        id,
        type,
        tabBarGutter,
        tabBarStyle,
        locale,
        destroyInactiveTabPane,
        renderTabBar = slots.renderTabBar,
        onTabScroll,
        hideAdd,
        centered
      } = props;
      const sharedProps = {
        id: mergedId.value,
        activeKey: mergedActiveKey.value,
        animated: mergedAnimated.value,
        tabPosition: mergedTabPosition.value,
        rtl: rtl.value,
        mobile: mobile.value
      };
      let editable;
      if (type === "editable-card") {
        editable = {
          onEdit: (editType, {
            key,
            event
          }) => {
            var _a;
            (_a = props.onEdit) == null ? void 0 : _a.call(props, editType === "add" ? event : key, editType);
          },
          removeIcon: () => vue.createVNode(CloseOutlined.CloseOutlined, null, null),
          addIcon: slots.addIcon ? slots.addIcon : () => vue.createVNode(CloseOutlined.PlusOutlined, null, null),
          showAdd: hideAdd !== true
        };
      }
      let tabNavBar;
      const tabNavBarProps = {
        ...sharedProps,
        moreTransitionName: `${rootPrefixCls.value}-slide-up`,
        editable,
        locale,
        tabBarGutter,
        onTabClick: onInternalTabClick,
        onTabScroll,
        style: tabBarStyle,
        getPopupContainer: getPopupContainer.value,
        popupClassName: classNames.default(props.popupClassName, hashId.value)
      };
      if (renderTabBar) {
        tabNavBar = renderTabBar({
          ...tabNavBarProps,
          DefaultTabBar: index$3.default
        });
      } else {
        tabNavBar = vue.createVNode(index$3.default, tabNavBarProps, pick(slots, ["moreIcon", "leftExtra", "rightExtra", "tabBarExtraContent"]));
      }
      const pre = prefixCls.value;
      return wrapSSR(vue.createVNode("div", vue.mergeProps(attrs, {
        "id": id,
        "class": classNames.default(pre, `${pre}-${mergedTabPosition.value}`, {
          [hashId.value]: true,
          [`${pre}-${size.value}`]: size.value,
          [`${pre}-card`]: ["card", "editable-card"].includes(type),
          [`${pre}-editable-card`]: type === "editable-card",
          [`${pre}-centered`]: centered,
          [`${pre}-mobile`]: mobile.value,
          [`${pre}-editable`]: type === "editable-card",
          [`${pre}-rtl`]: rtl.value
        }, attrs.class)
      }), [tabNavBar, vue.createVNode(index$4.default, vue.mergeProps({
        "destroyInactiveTabPane": destroyInactiveTabPane
      }, sharedProps, {
        "animated": mergedAnimated.value
      }), null)]));
    };
  }
});
var Tabs = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ATabs",
  inheritAttrs: false,
  props: initDefaultProps.default(tabsProps(), {
    tabPosition: "top",
    animated: {
      inkBar: true,
      tabPane: false
    }
  }),
  slots: Object,
  // emits: ['tabClick', 'tabScroll', 'change', 'update:activeKey'],
  setup(props, {
    attrs,
    slots,
    emit
  }) {
    const handleChange = (key) => {
      emit("update:activeKey", key);
      emit("change", key);
    };
    return () => {
      var _a;
      const tabs = parseTabList(index$1.flattenChildren((_a = slots.default) == null ? void 0 : _a.call(slots)));
      return vue.createVNode(InternalTabs, vue.mergeProps(omit.default(props, ["onUpdate:activeKey", "onChange"]), attrs, {
        "onChange": handleChange,
        "tabs": tabs
      }), slots);
    };
  }
});

exports.default = Tabs;
exports.tabsProps = tabsProps;
//# sourceMappingURL=Tabs.js.map
