import { Injectable } from '@angular/core';
import { Observable, map } from 'rxjs';
import { PageRendererService } from '../../dynamic-renderer/page-renderer/page-renderer.service';
import { PageRenderModel } from '../../dynamic-renderer/page-renderer/page-renderer.model';
import { PageResolverService } from '../../dynamic-renderer/page-resolver/page-resolver.service';

@Injectable({ providedIn: 'root' })
export class WebsiteRuntimeFacade {
  constructor(
    private readonly pageResolver: PageResolverService,
    private readonly pageRenderer: PageRendererService
  ) {}

  getPageRenderModelForUrl(url: string): Observable<PageRenderModel | null> {
    return this.pageResolver.resolveByUrl(url).pipe(
      map(page => {
        return page ? this.pageRenderer.toRenderModel(page) : null;
      })
    );
  }
}
