"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Vue = require("vue");
const DialogRenderer = require("./DialogRenderer.vue2.js");
let currentDialogInstance = null;
let isCleaningUp = false;
const cleanupDialog = () => {
  if (isCleaningUp) return;
  isCleaningUp = true;
  setTimeout(() => {
    var _a;
    try {
      if ((_a = currentDialogInstance == null ? void 0 : currentDialogInstance.container) == null ? void 0 : _a.parentNode) {
        currentDialogInstance.container.parentNode.removeChild(currentDialogInstance.container);
      }
      if (currentDialogInstance == null ? void 0 : currentDialogInstance.vnode) {
        Vue.render(null, currentDialogInstance.container);
      }
      currentDialogInstance = null;
    } catch (error) {
      console.error("Dialog cleanup error:", error);
    } finally {
      isCleaningUp = false;
    }
  }, 300);
};
function openDialog(displayField, dialogConfig, instance, componentCtx, fieldsList, formData) {
  return new Promise((resolve, reject) => {
    if (currentDialogInstance) {
      closeDialog();
    }
    const container = document.createElement("div");
    document.body.appendChild(container);
    const vnode = Vue.createVNode(DialogRenderer.default, {
      dialogConfig,
      modelValue: true,
      fieldsList,
      formData,
      displayField,
      componentCtx,
      instance,
      "onUpdate:modelValue": (val) => {
        if (!val) {
          cleanupDialog();
        }
      },
      onConfirm: (data) => {
        cleanupDialog();
        resolve(data);
      },
      onCancel: () => {
        cleanupDialog();
        reject(new Error("用户取消"));
      }
    });
    vnode.appContext = instance.appContext;
    Vue.render(vnode, container);
    currentDialogInstance = { vnode, container };
  });
}
function closeDialog() {
  var _a, _b, _c;
  if ((_c = (_b = (_a = currentDialogInstance == null ? void 0 : currentDialogInstance.vnode) == null ? void 0 : _a.component) == null ? void 0 : _b.ctx) == null ? void 0 : _c.dialogVisible) {
    currentDialogInstance.vnode.component.ctx.dialogVisible.value = false;
  }
}
exports.closeDialog = closeDialog;
exports.openDialog = openDialog;
