(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('@wm/components/base'), require('@wm/components/dialogs'), require('@wm/components/dialogs/design-dialog'), require('@wm/components/input'), require('@wm/core'), require('@wm/components/data/form')) :
    typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/common', '@wm/components/base', '@wm/components/dialogs', '@wm/components/dialogs/design-dialog', '@wm/components/input', '@wm/core', '@wm/components/data/form'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.wm = global.wm || {}, global.wm.components = global.wm.components || {}, global.wm.components.dialogs = global.wm.components.dialogs || {}, global.wm.components.dialogs.logindialog = {}), global.ng.core, global.ng.common, global.wm.components.base, global.wm.components.dialogs, global.wm.components.dialogs.designdialog, global.wm.components.input, global.wm.core, global.wm.components.data.form));
})(this, (function (exports, i0, common, i1, dialogs, designDialog, input, core, form) { 'use strict';

    function _interopNamespaceDefault(e) {
        var n = Object.create(null);
        if (e) {
            Object.keys(e).forEach(function (k) {
                if (k !== 'default') {
                    var d = Object.getOwnPropertyDescriptor(e, k);
                    Object.defineProperty(n, k, d.get ? d : {
                        enumerable: true,
                        get: function () { return e[k]; }
                    });
                }
            });
        }
        n.default = e;
        return Object.freeze(n);
    }

    var i0__namespace = /*#__PURE__*/_interopNamespaceDefault(i0);
    var i1__namespace = /*#__PURE__*/_interopNamespaceDefault(i1);

    class LoginDialogDirective {
        constructor(contexts, dialogRef) {
            this.contexts = contexts;
            this.dialogRef = dialogRef;
            this.contexts[0].doLogin = () => this.doLogin();
        }
        hideMsg() {
            if (this.msgCmp) {
                this.msgCmp.hideMessage();
            }
        }
        showError(msg) {
            if (this.msgCmp) {
                this.msgCmp.showMessage(msg, 'error');
            }
        }
        showLoading() {
            if (this.msgCmp) {
                this.msgCmp.showMessage('Loading...', 'loading');
            }
        }
        onSuccess() {
            this.hideMsg();
        }
        onError(error) {
            this.showError(error);
        }
        getLoginDetails() {
            return this.formCmp.first.dataoutput;
        }
        doLogin() {
            const loginInfo = this.getLoginDetails();
            const ds = this.dialogRef.eventsource;
            if (ds && loginInfo['j_username'] && loginInfo['j_password']) {
                this.showLoading();
                ds.invoke({ loginInfo: loginInfo }, this.onSuccess.bind(this), this.onError.bind(this));
            }
        }
        ngAfterViewInit() {
            // On login dialog open we wait till the form loads and then assign the enter
            // key event to the textbox in form.
            this.dialogOpenSubscription = this.dialogRef.bsModal.onShown.subscribe(() => {
                setTimeout(() => {
                    // On enter key press submit the login form in Login Dialog
                    this.dialogRef.$element.find('.app-textbox').keypress((evt) => {
                        if (evt.which === 13) {
                            evt.stopPropagation();
                            /**
                             * As this function is getting executed outside of angular context,
                             * trigger a digest cycle and then trigger doLogin method, So that the bindings in the loginVariable will be evaluated property
                             */
                            core.$appDigest();
                            this.doLogin();
                        }
                    });
                });
            });
        }
        ngOnDestroy() {
            this.dialogOpenSubscription.unsubscribe();
        }
        static { this.ɵfac = function LoginDialogDirective_Factory(t) { return new (t || LoginDialogDirective)(i0__namespace.ɵɵdirectiveInject(i1.Context, 2), i0__namespace.ɵɵdirectiveInject(i1__namespace.DialogRef, 2)); }; }
        static { this.ɵdir = /*@__PURE__*/ i0__namespace.ɵɵdefineDirective({ type: LoginDialogDirective, selectors: [["", "wmDialog", "", "wmLoginDialog", ""]], contentQueries: function LoginDialogDirective_ContentQueries(rf, ctx, dirIndex) { if (rf & 1) {
                i0__namespace.ɵɵcontentQuery(dirIndex, i1.MessageComponent, 5);
                i0__namespace.ɵɵcontentQuery(dirIndex, form.FormComponent, 4);
            } if (rf & 2) {
                let _t;
                i0__namespace.ɵɵqueryRefresh(_t = i0__namespace.ɵɵloadQuery()) && (ctx.msgCmp = _t.first);
                i0__namespace.ɵɵqueryRefresh(_t = i0__namespace.ɵɵloadQuery()) && (ctx.formCmp = _t);
            } } }); }
    }
    (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0__namespace.ɵsetClassMetadata(LoginDialogDirective, [{
            type: i0.Directive,
            args: [{
                    selector: '[wmDialog][wmLoginDialog]'
                }]
        }], () => [{ type: Array, decorators: [{
                    type: i0.Self
                }, {
                    type: i0.Inject,
                    args: [i1.Context]
                }] }, { type: i1__namespace.DialogRef, decorators: [{
                    type: i0.Self
                }] }], { formCmp: [{
                type: i0.ContentChildren,
                args: [form.FormComponent]
            }], msgCmp: [{
                type: i0.ContentChild,
                args: [i1.MessageComponent]
            }] }); })();

    const components = [
        LoginDialogDirective
    ];
    class LoginDialogModule {
        static { this.ɵfac = function LoginDialogModule_Factory(t) { return new (t || LoginDialogModule)(); }; }
        static { this.ɵmod = /*@__PURE__*/ i0__namespace.ɵɵdefineNgModule({ type: LoginDialogModule }); }
        static { this.ɵinj = /*@__PURE__*/ i0__namespace.ɵɵdefineInjector({ imports: [common.CommonModule,
                designDialog.DesignDialogModule,
                dialogs.DialogModule,
                input.InputModule,
                i1.WmComponentsModule] }); }
    }
    (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0__namespace.ɵsetClassMetadata(LoginDialogModule, [{
            type: i0.NgModule,
            args: [{
                    imports: [
                        common.CommonModule,
                        designDialog.DesignDialogModule,
                        dialogs.DialogModule,
                        input.InputModule,
                        i1.WmComponentsModule
                    ],
                    declarations: [...components],
                    exports: [...components]
                }]
        }], null, null); })();
    (function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0__namespace.ɵɵsetNgModuleScope(LoginDialogModule, { declarations: [LoginDialogDirective], imports: [common.CommonModule,
            designDialog.DesignDialogModule,
            dialogs.DialogModule,
            input.InputModule,
            i1.WmComponentsModule], exports: [LoginDialogDirective] }); })();

    exports.LoginDialogDirective = LoginDialogDirective;
    exports.LoginDialogModule = LoginDialogModule;

}));
