import * as i0 from '@angular/core';
import { Component, Inject, Optional, ContentChild, ContentChildren, ViewChild, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import * as i1 from '@wm/components/base';
import { PROP_STRING, PROP_ANY, register, StylableComponent, APPLY_STYLES_TYPE, styler, MessageComponent, provideAsWidgetRef, WmComponentsModule } from '@wm/components/base';
import { FormComponent } from '@wm/components/data/form';
import { ButtonComponent } from '@wm/components/input';
import { includes } from 'lodash-es';

const registerProps = () => {
    register('wm-login', new Map([
        ['errormessage', PROP_STRING],
        ['eventsource', PROP_ANY]
    ]));
};

const _c0 = ["wmLogin", ""];
const _c1 = ["*"];
const WIDGET_INFO = { widgetType: 'wm-login', hostClass: 'app-login' };
class LoginComponent extends StylableComponent {
    static { this.initializeProps = registerProps(); }
    constructor(inj, explicitContext) {
        super(inj, WIDGET_INFO, explicitContext);
        styler(this.nativeElement, this, APPLY_STYLES_TYPE.CONTAINER);
    }
    onSuccessCB() {
        this.invokeEventCallback('success');
    }
    onErrorCB(error) {
        this.loginMessage = {
            type: 'error',
            caption: this.errormessage || error || this.appLocale.LABEL_INVALID_USERNAME_OR_PASSWORD,
            show: true
        };
        this.messageCmp.showMessage(this.loginMessage.caption, this.loginMessage.type);
        this.invokeEventCallback('error');
    }
    getLoginDetails() {
        return this.formCmp.dataoutput;
    }
    doLogin() {
        if (this.eventsource) {
            this.formCmp.ngform.valid && this.eventsource.invoke({ loginInfo: this.getLoginDetails() }, this.onSuccessCB.bind(this), this.onErrorCB.bind(this));
        }
        else {
            console.warn('Default action "loginAction" does not exist. Either create the Action or assign an event to onSubmit of the login widget');
        }
    }
    initLoginButtonActions() {
        this.loginBtnCmp.getNativeElement().addEventListener('click', event => {
            // if no event is attached to the onSubmit of login widget or loginButton inside it, invoke default login action
            if (!this.nativeElement.hasAttribute('submit.event') && !this.loginBtnCmp.getNativeElement().hasAttribute('click.event') && !this.loginBtnCmp.getNativeElement().hasAttribute('tap.event')) {
                this.doLogin();
            }
        });
    }
    ngAfterViewInit() {
        super.ngAfterViewInit();
        // suppresses the default form submission (in browsers like Firefox)
        this.formCmp.getNativeElement().addEventListener('submit', e => e.preventDefault());
        // get login button component
        this.buttonComponents.forEach(cmp => {
            if (cmp.getNativeElement().getAttribute('name') === 'loginButton' || includes(cmp.getNativeElement().classList, 'app-login-button')) {
                if (this.loginBtnCmp) {
                    return;
                }
                this.loginBtnCmp = cmp;
                this.initLoginButtonActions();
            }
        });
    }
    static { this.ɵfac = function LoginComponent_Factory(t) { return new (t || LoginComponent)(i0.ɵɵdirectiveInject(i0.Injector), i0.ɵɵdirectiveInject('EXPLICIT_CONTEXT', 8)); }; }
    static { this.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: LoginComponent, selectors: [["div", "wmLogin", ""]], contentQueries: function LoginComponent_ContentQueries(rf, ctx, dirIndex) { if (rf & 1) {
            i0.ɵɵcontentQuery(dirIndex, FormComponent, 5);
            i0.ɵɵcontentQuery(dirIndex, ButtonComponent, 5);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.formCmp = _t.first);
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.buttonComponents = _t);
        } }, viewQuery: function LoginComponent_Query(rf, ctx) { if (rf & 1) {
            i0.ɵɵviewQuery(MessageComponent, 7);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.messageCmp = _t.first);
        } }, features: [i0.ɵɵProvidersFeature([
                provideAsWidgetRef(LoginComponent)
            ]), i0.ɵɵInheritDefinitionFeature], attrs: _c0, ngContentSelectors: _c1, decls: 3, vars: 0, consts: [["message", ""], ["wmMessage", "", "show.bind", "loginMessage.show", "caption.bind", "loginMessage.caption", "type.bind", "loginMessage.type", 1, "app-login-message"]], template: function LoginComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵprojectionDef();
            i0.ɵɵelement(0, "p", 1, 0);
            i0.ɵɵprojection(2);
        } }, dependencies: [i1.MessageComponent], encapsulation: 2 }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(LoginComponent, [{
        type: Component,
        args: [{ selector: 'div[wmLogin]', providers: [
                    provideAsWidgetRef(LoginComponent)
                ], template: "<p wmMessage class=\"app-login-message\" #message show.bind=\"loginMessage.show\" caption.bind=\"loginMessage.caption\" type.bind=\"loginMessage.type\"></p>\n<ng-content></ng-content>\n" }]
    }], () => [{ type: i0.Injector }, { type: undefined, decorators: [{
                type: Inject,
                args: ['EXPLICIT_CONTEXT']
            }, {
                type: Optional
            }] }], { formCmp: [{
            type: ContentChild,
            args: [FormComponent]
        }], buttonComponents: [{
            type: ContentChildren,
            args: [ButtonComponent, { descendants: true }]
        }], messageCmp: [{
            type: ViewChild,
            args: [MessageComponent, { static: true }]
        }] }); })();
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(LoginComponent, { className: "LoginComponent", filePath: "login.component.ts", lineNumber: 29 }); })();

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

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

export { LoginComponent, LoginModule };
//# sourceMappingURL=index.mjs.map
