import * as i0 from '@angular/core';
import { Component, Injectable, NgModule } from '@angular/core';
import * as i1 from '@angular/router';
import { Router, RouterModule } from '@angular/router';
import * as i2 from '@ishop/admin-ui/core';
import { ADMIN_UI_VERSION, getAppConfig, AUTH_REDIRECT_PARAM, AuthService, SharedModule } from '@ishop/admin-ui/core';
import { map } from 'rxjs/operators';

class LoginComponent {
    constructor(authService, router) {
        this.authService = authService;
        this.router = router;
        this.username = '';
        this.password = '';
        this.rememberMe = false;
        this.version = ADMIN_UI_VERSION;
        this.brand = getAppConfig().brand;
        this.hideVendureBranding = getAppConfig().hideVendureBranding;
        this.hideVersion = getAppConfig().hideVersion;
    }
    logIn() {
        this.errorMessage = undefined;
        this.authService.logIn(this.username, this.password, this.rememberMe).subscribe(result => {
            switch (result.__typename) {
                case 'CurrentUser':
                    const redirect = this.getRedirectRoute();
                    this.router.navigateByUrl(redirect ? redirect : '/');
                    break;
                case 'InvalidCredentialsError':
                case 'NativeAuthStrategyError':
                    this.errorMessage = result.message;
                    break;
            }
        });
    }
    /**
     * Attempts to read a redirect param from the current url and parse it into a
     * route from which the user was redirected after a 401 error.
     */
    getRedirectRoute() {
        let redirectTo;
        const re = new RegExp(`${AUTH_REDIRECT_PARAM}=(.*)`);
        try {
            const redirectToParam = window.location.search.match(re);
            if (redirectToParam && 1 < redirectToParam.length) {
                redirectTo = atob(decodeURIComponent(redirectToParam[1]));
            }
        }
        catch (e) {
            // ignore
        }
        return redirectTo;
    }
}
LoginComponent.decorators = [
    { type: Component, args: [{
                selector: 'vdr-login',
                template: "<div class=\"login-wrapper\">\r\n    <form class=\"login\">\r\n        <label class=\"title\"><img src=\"assets/logo-300px.png\" /></label>\r\n        <div class=\"login-group\">\r\n            <input\r\n                class=\"username\"\r\n                type=\"text\"\r\n                name=\"username\"\r\n                id=\"login_username\"\r\n                [(ngModel)]=\"username\"\r\n                [placeholder]=\"'common.username' | translate\"\r\n            />\r\n            <input\r\n                class=\"password\"\r\n                name=\"password\"\r\n                type=\"password\"\r\n                id=\"login_password\"\r\n                [(ngModel)]=\"password\"\r\n                [placeholder]=\"'common.password' | translate\"\r\n            />\r\n            <clr-alert [clrAlertType]=\"'danger'\"  [clrAlertClosable]=\"false\" [class.visible]=\"errorMessage\" class=\"login-error\">\r\n                <clr-alert-item>\r\n                    <span class=\"alert-text\">\r\n                        {{ errorMessage }}\r\n                    </span>\r\n                </clr-alert-item>\r\n            </clr-alert>\r\n            <clr-checkbox-wrapper>\r\n                <input\r\n                    type=\"checkbox\"\r\n                    clrCheckbox\r\n                    id=\"rememberme\"\r\n                    name=\"rememberme\"\r\n                    [(ngModel)]=\"rememberMe\"\r\n                />\r\n                <label>{{ 'common.remember-me' | translate }}</label>\r\n            </clr-checkbox-wrapper>\r\n            <button\r\n                type=\"submit\"\r\n                class=\"btn btn-primary\"\r\n                (click)=\"logIn()\"\r\n                [disabled]=\"!username || !password\"\r\n            >\r\n                {{ 'common.login' | translate }}\r\n            </button>\r\n        </div>\r\n        <div class=\"version\">\r\n            <span *ngIf=\"brand\">{{ brand }} <span *ngIf=\"!hideVendureBranding || !hideVersion\">-</span></span>\r\n            <span *ngIf=\"!hideVendureBranding\">vendure</span>\r\n            <span *ngIf=\"!hideVersion\">v{{ version }}</span>\r\n        </div>\r\n    </form>\r\n</div>\r\n",
                styles: [".login-wrapper{background:var(--login-page-bg);background-size:auto;justify-content:center}.title{text-align:center}.version{flex:1;display:flex;align-items:flex-end;justify-content:center;color:var(--color-grey-300)}.version span+span{margin-left:5px}.login-error{max-height:0;overflow:hidden}.login-error.visible{max-height:46px;transition:max-height .2s;-webkit-animation:shake .82s cubic-bezier(.36,.07,.19,.97) both;animation:shake .82s cubic-bezier(.36,.07,.19,.97) both;-webkit-animation-delay:.2s;animation-delay:.2s;transform:translateZ(0);-webkit-backface-visibility:hidden;backface-visibility:hidden;perspective:1000px}@-webkit-keyframes shake{10%,90%{transform:translate3d(-1px,0,0)}20%,80%{transform:translate3d(2px,0,0)}30%,50%,70%{transform:translate3d(-4px,0,0)}40%,60%{transform:translate3d(4px,0,0)}}@keyframes shake{10%,90%{transform:translate3d(-1px,0,0)}20%,80%{transform:translate3d(2px,0,0)}30%,50%,70%{transform:translate3d(-4px,0,0)}40%,60%{transform:translate3d(4px,0,0)}}"]
            },] }
];
LoginComponent.ctorParameters = () => [
    { type: AuthService },
    { type: Router }
];

/**
 * This guard prevents loggen-in users from navigating to the login screen.
 */
class LoginGuard {
    constructor(router, authService) {
        this.router = router;
        this.authService = authService;
    }
    canActivate(route) {
        return this.authService.checkAuthenticatedStatus().pipe(map(authenticated => {
            if (authenticated) {
                this.router.navigate(['/']);
            }
            return !authenticated;
        }));
    }
}
LoginGuard.ɵprov = i0.ɵɵdefineInjectable({ factory: function LoginGuard_Factory() { return new LoginGuard(i0.ɵɵinject(i1.Router), i0.ɵɵinject(i2.AuthService)); }, token: LoginGuard, providedIn: "root" });
LoginGuard.decorators = [
    { type: Injectable, args: [{
                providedIn: 'root',
            },] }
];
LoginGuard.ctorParameters = () => [
    { type: Router },
    { type: AuthService }
];

const loginRoutes = [
    {
        path: '',
        component: LoginComponent,
        pathMatch: 'full',
        canActivate: [LoginGuard],
    },
];

class LoginModule {
}
LoginModule.decorators = [
    { type: NgModule, args: [{
                imports: [SharedModule, RouterModule.forChild(loginRoutes)],
                exports: [],
                declarations: [LoginComponent],
            },] }
];

// This file was generated by the build-public-api.ts script

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

export { LoginComponent, LoginGuard, LoginModule, loginRoutes };
//# sourceMappingURL=ishop-admin-ui-login.js.map
