import _Object$assign from "@babel/runtime-corejs2/core-js/object/assign";

/*
 * @Author: lijianzhang
 * @Date: 2018-03-07 23:50:36
 * @Last Modified by: lijianzhang
 * @Last Modified time: 2018-12-16 13:07:59
 */
import Vue from 'vue';
import Tips from './tips';
import Icon from '../icon';
const TipsVue = Vue.extend(Tips);
let vm = null;
let close = null;

function createTips(options) {
  if (!vm) {
    vm = new TipsVue({
      components: {
        Icon
      }
    });
    vm.$mount();
  }

  const {
    message,
    loading,
    ...props
  } = options;

  _Object$assign(vm, {
    delay: 2000,
    ...props,

    onClose() {
      vm.$destroy();
      if (vm.$el && vm.$el.parentNode) vm.$el.parentNode.removeChild(vm.$el);
      vm = null;
      close = null;
    }

  });

  vm.clearAutoClose();
  vm.autoClose();
  const h = vm.$createElement;
  let msg = message;

  if (msg instanceof Error) {
    msg = msg.message;
  }

  if (loading) {
    vm.$slots.default = h('div', [h('icon', {
      props: {
        name: 'loading',
        spin: true
      },
      style: {
        color: '#fff',
        fontSize: '50px'
      }
    }), h('div', msg)]);
  } else {
    vm.$slots.default = h('div', msg);
  }

  vm.visible = true;

  close = () => {
    if (vm.visible) {
      vm.visible = false;
    }
  };

  return close;
}

function tips(message, props) {
  return createTips({
    message,
    ...props
  });
}

;

tips.loading = function (message, props) {
  return createTips({
    message,
    delay: 0,
    ...props,
    loading: true
  });
};

tips.close = function () {
  close && close();
};

tips.install = function (Vue) {
  Vue.prototype.$tips = tips;
};

export default tips;