import { _weather } from "agency-lang/stdlib-lib/weather.js"

/** @module
  @summary Look up current weather and convert temperatures.
  Look up current weather for a city or zip code, and convert between
  Celsius and Fahrenheit. No API key required.

  ```ts
  import { weather } from "std::weather"

  node main() {
    const w = weather("San Francisco")
    print(w)
  }
  ```

  Weather data comes from Open-Meteo (https://open-meteo.com), licensed under
  CC BY 4.0, for non-commercial use.
*/

effect std::weather { location: string, units: string }

type WeatherResult = {
  location: string;
  country: string;
  latitude: number;
  longitude: number;
  temperature: number;
  feelsLike: number;
  humidity: number;
  description: string;
  windSpeed: number;
  windDirection: number;
  precipitation: number;
  cloudCover: number;
  units: string
}

/** Include a region or country in a city name when the location is ambiguous. */
@capabilities(network)
export def weather(location: string, units: "imperial" | "metric" = "imperial"): Result raises <std::weather> {
  """
  Get current weather for a city or zip code.

  @param location - A city name, optionally with a region or country, or a zip code.
  @param units - Use imperial for Fahrenheit and mph, or metric for Celsius and km/h.
  """
  return interrupt std::weather("Are you sure you want to fetch the weather from Open-Meteo?", {
    location: location,
    units: units
  })

  return try _weather(location, units)
}

export def celsiusToFahrenheit(celsius: number): number {
  """Convert a temperature from Celsius to Fahrenheit."""
  return celsius * 9 / 5 + 32
}

export def fahrenheitToCelsius(fahrenheit: number): number {
  """Convert a temperature from Fahrenheit to Celsius."""
  let diff = fahrenheit - 32
  return diff * 5 / 9
}