"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const event = require("../../../constants/event.js");
const types = require("../../../utils/types.js");
const error = require("../../../utils/error.js");
const index = require("../../../hooks/use-namespace/index.js");
const constants = require("./constants.js");
const Vue = require("vue");
const castArray = require("../../../../../lodash-es/castArray.js");
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const SCOPE = "ElCollapse";
const useCollapse = (props, emit) => {
  const activeNames = Vue.ref(castArray.default(props.modelValue));
  const setActiveNames = (_activeNames) => {
    activeNames.value = _activeNames;
    const value = props.accordion ? activeNames.value[0] : activeNames.value;
    emit(event.UPDATE_MODEL_EVENT, value);
    emit(event.CHANGE_EVENT, value);
  };
  const handleChange = (name) => {
    if (props.accordion) setActiveNames([activeNames.value[0] === name ? "" : name]);
    else {
      const _activeNames = [...activeNames.value];
      const index2 = _activeNames.indexOf(name);
      if (index2 > -1) _activeNames.splice(index2, 1);
      else _activeNames.push(name);
      setActiveNames(_activeNames);
    }
  };
  const handleItemClick = async (name) => {
    const { beforeCollapse } = props;
    if (!beforeCollapse) {
      handleChange(name);
      return;
    }
    const shouldChange = beforeCollapse(name);
    if (![shared_esmBundler.isPromise(shouldChange), types.isBoolean(shouldChange)].includes(true)) error.throwError(SCOPE, "beforeCollapse must return type `Promise<boolean>` or `boolean`");
    if (shared_esmBundler.isPromise(shouldChange)) shouldChange.then((result) => {
      if (result !== false) handleChange(name);
    }).catch((e) => {
      error.debugWarn(SCOPE, `some error occurred: ${e}`);
    });
    else if (shouldChange) handleChange(name);
  };
  Vue.watch(() => props.modelValue, () => activeNames.value = castArray.default(props.modelValue), { deep: true });
  Vue.provide(constants.collapseContextKey, {
    activeNames,
    handleItemClick
  });
  return {
    activeNames,
    setActiveNames
  };
};
const useCollapseDOM = (props) => {
  const ns = index.useNamespace("collapse");
  return { rootKls: Vue.computed(() => [ns.b(), ns.b(`icon-position-${props.expandIconPosition}`)]) };
};
exports.useCollapse = useCollapse;
exports.useCollapseDOM = useCollapseDOM;
