"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _assign = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/assign"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/objectWithoutPropertiesLoose"));

var _vue = _interopRequireDefault(require("vue"));

var _Tips = _interopRequireDefault(require("./Tips"));

var _Icon = _interopRequireDefault(require("../Icon"));

/*
 * @Author: lijianzhang
 * @Date: 2018-03-07 23:50:36
 * @Last Modified by: lijianzhang
 * @Last Modified time: 2019-02-25 11:30:37
 */
var TipsVue = _vue.default.extend(_Tips.default);

var vm = null;
var close = null;

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

  var message = options.message,
      loading = options.loading,
      props = (0, _objectWithoutPropertiesLoose2.default)(options, ["message", "loading"]);
  (0, _assign.default)(vm, (0, _assign.default)({
    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();
  var h = vm.$createElement;
  var msg = message;

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

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

  vm.visible = true;

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

  return close;
}

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

;

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

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

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

var _default = tips;
exports.default = _default;