import * as i0 from '@angular/core';
import { Directive, Self, Inject, ContentChildren, ContentChild, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import * as i1 from '@wm/components/base';
import { Context, MessageComponent, WmComponentsModule } from '@wm/components/base';
import { DialogModule } from '@wm/components/dialogs';
import { DesignDialogModule } from '@wm/components/dialogs/design-dialog';
import { InputModule } from '@wm/components/input';
import { $appDigest } from '@wm/core';
import { FormComponent } from '@wm/components/data/form';

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
                         */
                        $appDigest();
                        this.doLogin();
                    }
                });
            });
        });
    }
    ngOnDestroy() {
        this.dialogOpenSubscription.unsubscribe();
    }
    static { this.ɵfac = function LoginDialogDirective_Factory(t) { return new (t || LoginDialogDirective)(i0.ɵɵdirectiveInject(Context, 2), i0.ɵɵdirectiveInject(i1.DialogRef, 2)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: LoginDialogDirective, selectors: [["", "wmDialog", "", "wmLoginDialog", ""]], contentQueries: function LoginDialogDirective_ContentQueries(rf, ctx, dirIndex) { if (rf & 1) {
            i0.ɵɵcontentQuery(dirIndex, MessageComponent, 5);
            i0.ɵɵcontentQuery(dirIndex, FormComponent, 4);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.msgCmp = _t.first);
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.formCmp = _t);
        } } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(LoginDialogDirective, [{
        type: Directive,
        args: [{
                selector: '[wmDialog][wmLoginDialog]'
            }]
    }], () => [{ type: Array, decorators: [{
                type: Self
            }, {
                type: Inject,
                args: [Context]
            }] }, { type: i1.DialogRef, decorators: [{
                type: Self
            }] }], { formCmp: [{
            type: ContentChildren,
            args: [FormComponent]
        }], msgCmp: [{
            type: ContentChild,
            args: [MessageComponent]
        }] }); })();

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

/**
 * Generated bundle index. Do not edit.
 */

export { LoginDialogDirective, LoginDialogModule };
//# sourceMappingURL=index.mjs.map
