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

/** @module
  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
}

/**
  Usage from Agency code:
  import { weather } from "std::weather"

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

  Open-Meteo (https://open-meteo.com) provides the weather data, licensed
  under CC BY 4.0. Free API usage is for non-commercial purposes only.
*/
export def weather(location: string, units: "imperial" | "metric" = "imperial"): Result {
  """
  Get current weather for a city name or zip code. Returns temperature, feels-like temperature, humidity, wind speed/direction, precipitation, cloud cover, and a text description.

  @param location - City name or zip code
  @param units - "imperial" for Fahrenheit/mph, "metric" for Celsius/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
}