/*! bxs-vue-ui v3.3.0 */
import '../utils/entry-virtual.js';
import { C as create, _ as __vue_normalize__, Q as _Promise } from '../utils/normalize-component.js';
import '../utils/object-create.js';
import '../utils/html.js';
import '../utils/es.number.constructor.js';
import '../utils/array-iteration.js';
import '../utils/sloppy-array-method.js';
import { _ as _indexOfInstanceProperty } from '../utils/index-of.js';
import '../utils/filter.js';
import { _ as _extends } from '../utils/extends.js';
import { P as Popup } from '../utils/index.js';
import '../utils/some.js';
import Vue from 'vue';
import '../utils/env.js';
import '../safe-area-patch/index.js';

var onlyValues = function onlyValues(values) {
  return function (str) {
    return _indexOfInstanceProperty(values).call(values, str) !== -1;
  };
};

var script = create({
  name: 'message-box',
  mixins: [Popup],
  props: {
    title: String,
    message: String,
    tips: String,
    className: [String, Object, Array],
    messageAlign: {
      type: String,
      default: 'center',
      validator: onlyValues(['left', 'right', 'center', 'justify'])
    },
    cancelButtonText: {
      type: String,
      default: '取消'
    },
    confirmButtonText: {
      type: String,
      default: '确定'
    },
    cancelButtonColor: {
      type: String,
      default: ''
    },
    confirmButtonColor: {
      type: String,
      default: '#508cee'
    },
    showCancel: {
      type: Boolean,
      default: true
    },
    showConfirm: {
      type: Boolean,
      default: true
    },
    closeOnClickOverlay: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    cancelButtonStyle: function cancelButtonStyle() {
      var style = {};

      if (this.cancelButtonColor) {
        style.color = this.cancelButtonColor;
      }

      return style;
    },
    confirmButtonStyle: function confirmButtonStyle() {
      var style = {};

      if (this.confirmButtonColor) {
        style.color = this.confirmButtonColor;
      }

      return style;
    }
  },
  methods: {
    handleAction: function handleAction(action) {
      var _this = this;

      this.$emit('before-close', action, function (state) {
        if (state !== false) {
          _this.closeModal(action);
        }
      });

      if (this.callback) {
        this.callback(action);
      }
    },
    closeModal: function closeModal(action) {
      this.$emit('input', false);
      this.$emit(action);
    }
  }
});

/* 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.value ? _c('div', {
    staticClass: "bx-modal",
    class: _vm.className
  }, [_c('div', {
    staticClass: "bx-modal-body"
  }, [_vm.title ? _c('div', {
    staticClass: "bx-modal-title"
  }, [_vm._v("\n        " + _vm._s(_vm.title) + "\n      ")]) : _vm._e(), _vm._t("default", [_c('div', {
    staticClass: "bx-modal-content"
  }, [_c('div', {
    staticClass: "bx-modal-message",
    style: {
      'text-align': _vm.messageAlign
    },
    domProps: {
      "innerHTML": _vm._s(_vm.message)
    }
  }), _vm.tips ? _c('div', {
    staticClass: "bx-modal-tips"
  }, [_vm._v("\n            " + _vm._s(_vm.tips) + "\n          ")]) : _vm._e()])])], 2), _c('div', {
    staticClass: "bx-modal-footer bx-hairline--top"
  }, [_vm.showCancel ? _c('a', {
    staticClass: "bx-modal-btn bx-hairline--left",
    style: _vm.cancelButtonStyle,
    on: {
      "click": function click($event) {
        _vm.handleAction('cancel');
      }
    }
  }, [_vm._v("\n        " + _vm._s(_vm.cancelButtonText) + "\n      ")]) : _vm._e(), _vm.showConfirm ? _c('a', {
    staticClass: "bx-modal-btn bx-hairline--left",
    style: _vm.confirmButtonStyle,
    on: {
      "click": function click($event) {
        _vm.handleAction('confirm');
      }
    }
  }, [_vm._v("\n        " + _vm._s(_vm.confirmButtonText) + "\n      ")]) : _vm._e()])]) : _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 msgboxView = __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 defaults = {
  title: '',
  tips: '',
  message: '',
  className: '',
  type: 'dialog',
  messageAlign: 'center',
  cancelButtonText: '取消',
  confirmButtonText: '确定',
  cancelButtonColor: '',
  confirmButtonColor: '#508cee',
  showCancel: true,
  showConfirm: true,
  lockScroll: true,
  closeOnClickOverlay: false
};
var MessageBoxConstructor = Vue.extend(msgboxView);
var currentMsg;
var instance;
var msgQueue = [];
/* eslint-disable no-use-before-define */

