/**
 * @copyright nhcarrigan
 * @license Naomi's Public License
 * @author Naomi Carrigan
 */
import { precipitations } from "../../data/weather/precipitations.js";
import { getRandomValue } from "../getRandomValue.js";
import type { TemperatureName }
  from "../../interfaces/weather/names/temperatureName.js";
import type { WindName } from "../../interfaces/weather/names/windName.js";
import type { Precipitation }
  from "../../interfaces/weather/precipitation.js";
import type { RegionRestriction } from "../../interfaces/weather/regions/regionRestriction.js";

/**
 * Get a precipitation forecast for the region and season.
 * @param options - The allowed weather for the region + season.
 * @param temperature - The current temperature.
 * @param wind - The current wind.
 * @returns The precipitation, or null if there are no valid forecasts.
 */
export const getPrecipitation = (
  options: RegionRestriction,
  temperature: TemperatureName,
  wind: WindName,
): Precipitation | null => {
  const restrictedPrecipitation = precipitations.filter((element) => {
    return options.precipitation.includes(element.name);
  });
  const validPrecipitations = restrictedPrecipitation.filter(
    (element) => {
      return (element.temps.includes(temperature) || element.temps === "any")
      && (element.winds.includes(wind) || element.winds === "any");
    },
  );

  if (validPrecipitations.length === 0) {
    return null;
  }

  return getRandomValue(validPrecipitations);
};
