import { __awaiter, __generator } from "tslib";
import Vue from 'vue';
import { MToast } from '../../components/toast/toast';
var TIME_BEFORE_ANIMATION_IS_OVER = 300;
var ToastService = /** @class */ (function () {
    function ToastService() {
        this.toasts = [];
        this.baseTopPosition = '0';
    }
    ToastService.prototype.show = function (params) {
        var toast = this.createToast(params);
        this.toasts.push(toast);
        if (this.activeToast) {
            this.activeToast.open = false;
            this.activeToast = this.toasts[1];
            this.toasts.splice(0, 1);
        }
        else {
            this.activeToast = this.toasts[0];
        }
    };
    ToastService.prototype.clear = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                if (this.activeToast) {
                    this.activeToast.open = false;
                }
                this.toasts = [];
                this.activeToast = undefined;
                return [2 /*return*/, new Promise(function (resolve) {
                        setTimeout(function () { return resolve(); }, TIME_BEFORE_ANIMATION_IS_OVER);
                    })];
            });
        });
    };
    ToastService.prototype.createToast = function (params) {
        var _a;
        var toast = new MToast({
            el: document.createElement('div'),
            propsData: (_a = {},
                _a['open.sync'] = true,
                _a.state = params.state,
                _a.position = params.position,
                _a.timeout = params.timeout,
                _a.actionLabel = params.actionLabel,
                _a.icon = params.icon,
                _a.closeButton = params.closeButton,
                _a)
        });
        toast.$on('close', function () { return setTimeout(function () { return toast.$destroy(); }, TIME_BEFORE_ANIMATION_IS_OVER); });
        if (params.action) {
            toast.$on('action-button', function (event) {
                params.action(event);
            });
        }
        toast.offset = toast.isTop ? this.baseTopPosition : '0';
        // Si c'est déjà du HTML, laisser tel quel, sinon, encapsuler dans un p
        params.text = /^<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>$/.test(params.text) ? params.text : "<p>" + params.text + "</p>";
        if (Vue.compile !== undefined) {
            toast.$slots.default = [toast.$createElement(Vue.compile(params.text))];
        }
        else {
            toast.$slots.default = [toast.$createElement('div', { domProps: { innerHTML: params.text } })];
        }
        return toast;
    };
    return ToastService;
}());
export default ToastService;
//# sourceMappingURL=toast-service.js.map