import { createContext, useContext, useState, type ReactNode } from "react";

type Units = { imperial: boolean; usd: boolean; setImperial: (v: boolean) => void; setUsd: (v: boolean) => void };
const Ctx = createContext<Units | null>(null);

export function UnitsProvider({ children }: { children: ReactNode }) {
  const [imperial, setImperial] = useState(false);
  const [usd, setUsd] = useState(false);
  return <Ctx.Provider value={{ imperial, usd, setImperial, setUsd }}>{children}</Ctx.Provider>;
}

export function useUnits() {
  const c = useContext(Ctx);
  if (!c) throw new Error("UnitsProvider missing");
  return c;
}

export const AUD_USD = 0.66; // indicative rate
export const kmToMi = (km: number) => km * 0.621371;
export const mToFt = (m: number) => m * 3.28084;
export const tjToBcf = (tj: number) => tj / 1055; // 1 Bcf ≈ 1,055 TJ
