import { __assign } from "tslib";
import Vue from 'vue';
import { MDialog, MDialogState, MDialogWidth } from '../../components/dialog/dialog';
var DialogService = /** @class */ (function () {
    function DialogService() {
    }
    /**
     *
     * @param message the message of the confirmation dialog
     * @param title a title label if any
     * @param okLabel a cancel label if any
     */
    DialogService.prototype.alert = function (message, title, okLabel) {
        var alertInstance = new MDialog({
            el: document.createElement('div'),
            propsData: {
                message: message,
                title: title,
                okLabel: okLabel,
                negativeLink: false
            }
        });
        document.body.appendChild(alertInstance.$el);
        return this.show(alertInstance).then(function (result) {
            alertInstance.$destroy();
            return result;
        });
    };
    /**
     *
     * @param message the message of the confirmation dialog
     * @param title a title label if any
     * @param okLabel a ok label if any
     * @param cancelLabel a cancel label if any
     */
    DialogService.prototype.confirm = function (message, title, okLabel, cancelLabel) {
        var confirmInstance = new MDialog({
            el: document.createElement('div'),
            propsData: {
                message: message,
                state: MDialogState.Confirmation,
                title: title,
                okLabel: okLabel,
                cancelLabel: cancelLabel
            }
        });
        document.body.appendChild(confirmInstance.$el);
        return this.show(confirmInstance).then(function (result) {
            confirmInstance.$destroy();
            return result;
        });
    };
    DialogService.prototype.generic = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        var genericInstance;
        if (typeof args[0] === 'string') {
            genericInstance = new MDialog({
                el: document.createElement('div'),
                propsData: {
                    message: args[0],
                    state: args[1] || MDialogState.Default,
                    title: args[2],
                    okLabel: args[3],
                    secBtn: args[4] || false,
                    secBtnLabel: args[5],
                    btnWidth: args[6],
                    negativeLink: args[7] || true,
                    cancelLabel: args[8],
                    width: args[9] || MDialogWidth.Default,
                    messageStyle: args[10]
                }
            });
        }
        else {
            genericInstance = new MDialog({
                el: document.createElement('div'),
                propsData: __assign({
                    state: MDialogState.Default,
                    secBtn: false,
                    negativeLink: true,
                    width: MDialogWidth.Default
                }, args[0])
            });
        }
        document.body.appendChild(genericInstance.$el);
        return this.show(genericInstance).then(function (result) {
            genericInstance.$destroy();
            return result;
        });
    };
    /**
     *
     * @param mDialogInstance the MDialog instance
     * @param rejectOnCancel if true, the promise will be rejected on cancel
     */
    DialogService.prototype.show = function (mDialogInstance, rejectOnCancel) {
        return new Promise(function (resolve, reject) {
            var onOk = function () {
                if (mDialogInstance) {
                    unhook();
                }
                Vue.nextTick(function () {
                    resolve(true);
                });
            };
            var onCancel = function () {
                if (mDialogInstance) {
                    unhook();
                }
                Vue.nextTick(function () {
                    resolve(false);
                });
            };
            var onSecondaryBtn = function () {
                if (mDialogInstance) {
                    unhook();
                }
                Vue.nextTick(function () {
                    resolve(false);
                });
            };
            var hook = function () {
                if (mDialogInstance) {
                    mDialogInstance.$on('ok', onOk);
                    mDialogInstance.$on('cancel', onCancel);
                    mDialogInstance.$on('secondaryBtn', onSecondaryBtn);
                    mDialogInstance.openDialog();
                }
            };
            var unhook = function () {
                if (mDialogInstance) {
                    mDialogInstance.$off('ok', onOk);
                    mDialogInstance.$off('cancel', onCancel);
                    mDialogInstance.$off('secondaryBtn', onSecondaryBtn);
                }
            };
            if (mDialogInstance) {
                mDialogInstance.$nextTick(function () {
                    hook();
                });
            }
            else {
                console.error('No instance of dialog');
                reject();
            }
        });
    };
    return DialogService;
}());
export { DialogService };
//# sourceMappingURL=dialog-service.js.map