import Location from "../entities/location";
import LocationsRepository from "../entities/locationsRepository";
import WeatherService, { Forecast } from "../entities/weatherService";
import Presenter from "./presenter";

export type GetForecastsResult = {
  location: Location;
  forecast: Forecast;
};

export default async function getForecasts(
  presenter: Presenter<GetForecastsResult[]>,
  repository: LocationsRepository,
  weatherService: WeatherService
) {
  const locations = repository.getAll();
  const forecasts = await Promise.all(
    locations.map(async (location) => {
      const forecast = await weatherService.getForecastFor(
        location.lat,
        location.lon
      );

      return {
        location,
        forecast,
      };
    })
  );

  presenter.show(forecasts);
}
