import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
import { IScheduleRequestData } from '@schedule/types';
import { ScheduleRequestParserService } from '@schedule/services/request/request-parser.service';

@Injectable()
export class ScheduleRequestDataResolver
    implements Resolve<IScheduleRequestData | undefined>
{
    constructor(private requestService: ScheduleRequestParserService) {}

    resolve(route: ActivatedRouteSnapshot): IScheduleRequestData | undefined {
        const { queryParams } = route;

        return (
            queryParams.request && {
                raw: queryParams.request,
                parsed: this.requestService.parseRouteRequest(
                    queryParams.request,
                ),
            }
        );
    }
}
