import { createVNode as _createVNode, mergeProps as _mergeProps } from "vue";
//@ts-nocheck
import { createApp, mergeProps } from 'vue';
import Modal from './Modal';
export const destroyFns = [];
export default function (config) {
  const div = document.createElement('div');
  document.body.appendChild(div);
  config = { ...config,
    visible: true
  };
  let ins = null;
  ins = createApp({
    data: () => ({
      config: { ...config
      },
      displayed: true
    }),

    render() {
      if (!this.displayed) return;
      const config = mergeProps(this.config, {
        onAfterLeave
      });
      return _createVNode(Modal, _mergeProps(config, {
        'visible': this.config.visible,
        "onUpdate:visible": $event => this.config.visible = $event,
        "closable": false,
        "footer": true
      }), {
        default: () => [config.content]
      });
    }

  }).mount(div);

  function onAfterLeave() {
    if (ins && div.parentNode) {
      ins.displayed = false;
      ins = null;
      div.parentNode.removeChild(div);
    }
  }

  function update(newConfig) {
    if (!ins) return;
    Object.assign(ins.config, newConfig);
  }

  function destroy() {
    const i = destroyFns.indexOf(destroy);
    if (~i) destroyFns.splice(i, 1);
    if (!ins) return;
    ins.config.visible = false;
  }

  destroyFns.push(destroy);
  return {
    destroy,
    update
  };
}