/*! bxs-vue-ui v3.3.0 */
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

require('../utils/entry-virtual.js');
var normalizeComponent = require('../utils/normalize-component.js');
require('../utils/array-iteration.js');
require('../utils/sloppy-array-method.js');
var forEach = require('../utils/for-each.js');
var _extends = require('../utils/extends.js');
var Vue = _interopDefault(require('vue'));
var icon = require('../icon/index.js');
var splice = require('../utils/splice.js');
var setTimeout = require('../utils/set-timeout.js');

var _components;
var script = normalizeComponent.create({
  name: 'toast',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    message: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: ''
    },
    icon: {
      type: String,
      default: ''
    }
  },
  components: (_components = {}, _components[icon.default.name] = icon.default, _components)
});

/* script */
var __vue_script__ = script;
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('transition', {
    attrs: {
      "name": "bx-animate-fade"
    }
  }, [_vm.visible ? _c('div', {
    staticClass: "bx-toast"
  }, [_c('div', {
    staticClass: "bx-toast-inner",
    class: _vm.icon && 'bx-toast-block'
  }, [_vm.icon ? _c('bxs-icon', {
    staticClass: "bx-toast-icon",
    attrs: {
      "name": _vm.icon
    }
  }) : _vm._e(), _c('span', {
    staticClass: "bx-toast-text"
  }, [_vm._v("\n        " + _vm._s(_vm.message) + "\n      ")])], 1)]) : _vm._e()]);
};

var __vue_staticRenderFns__ = [];
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* style inject */

/* style inject SSR */

var ToastView = normalizeComponent.__vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

var _context;
var ToastConstructor = Vue.extend(ToastView);
var toastPool = [];

var getAnInstance = function getAnInstance() {
  if (toastPool.length > 0) {
    var instance = toastPool[0];

    splice._spliceInstanceProperty(toastPool).call(toastPool, 0, 1);

    return instance;
  }

  return new ToastConstructor({
    el: document.createElement('div')
  });
};

var returnAnInstance = function returnAnInstance(instance) {
  if (instance) {
    toastPool.push(instance);
  }
};

var removeDom = function removeDom(event) {
  if (event.target.parentNode) {
    event.target.parentNode.removeChild(event.target);
  }
};

ToastConstructor.prototype.close = function close() {
  this.visible = false;
  this.$el.addEventListener('transitionend', removeDom);
  this.closed = true;
  returnAnInstance(this);
};

function Toast(options) {
  if (options === void 0) {
    options = {};
  }

  var duration = options.duration || 2000;
  var instance = getAnInstance();
  instance.closed = false;
  clearTimeout(instance.timer);
  instance.message = typeof options === 'string' ? options : options.message;
  instance.type = options.type || '';
  instance.icon = options.icon || '';

  if (!instance.icon) {
    switch (instance.type) {
      case 'success':
        instance.icon = 'check-plain';
        break;

      case 'fail':
        instance.icon = 'close-plain';
        break;

      case 'warning':
        instance.icon = 'warning-plain';
        break;

      default:
        instance.icon = '';
    }
  }

  document.body.appendChild(instance.$el);
  Vue.nextTick(function () {
    instance.visible = true;
    instance.$el.removeEventListener('transitionend', removeDom);

    if (duration) {
      instance.timer = setTimeout._setTimeout(function () {
        if (instance.closed) return;
        instance.close();
      }, duration);
    }
  });
  return instance;
}

forEach._forEachInstanceProperty(_context = ['success', 'fail', 'warning']).call(_context, function (method) {
  Toast[method] = function (options) {
    if (options === void 0) {
      options = {};
    }

    var opt = {
      type: method
    };

    if (typeof options === 'string') {
      opt.message = options;
    } else {
      opt = _extends._extends(opt, options);
    }

    Toast(opt);
  };
});

Toast.install = function (vueGlobal) {
  /* eslint no-param-reassign: ["error", { "props": false }] */
  vueGlobal.use(ToastView);
  vueGlobal.prototype.$toast = Toast;
};

exports.default = Toast;
