import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

import Vue from 'vue';
import ToastComponent from "./WToast";
import { isObj } from "../../../lib/utils";

var parseOptions = function parseOptions(params) {
  return isObj(params) ? params : {
    message: params
  };
};

var defaultOptions = {
  visible: true,
  duration: 2000,
  mask: false,
  message: '',
  type: 'success',
  icon: 'success-no-circle'
};
var queue = [];
var singleton = true;

var createInstance = function createInstance() {
  if (!queue.length || !singleton) {
    var toast = new (Vue.extend(ToastComponent))({
      el: document.createElement('div')
    });
    document.body.appendChild(toast.$el);
    queue.push(toast);
  }

  return queue[queue.length - 1];
};

var WToast = function WToast(options) {
  var toast = createInstance();
  options = _objectSpread({}, WToast.defaultOptions, {}, parseOptions(options));

  if (options.type === 'fail') {
    options.icon = 'warn';
  }

  _extends(toast, options);

  clearTimeout(toast.timer);

  if (options.duration > 0) {
    toast.timer = setTimeout(function () {
      toast.visible = false;
    }, options.duration);
  }

  return toast;
};

var createMethod = function createMethod(type) {
  return function (options) {
    return WToast(_objectSpread({
      type: type
    }, parseOptions(options)));
  };
};

WToast.defaultOptions = defaultOptions;
WToast.text = createMethod('text');
WToast.success = createMethod('success');
WToast.fail = createMethod('fail');
WToast.loading = createMethod('loading');

WToast.close = function () {
  var all = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;

  if (queue.length > 1) {
    if (all) {
      queue.forEach(function (toast) {
        toast.close();
      });
      queue = [];
    } else {
      queue.shift().close();
    }
  } else if (queue.length === 1) {
    queue[0].close();
  }
};

WToast.setDefaultOptions = function (options) {
  WToast.defaultOptions = _objectSpread({}, defaultOptions, {}, options);
};

WToast.resetDefaultOptions = function () {
  WToast.defaultOptions = defaultOptions;
};

WToast.allowMultiple = function () {
  var allow = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  singleton = !allow;
};

WToast.install = function () {// TODO
}; // TODO: 放到 install？


Vue.prototype.$toast = WToast;
export { WToast };
export default WToast;
//# sourceMappingURL=index.js.map