'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _vue = require('vue');

var _vue2 = _interopRequireDefault(_vue);

var _modal = require('./modal');

var _modal2 = _interopRequireDefault(_modal);

require('./index.less');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var instance = void 0;

function isInDocument(element) {
    return document.body.contains(element);
}

function initInstance() {
    if (instance) {
        instance.$destroy();
    }

    instance = new (_vue2['default'].extend(_modal2['default']))({
        el: document.createElement('div'),
        propsData: {
            lazyRender: false
        }
    });

    instance.$on('input', function (value) {
        instance.value = value;
    });
}

function Modal(options) {
    return new Promise(function (resolve, reject) {
        if (!instance || !isInDocument(instance.$el)) {
            initInstance();
        }

        (0, _extends3['default'])(instance, Modal.currentOptions, options, {
            resolve: resolve,
            reject: reject
        });
    });
}

Modal.defaultOptions = {
    value: true,
    title: '',
    width: '',
    message: '',
    overlay: true,
    className: '',
    lockScroll: true,
    transition: 'sn-modal-bounce',
    beforeClose: null,
    overlayClass: '',
    overlayStyle: null,
    messageAlign: '',
    getContainer: 'body',
    cancelButtonText: '取消',
    cancelButtonColor: null,
    confirmButtonText: '确认',
    confirmButtonColor: null,
    showConfirmButton: true,
    showCancelButton: false,
    closeOnPopstate: false,
    closeOnClickOverlay: false,
    callback: function callback(action) {
        instance[action === 'confirm' ? 'resolve' : 'reject'](action);
    }
};

Modal.alert = Modal;

Modal.confirm = function (options) {
    return Modal((0, _extends3['default'])({
        showCancelButton: true
    }, options));
};

Modal.close = function () {
    if (instance) {
        instance.value = false;
    }
};

Modal.setDefaultOptions = function (options) {
    (0, _extends3['default'])(Modal.currentOptions, options);
};

Modal.resetDefaultOptions = function () {
    Modal.currentOptions = (0, _extends3['default'])({}, Modal.defaultOptions);
};

Modal.resetDefaultOptions();

Modal.install = function () {
    _vue2['default'].use(_modal2['default']);
};

Modal.Component = _modal2['default'];

_vue2['default'].prototype.$modal = Modal;

exports['default'] = Modal;
module.exports = exports.default;