import { defineComponent, ref, watch, computed, createVNode, mergeProps } from 'vue';
import { RightOutlined } from '@ant-design/icons-vue';
import { getDataAndAriaProps } from '../_util/util.mjs';
import { cloneElement } from '../_util/vnode.mjs';
import { isValidElement, isEmptyElement, flattenChildren } from '../_util/props-util/index.mjs';
import classNames from '../_util/classNames.mjs';
import '../hooks/index.mjs';
import collapseMotion from '../_util/components/collapseMotion.mjs';
import firstNotUndefined from '../_util/firstNotUndefined.mjs';
import useStyle from './style/index.mjs';
import { collapseProps } from './commonProps.mjs';
import initDefaultProps from '../_util/props-util/initDefaultProps.mjs';
import useConfigInject from '../config-provider/hooks/useConfigInject.mjs';

function getActiveKeysArray(activeKey) {
  let currentActiveKey = activeKey;
  if (!Array.isArray(currentActiveKey)) {
    const activeKeyType = typeof currentActiveKey;
    currentActiveKey = activeKeyType === "number" || activeKeyType === "string" ? [currentActiveKey] : [];
  }
  return currentActiveKey.map((key) => String(key));
}
var Collapse = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ACollapse",
  inheritAttrs: false,
  props: initDefaultProps(collapseProps(), {
    accordion: false,
    destroyInactivePanel: false,
    bordered: true,
    openAnimation: collapseMotion("ant-motion-collapse", false),
    expandIconPosition: "start"
  }),
  slots: Object,
  setup(props, {
    attrs,
    slots,
    emit
  }) {
    const stateActiveKey = ref(getActiveKeysArray(firstNotUndefined([props.activeKey, props.defaultActiveKey])));
    watch(() => props.activeKey, () => {
      stateActiveKey.value = getActiveKeysArray(props.activeKey);
    }, {
      deep: true
    });
    const {
      prefixCls,
      direction
    } = useConfigInject("collapse", props);
    const [wrapSSR, hashId] = useStyle(prefixCls);
    const iconPosition = computed(() => {
      const {
        expandIconPosition
      } = props;
      if (expandIconPosition !== void 0)
        return expandIconPosition;
      return direction.value === "rtl" ? "end" : "start";
    });
    const setActiveKey = (activeKey) => {
      if (props.activeKey === void 0)
        stateActiveKey.value = activeKey;
      const newKey = props.accordion ? activeKey[0] : activeKey;
      emit("update:activeKey", newKey);
      emit("change", newKey);
    };
    const onClickItem = (key) => {
      let activeKey = stateActiveKey.value;
      if (props.accordion) {
        activeKey = activeKey[0] === key ? [] : [key];
      } else {
        activeKey = [...activeKey];
        const index = activeKey.indexOf(key);
        const isActive = index > -1;
        if (isActive) {
          activeKey.splice(index, 1);
        } else {
          activeKey.push(key);
        }
      }
      setActiveKey(activeKey);
    };
    const renderExpandIcon = (panelProps) => {
      const {
        expandIcon = slots.expandIcon
      } = props;
      const icon = expandIcon ? expandIcon(panelProps) : createVNode(RightOutlined, {
        "rotate": panelProps.isActive ? 90 : void 0
      }, null);
      return createVNode("div", {
        "class": [`${prefixCls.value}-expand-icon`, hashId.value],
        "onClick": () => ["header", "icon"].includes(props.collapsible) && onClickItem(panelProps.panelKey)
      }, [isValidElement(Array.isArray(expandIcon) ? icon[0] : icon) ? cloneElement(icon, {
        class: `${prefixCls.value}-arrow`
      }, false) : icon]);
    };
    const getNewChild = (child, index) => {
      var _a, _b, _c;
      if (isEmptyElement(child))
        return;
      const activeKey = stateActiveKey.value;
      const {
        accordion,
        destroyInactivePanel,
        collapsible,
        openAnimation
      } = props;
      const key = String((_a = child.key) != null ? _a : index);
      const {
        header = (_c = (_b = child.children) == null ? void 0 : _b.header) == null ? void 0 : _c.call(_b),
        headerClass,
        collapsible: childCollapsible,
        disabled
      } = child.props || {};
      let isActive = false;
      if (accordion)
        isActive = activeKey[0] === key;
      else
        isActive = activeKey.includes(key);
      let mergeCollapsible = childCollapsible != null ? childCollapsible : collapsible;
      if (disabled || disabled === "")
        mergeCollapsible = "disabled";
      const newProps = {
        key,
        panelKey: key,
        header,
        headerClass,
        isActive,
        prefixCls: prefixCls.value,
        destroyInactivePanel,
        openAnimation,
        accordion,
        onItemClick: mergeCollapsible === "disabled" ? null : onClickItem,
        expandIcon: renderExpandIcon,
        collapsible: mergeCollapsible
      };
      return cloneElement(child, newProps);
    };
    const getItems = () => {
      var _a;
      return flattenChildren((_a = slots.default) == null ? void 0 : _a.call(slots)).map(getNewChild);
    };
    return () => {
      const {
        accordion,
        bordered,
        ghost
      } = props;
      const collapseClassName = classNames(prefixCls.value, {
        [`${prefixCls.value}-borderless`]: !bordered,
        [`${prefixCls.value}-icon-position-${iconPosition.value}`]: true,
        [`${prefixCls.value}-rtl`]: direction.value === "rtl",
        [`${prefixCls.value}-ghost`]: !!ghost,
        [attrs.class]: !!attrs.class
      }, hashId.value);
      return wrapSSR(createVNode("div", mergeProps({
        "class": collapseClassName
      }, getDataAndAriaProps(attrs), {
        "style": attrs.style,
        "role": accordion ? "tablist" : null
      }), [getItems()]));
    };
  }
});

export { collapseProps, Collapse as default };
//# sourceMappingURL=Collapse.mjs.map