var defaultCallback = function defaultCallback(action) {
  if (currentMsg) {
    var _currentMsg = currentMsg,
        callback = _currentMsg.callback;

    if (typeof callback === 'function') {
      callback(action);
    }

    if (currentMsg.resolve) {
      var type = currentMsg.options.type;

      if (type === 'dialog') {
        currentMsg[action === 'confirm' ? 'resolve' : 'reject'](action);
      } else {
        currentMsg.resolve(action);
      }
    }

    MessageBox.close();
  }
};

var initInstance = function initInstance() {
  instance = new MessageBoxConstructor({
    el: document.createElement('div')
  });
  instance.$on('input', function (value) {
    instance.value = value;
  });
  instance.callback = defaultCallback;
}; // 显示消息队列里的第一个消息


var showNextMsg = function showNextMsg() {
  if (!instance) {
    initInstance();
  }

  if (!instance.value) {
    if (msgQueue.length > 0) {
      currentMsg = msgQueue.shift();
      var _currentMsg2 = currentMsg,
          options = _currentMsg2.options;
      instance = _extends(instance, options);

      if (options.callback === undefined) {
        instance.callback = defaultCallback;
      }

      document.body.appendChild(instance.$el);
      instance.value = true;
    }
  }
};
/* eslint-disable no-param-reassign */

/* eslint-disable prefer-rest-params */

/* eslint-disable prefer-destructuring */


var MessageBox = function MessageBox(options, callback) {
  if (typeof options === 'string') {
    options = {
      message: options
    };
  }

  if (options.callback && !callback) {
    callback = options.callback;
  }

  if (typeof _Promise !== 'undefined') {
    return new _Promise(function (resolve, reject) {
      // eslint-disable-line
      msgQueue.push({
        options: _extends({}, defaults, MessageBox.defaults || {}, options),
        callback: callback,
        resolve: resolve,
        reject: reject
      });
      showNextMsg();
    });
  }

  msgQueue.push({
    options: _extends({}, defaults, MessageBox.defaults || {}, options),
    callback: callback
  });
  showNextMsg();
  return instance;
};

MessageBox.setDefaults = function (newDefaults) {
  MessageBox.defaults = newDefaults;
};

MessageBox.alert = function (message, title, tips, options) {
  if (typeof message === 'object') {
    options = message;
    message = '';
  }

  if (typeof title === 'object') {
    options = title;
    title = '';
  }

  if (typeof tips === 'object') {
    options = tips;
    tips = '';
  }

  return MessageBox(_extends({
    title: title,
    message: message,
    tips: tips,
    type: 'alert',
    showCancel: true,
    showConfirm: false,
    cancelButtonText: '知道了'
  }, options));
};

MessageBox.dialog = function (message, title, tips, options) {
  if (typeof message === 'object') {
    options = message;
    message = '';
  }

  if (typeof title === 'object') {
    options = title;
    title = '';
  }

  if (typeof tips === 'object') {
    options = tips;
    tips = '';
  }

  return MessageBox(_extends({
    title: title,
    message: message,
    tips: tips,
    type: 'dialog',
    showCancel: true,
    showConfirm: true
  }, options));
};

MessageBox.close = function () {
  if (!instance) return;
  instance.value = false;
  msgQueue = [];
  currentMsg = null;
};

MessageBox.install = function (vueGlobal) {
  vueGlobal.use(msgboxView);
  vueGlobal.prototype.$messagebox = MessageBox;
};

export default MessageBox;
