import { defineNuxtPlugin } from "nuxt/app";
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.directive("default", {
    beforeMount(el, binding) {
      const def = binding.value;
      if (def === void 0 || def === null || def === "") return;
      const apply = (value) => {
        if ("value" in el) {
          ;
          el.value = value;
        }
        el.dispatchEvent(new Event("input", { bubbles: true }));
        el.dispatchEvent(new Event("change", { bubbles: true }));
      };
      apply(def);
      const inner = el.querySelector?.("input, textarea, select");
      if (inner) {
        const onMountedSync = () => {
          const cur = inner.value;
          if (cur === "" || cur == null) {
            inner.value = String(def);
            inner.dispatchEvent(new Event("input", { bubbles: true }));
            inner.dispatchEvent(new Event("change", { bubbles: true }));
          }
        };
        queueMicrotask(onMountedSync);
        el.__vDefaultCleanup = () => {
        };
      }
    },
    // If the default value changes later, you can decide whether to re-apply.
    // This version only applies when the current DOM value is empty.
    updated(el, binding) {
      const def = binding.value;
      if (def === void 0 || def === null || def === "") return;
      const target = "value" in el ? el : el.querySelector?.("input, textarea, select");
      if (!target) return;
      const cur = target.value;
      if (cur === "" || cur === void 0 || cur === null) {
        target.value = def;
        target.dispatchEvent(new Event("input", { bubbles: true }));
        target.dispatchEvent(new Event("change", { bubbles: true }));
      }
    },
    unmounted(el) {
      el.__vDefaultCleanup?.();
      delete el.__vDefaultCleanup;
    }
  });
});
