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

/**
 * Module to get a special weather event for a region. Checks if the temp, wind, and precipitation
 * allow for a special event. If so, has a 30% chance to trigger one.
 * @param options - The allowed weather for the region + season.
 * @param temperature - The current temperature.
 * @param wind - The current wind.
 * @param precipitation - The current precipitation.
 * @returns The special event, or null if there are no valid forecasts.
 */
export const getSpecial = (
  options: RegionRestriction,
  temperature: TemperatureName,
  wind: WindName,
  precipitation: PrecipitationName | undefined,
): Special | null => {
  const restrictedSpecial = specials.filter((element) => {
    return options.special.includes(element.name);
  });
  const validSpecials = precipitation !== undefined
    ? restrictedSpecial.filter(
      (element) => {
        return (element.temps.includes(temperature) || element.temps === "any")
          && (element.winds.includes(wind) || element.winds === "any")
          && (element.precipitations.includes(precipitation)
            || element.precipitations === "any");
      },
    )
    : restrictedSpecial.filter(
      (element) => {
        return (element.temps.includes(temperature) || element.temps === "any")
          && (element.winds.includes(wind) || element.winds === "any");
      },
    );

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

  const shouldSpecialTrigger = Math.floor(Math.random() * 100) < 30;

  if (!shouldSpecialTrigger) {
    return null;
  }

  return getRandomValue(validSpecials);
};
