"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const types = require("../../utils/types.js");
const runtime = require("../../utils/vue/props/runtime.js");
const Vue = require("vue");
const shared_esmBundler = require("../../../../@vue/shared/dist/shared.esm-bundler.js");
const index = require("../../../node_modules/@vueuse/shared/index.js");
const _prop = runtime.buildProp({
  type: runtime.definePropType(Boolean),
  default: null
});
const _event = runtime.buildProp({ type: runtime.definePropType(Function) });
const createModelToggleComposable = (name) => {
  const updateEventKey = `update:${name}`;
  const updateEventKeyRaw = `onUpdate:${name}`;
  const useModelToggleEmits = [updateEventKey];
  const useModelToggleProps = {
    [name]: _prop,
    [updateEventKeyRaw]: _event
  };
  const useModelToggle = ({ indicator, toggleReason, shouldHideWhenRouteChanges, shouldProceed, onShow, onHide }) => {
    const instance = Vue.getCurrentInstance();
    const { emit } = instance;
    const props = instance.props;
    const hasUpdateHandler = Vue.computed(() => shared_esmBundler.isFunction(props[updateEventKeyRaw]));
    const isModelBindingAbsent = Vue.computed(() => props[name] === null);
    const doShow = (event) => {
      if (indicator.value === true) return;
      indicator.value = true;
      if (toggleReason) toggleReason.value = event;
      if (shared_esmBundler.isFunction(onShow)) onShow(event);
    };
    const doHide = (event) => {
      if (indicator.value === false) return;
      indicator.value = false;
      if (toggleReason) toggleReason.value = event;
      if (shared_esmBundler.isFunction(onHide)) onHide(event);
    };
    const show = (event) => {
      if (props.disabled === true || shared_esmBundler.isFunction(shouldProceed) && !shouldProceed()) return;
      const shouldEmit = hasUpdateHandler.value && index.isClient;
      if (shouldEmit) emit(updateEventKey, true);
      if (isModelBindingAbsent.value || !shouldEmit) doShow(event);
    };
    const hide = (event) => {
      if (props.disabled === true || !index.isClient) return;
      const shouldEmit = hasUpdateHandler.value && index.isClient;
      if (shouldEmit) emit(updateEventKey, false);
      if (isModelBindingAbsent.value || !shouldEmit) doHide(event);
    };
    const onChange = (val) => {
      if (!types.isBoolean(val)) return;
      if (props.disabled && val) {
        if (hasUpdateHandler.value) emit(updateEventKey, false);
      } else if (indicator.value !== val) if (val) doShow();
      else doHide();
    };
    const toggle = () => {
      if (indicator.value) hide();
      else show();
    };
    Vue.watch(() => props[name], onChange);
    if (shouldHideWhenRouteChanges && instance.appContext.config.globalProperties.$route !== void 0) Vue.watch(() => ({ ...instance.proxy.$route }), () => {
      if (shouldHideWhenRouteChanges.value && indicator.value) hide();
    });
    Vue.onMounted(() => {
      onChange(props[name]);
    });
    return {
      hide,
      show,
      toggle,
      hasUpdateHandler
    };
  };
  return {
    useModelToggle,
    useModelToggleProps,
    useModelToggleEmits
  };
};
exports.createModelToggleComposable = createModelToggleComposable;
