'use strict';

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

var vue = require('vue');
var iconsVue = require('@ant-design/icons-vue');
require('../../../utils/index.cjs');
require('../../../vue-components/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var index = require('../style/index.cjs');
var props = require('./props.cjs');
var initDefaultProps = require('../../../utils/vue/initDefaultProps.cjs');
var firstNotUndefined = require('../../../utils/lang/firstNotUndefined.cjs');
var is = require('../../../utils/is.cjs');
var vnode = require('../../../utils/vue/vnode.cjs');
var collapseMotion = require('../../../vue-components/transition/src/collapseMotion.cjs');
var props$1 = require('../../../utils/vue/props.cjs');
var _class = require('../../../utils/dom/class.cjs');
var util = require('../../../utils/vue/util.cjs');

var ACollapse = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ACollapse",
  inheritAttrs: false,
  props: initDefaultProps.initDefaultProps(props.collapseProps(), {
    accordion: false,
    destroyInactivePanel: false,
    bordered: true,
    expandIconPosition: "start"
  }),
  slots: Object,
  setup(props, {
    attrs,
    slots,
    emit
  }) {
    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));
    }
    const stateActiveKey = vue.ref(getActiveKeysArray(firstNotUndefined.firstNotUndefined([props.activeKey, props.defaultActiveKey])));
    vue.watch(() => props.activeKey, () => {
      stateActiveKey.value = getActiveKeysArray(props.activeKey);
    }, {
      deep: true
    });
    const {
      prefixCls,
      direction,
      rootPrefixCls
    } = useConfigInject.default("collapse", props);
    const [wrapSSR, hashId] = index.default(prefixCls);
    const iconPosition = vue.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) : vue.createVNode(iconsVue.RightOutlined, {
        "rotate": panelProps.isActive ? 90 : void 0
      }, null);
      return vue.createVNode("div", {
        "class": [`${prefixCls.value}-expand-icon`, hashId.value],
        "onClick": () => ["header", "icon"].includes(props.collapsible) && onClickItem(panelProps.panelKey)
      }, [is.isValidElement(Array.isArray(expandIcon) ? icon[0] : icon) ? vnode.cloneElement(icon, {
        class: `${prefixCls.value}-arrow`
      }, false) : icon]);
    };
    const getNewChild = (child, index) => {
      var _a, _b, _c;
      if (is.isEmptyElement(child))
        return;
      const activeKey = stateActiveKey.value;
      const {
        accordion,
        destroyInactivePanel,
        collapsible,
        openAnimation
      } = props;
      const animation = openAnimation || collapseMotion.collapseMotion(`${rootPrefixCls.value}-motion-collapse`);
      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: animation,
        accordion,
        onItemClick: mergeCollapsible === "disabled" ? null : onClickItem,
        expandIcon: renderExpandIcon,
        collapsible: mergeCollapsible
      };
      return vnode.cloneElement(child, newProps);
    };
    const getItems = () => {
      var _a;
      return props$1.flattenChildren((_a = slots.default) == null ? void 0 : _a.call(slots)).map(getNewChild);
    };
    return () => {
      const {
        accordion,
        bordered,
        ghost
      } = props;
      const collapseClassName = _class.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(vue.createVNode("div", {
        "class": collapseClassName,
        ...util.getDataAndAriaProps(attrs),
        "style": attrs.style,
        "role": accordion ? "tablist" : null
      }, [getItems()]));
    };
  }
});

exports.default = ACollapse;
//# sourceMappingURL=Collapse.cjs.map
