import { Observable } from 'rxjs/Observable';
import { LocalStorageService, SessionStorageService } from 'ngx-webstorage';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { SERVER_API_URL } from '../../app.constants';
import {ActivatedRoute, Router} from "@angular/router";
import {NzMessageService} from 'ng-zorro-antd';

export class AuthInterceptor implements HttpInterceptor {

    constructor(
        private localStorage: LocalStorageService,
        private sessionStorage: SessionStorageService,
        public router: Router,private message:NzMessageService,
    ) {
    }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        if (!request || !request.url || (/^http/.test(request.url) && !(SERVER_API_URL && request.url.startsWith(SERVER_API_URL)))) {
            return next.handle(request);
        }

        const token = this.localStorage.retrieve('accessToken') || this.sessionStorage.retrieve('accessToken');
        const moduleId = this.localStorage.retrieve("moduleId") || "";
        if (!!token) {
            request = request.clone({
                setHeaders: {
                    accessToken: token,
                    moduleId:moduleId
                }
            });
        }
        return next.handle(request);
    }

}
