import { createVNode, render, vueVersion } from "../../../adapter-vue.mjs";
import { TUIGlobal } from "@tencentcloud/universal-api";
import { default as default2 } from "./type.mjs";
import MessageConstructor from "./index.vue.mjs";
const instances = [];
let seed = 1;
const vueVersionInt = Math.trunc(vueVersion);
const appendTo = document.body;
const Toast = function(options) {
  var _a;
  let verticalOffset = options.offset || 20;
  instances.forEach(({ vm: vm2 }) => {
    var _a2, _b;
    verticalOffset += (((_a2 = vm2 == null ? void 0 : vm2.el) == null ? void 0 : _a2.offsetHeight) || ((_b = vm2 == null ? void 0 : vm2.$el) == null ? void 0 : _b.offsetHeight) || 0) + 20;
  });
  verticalOffset += 20;
  const id = `message_${seed += 1}`;
  const userOnClose = options.onClose;
  const props = {
    zIndex: 20 + seed,
    offset: verticalOffset,
    id,
    ...options,
    onClose: () => {
      Toast.close(id, userOnClose);
    }
  };
  let vm;
  let container;
  switch (vueVersionInt) {
    case 2:
      const Vue = (_a = TUIGlobal) == null ? void 0 : _a.Vue;
      if (!Vue) {
        return {};
      }
      const Constructor = Vue.extend(MessageConstructor);
      const instance = new Constructor({ propsData: props });
      instance.$mount();
      appendTo.appendChild(instance.$el);
      instance.$el.style.zIndex = props.zIndex;
      instance.verticalOffset = props.verticalOffset;
      instance.visible = true;
      instances.push({ vm: instance });
      return {
        close: () => {
          instance.visible = false;
        }
      };
    case 3:
      vm = createVNode(MessageConstructor, props);
      container = document.createElement("div");
      (vm == null ? void 0 : vm.props) && (vm.props.onDestroy = () => {
        render(null, container);
      });
      (vm == null ? void 0 : vm.data) && (vm.data.onDestroy = () => {
        render(null, container);
      });
      render(vm, container);
      instances.push({ vm });
      appendTo.appendChild(container.firstElementChild);
      return {
        close: () => {
          var _a2;
          ((_a2 = vm == null ? void 0 : vm.component) == null ? void 0 : _a2.proxy) && (vm.component.proxy.visible = false);
        }
      };
  }
  return {};
};
function close(id, userOnClose) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
  const idx = instances == null ? void 0 : instances.findIndex(
    ({ vm: vm2 }) => {
      var _a2, _b2, _c2;
      return id === ((_b2 = (_a2 = vm2 == null ? void 0 : vm2.component) == null ? void 0 : _a2.props) == null ? void 0 : _b2.id) || id === ((_c2 = vm2 == null ? void 0 : vm2._props) == null ? void 0 : _c2.id);
    }
  );
  if (idx === -1) return;
  const { vm } = instances[idx];
  if (!vm) return;
  userOnClose == null ? void 0 : userOnClose(vm);
  const removedHeight = ((_a = vm == null ? void 0 : vm.el) == null ? void 0 : _a.offsetHeight) || ((_b = vm == null ? void 0 : vm.$el) == null ? void 0 : _b.offsetHeight);
  instances.splice(idx, 1);
  const len = instances.length;
  if (len < 1) return;
  for (let i = idx; i < len; i++) {
    const pos = Number.parseInt(
      ((_f = (_e = (_d = (_c = instances[i]) == null ? void 0 : _c.vm) == null ? void 0 : _d.el) == null ? void 0 : _e.style) == null ? void 0 : _f.top) || ((_j = (_i = (_h = (_g = instances[i]) == null ? void 0 : _g.vm) == null ? void 0 : _h.$el) == null ? void 0 : _i.style) == null ? void 0 : _j.top),
      10
    ) - removedHeight - 16;
    ((_n = (_m = (_l = (_k = instances[i]) == null ? void 0 : _k.vm) == null ? void 0 : _l.component) == null ? void 0 : _m.props) == null ? void 0 : _n.offset) && (instances[i].vm.component.props.offset = pos);
    ((_q = (_p = (_o = instances[i]) == null ? void 0 : _o.vm) == null ? void 0 : _p._props) == null ? void 0 : _q.offset) && (instances[i].vm._props.offset = pos);
  }
}
Toast.close = close;
export {
  default2 as TOAST_TYPE,
  Toast,
  close
};
