'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 _toast = require('./toast');

var _toast2 = _interopRequireDefault(_toast);

var _commonUtil = require('../utils/commonUtil');

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

var defaultOptions = {
    icon: '',
    mask: false,
    value: true,
    message: '',
    className: '',
    onClose: null,
    onOpened: null,
    duration: 2000,
    position: 'middle',
    transition: 'sn-fade',
    forbidClick: false,
    getContainer: 'body',
    overlayStyle: null,
    closeOnClick: false
};

var queue = [];
var multiple = false;
var currentOptions = (0, _extends3['default'])({}, defaultOptions);

function parseOptions(message) {
    if ((0, _commonUtil.isObj)(message)) {
        return message;
    }

    return { message: message };
}

function createInstance() {
    if (!queue.length || multiple) {
        var toast = new (_vue2['default'].extend(_toast2['default']))({
            el: document.createElement('div')
        });

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

        queue.push(toast);
    }

    return queue[queue.length - 1];
}

// transform toast options to popup props
function transformOptions(options) {
    options = (0, _extends3['default'])({}, options);
    options.overlay = options.mask;

    delete options.mask;
    delete options.duration;

    return options;
}

function Toast() {
    var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    var toast = createInstance();

    // should add z-index if previous toast has not disappeared
    if (toast.value) {
        toast.updateZIndex();
    }

    options = parseOptions(options);
    options = (0, _extends3['default'])({}, currentOptions, options);

    options.clear = function () {
        toast.value = false;

        if (options.onClose) {
            options.onClose();
        }

        if (multiple) {
            toast.$on('closed', function () {
                clearTimeout(toast.timer);
                queue = queue.filter(function (item) {
                    return item !== toast;
                });

                var parent = toast.$el.parentNode;
                if (parent) {
                    parent.removeChild(toast.$el);
                }

                toast.$destroy();
            });
        }
    };

    (0, _extends3['default'])(toast, transformOptions(options));
    clearTimeout(toast.timer);

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

    return toast;
}

var createMethod = function createMethod(type) {
    return function (options) {
        return Toast((0, _extends3['default'])({
            type: type
        }, parseOptions(options)));
    };
};

['loading', 'success', 'fail'].forEach(function (method) {
    Toast[method] = createMethod(method);
});

Toast.clear = function (all) {
    if (queue.length) {
        if (all) {
            queue.forEach(function (toast) {
                toast.clear();
            });
            queue = [];
        } else if (!multiple) {
            queue[0].clear();
        } else {
            queue.shift().clear();
        }
    }
};

Toast.allowMultiple = function () {
    var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;

    multiple = value;
};

Toast.install = function () {
    _vue2['default'].use(_toast2['default']);
};

_vue2['default'].prototype.$toast = Toast;

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