(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('@wm/components/base'), require('@wm/components/data/form'), require('@wm/components/input'), require('lodash-es')) :
    typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/common', '@wm/components/base', '@wm/components/data/form', '@wm/components/input', 'lodash-es'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.wm = global.wm || {}, global.wm.components = global.wm.components || {}, global.wm.components.advanced = global.wm.components.advanced || {}, global.wm.components.advanced.login = {}), global.ng.core, global.ng.common, global.wm.components.base, global.wm.components.data.form, global.wm.components.input, global._));
})(this, (function (exports, i0, common, i1, form, input, lodashEs) { '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);

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

    const _c0 = ["wmLogin", ""];
    const _c1 = ["*"];
    const WIDGET_INFO = { widgetType: 'wm-login', hostClass: 'app-login' };
    class LoginComponent extends i1.StylableComponent {
        static { this.initializeProps = registerProps(); }
        constructor(inj, explicitContext) {
            super(inj, WIDGET_INFO, explicitContext);
            i1.styler(this.nativeElement, this, i1.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' || lodashEs.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__namespace.ɵɵdirectiveInject(i0__namespace.Injector), i0__namespace.ɵɵdirectiveInject('EXPLICIT_CONTEXT', 8)); }; }
        static { this.ɵcmp = /*@__PURE__*/ i0__namespace.ɵɵdefineComponent({ type: LoginComponent, selectors: [["div", "wmLogin", ""]], contentQueries: function LoginComponent_ContentQueries(rf, ctx, dirIndex) { if (rf & 1) {
                i0__namespace.ɵɵcontentQuery(dirIndex, form.FormComponent, 5);
                i0__namespace.ɵɵcontentQuery(dirIndex, input.ButtonComponent, 5);
            } if (rf & 2) {
                let _t;
                i0__namespace.ɵɵqueryRefresh(_t = i0__namespace.ɵɵloadQuery()) && (ctx.formCmp = _t.first);
                i0__namespace.ɵɵqueryRefresh(_t = i0__namespace.ɵɵloadQuery()) && (ctx.buttonComponents = _t);
            } }, viewQuery: function LoginComponent_Query(rf, ctx) { if (rf & 1) {
                i0__namespace.ɵɵviewQuery(i1.MessageComponent, 7);
            } if (rf & 2) {
                let _t;
                i0__namespace.ɵɵqueryRefresh(_t = i0__namespace.ɵɵloadQuery()) && (ctx.messageCmp = _t.first);
            } }, features: [i0__namespace.ɵɵProvidersFeature([
                    i1.provideAsWidgetRef(LoginComponent)
                ]), i0__namespace.ɵɵ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__namespace.ɵɵprojectionDef();
                i0__namespace.ɵɵelement(0, "p", 1, 0);
                i0__namespace.ɵɵprojection(2);
            } }, dependencies: [i1__namespace.MessageComponent], encapsulation: 2 }); }
    }
    (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0__namespace.ɵsetClassMetadata(LoginComponent, [{
            type: i0.Component,
            args: [{ selector: 'div[wmLogin]', providers: [
                        i1.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__namespace.Injector }, { type: undefined, decorators: [{
                    type: i0.Inject,
                    args: ['EXPLICIT_CONTEXT']
                }, {
                    type: i0.Optional
                }] }], { formCmp: [{
                type: i0.ContentChild,
                args: [form.FormComponent]
            }], buttonComponents: [{
                type: i0.ContentChildren,
                args: [input.ButtonComponent, { descendants: true }]
            }], messageCmp: [{
                type: i0.ViewChild,
                args: [i1.MessageComponent, { static: true }]
            }] }); })();
    (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0__namespace.ɵ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__namespace.ɵɵdefineNgModule({ type: LoginModule }); }
        static { this.ɵinj = /*@__PURE__*/ i0__namespace.ɵɵdefineInjector({ imports: [common.CommonModule,
                i1.WmComponentsModule] }); }
    }
    (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0__namespace.ɵsetClassMetadata(LoginModule, [{
            type: i0.NgModule,
            args: [{
                    imports: [
                        common.CommonModule,
                        i1.WmComponentsModule
                    ],
                    declarations: [...components],
                    exports: [...components]
                }]
        }], null, null); })();
    (function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0__namespace.ɵɵsetNgModuleScope(LoginModule, { declarations: [LoginComponent], imports: [common.CommonModule,
            i1.WmComponentsModule], exports: [LoginComponent] }); })();

    exports.LoginComponent = LoginComponent;
    exports.LoginModule = LoginModule;

}));
