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')
        });
        document.body.appendChild(alertInstance.$el);
        alertInstance.message = message;
        alertInstance.okLabel = okLabel ? okLabel : undefined;
        alertInstance.title = title ? title : '';
        alertInstance.negativeLink = false;
        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')
        });
        document.body.appendChild(confirmInstance.$el);
        confirmInstance.message = message;
        confirmInstance.state = MDialogState.Confirmation;
        confirmInstance.title = title ? title : '';
        confirmInstance.okLabel = okLabel ? okLabel : undefined;
        confirmInstance.cancelLabel = cancelLabel ? cancelLabel : undefined;
        return this.show(confirmInstance).then(function (result) {
            confirmInstance.$destroy();
            return result;
        });
    };
    /**
     *
     * @param message the message of the confirmation dialog
     * @param state the dialog state
     * @param title a title label if any
     * @param okLabel a ok label if any
     * @param secBtn secBtn if any
     * @param secBtnLabel a secBtnLabel label if any
     * @param btnWidth a btnWidth if any
     * @param negativeLink negativeLink if any
     * @param cancelLabel a cancel label if any
     * @param width the width of the dialog if any
     */
    DialogService.prototype.generic = function (message, state, title, okLabel, secBtn, secBtnLabel, btnWidth, negativeLink, cancelLabel, width) {
        var genericInstance = new MDialog({
            el: document.createElement('div')
        });
        document.body.appendChild(genericInstance.$el);
        genericInstance.message = message;
        genericInstance.state = state ? state : MDialogState.Default;
        genericInstance.title = title ? title : '';
        genericInstance.okLabel = okLabel ? okLabel : undefined;
        genericInstance.secBtn = secBtn ? secBtn : false;
        genericInstance.secBtnLabel = secBtnLabel ? secBtnLabel : undefined;
        genericInstance.btnWidth = btnWidth ? btnWidth : '100%';
        genericInstance.negativeLink = negativeLink ? negativeLink : true;
        genericInstance.cancelLabel = cancelLabel ? cancelLabel : undefined;
        genericInstance.width = width ? width : MDialogWidth.Default;
        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