import { withDirectives as _withDirectives, createVNode as _createVNode, vShow as _vShow, Fragment as _Fragment } from "vue";
import { render } from "../_util/props-util";
import { newInstace } from "../MessageManager";
import Icon from '../icon';
let prefixCls = 'x-notification';
let instance, promise;

async function getInstace() {
  if (promise) return promise;
  promise = newInstace({
    prefixCls
  });
  return instance = await promise;
}

let __key = 0;

function getKey() {
  return '__' + __key++;
}

const api = {
  create: options => {
    var _options$key;

    const key = (_options$key = options.key) !== null && _options$key !== void 0 ? _options$key : getKey();
    getInstace().then(inst => {
      const obj = inst.add({ ...options,
        key,
        duration: options.duration === undefined ? 5000 : options.duration,
        default: () => {
          return _createVNode(_Fragment, null, [_withDirectives(_createVNode("div", {
            "class": `${prefixCls}_cover`
          }, [render(options.cover)]), [[_vShow, options.cover]]), _createVNode("div", {
            "class": `${prefixCls}_header`
          }, [_createVNode("div", {
            "class": `${prefixCls}_title`
          }, [render(options.title)]), _withDirectives(_createVNode("div", {
            "class": `${prefixCls}_close`,
            "onClick": onClose
          }, [render(options.closeIcon) || _createVNode(Icon, {
            "type": "cross"
          }, null)]), [[_vShow, options.closable]]), _withDirectives(_createVNode("div", {
            "class": `${prefixCls}_description`
          }, [render(options.description)]), [[_vShow, options.description]]), _withDirectives(_createVNode("div", {
            "class": `${prefixCls}_meta`
          }, [render(options.meta)]), [[_vShow, options.meta]])])]);
        },
        transitionProps: {
          width: w => w * .4,
          height: h => h * .2
        }
      });

      function onClose(e) {
        e.preventDefault();
        e.stopPropagation();
        obj.close();
      }
    });
    return () => {
      var _instance;

      return (_instance = instance) === null || _instance === void 0 ? void 0 : _instance.remove(key);
    };
  },
  destroyAll: () => instance.removeAll()
};

api.install = app => {
  app.config.globalProperties.$notification = api;
};

export default api;