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