{"version":3,"file":"distance.cjs","names":[],"sources":["../../src/geo/distance.ts"],"sourcesContent":["import type { Coordinate } from \"./types\";\n\n/**\n * Mean Earth radius in kilometers. Same value used by `tempest-fastapi-sdk`\n * (`geo/distance.py`) so client and server distances agree.\n */\nexport const EARTH_RADIUS_KM = 6371.0088;\n\n/** Convert degrees to radians. */\nexport function toRadians(degrees: number): number {\n    return (degrees * Math.PI) / 180;\n}\n\n/**\n * Great-circle distance between two coordinates using the haversine formula\n * (spherical Earth). Mirrors `haversine_km` from `tempest-fastapi-sdk`.\n *\n * @param origin - Start coordinate.\n * @param destination - End coordinate.\n * @returns Distance in kilometers (`>= 0`).\n *\n * @example\n * haversineKm(\n *   { latitude: -23.5505, longitude: -46.6333 }, // São Paulo\n *   { latitude: -22.9068, longitude: -43.1729 }, // Rio de Janeiro\n * ); // ≈ 360.9\n */\nexport function haversineKm(origin: Coordinate, destination: Coordinate): number {\n    const lat1 = toRadians(origin.latitude);\n    const lat2 = toRadians(destination.latitude);\n    const deltaLat = toRadians(destination.latitude - origin.latitude);\n    const deltaLon = toRadians(destination.longitude - origin.longitude);\n\n    const a =\n        Math.sin(deltaLat / 2) ** 2 + Math.cos(lat1) * Math.cos(lat2) * Math.sin(deltaLon / 2) ** 2;\n\n    return 2 * EARTH_RADIUS_KM * Math.asin(Math.sqrt(a));\n}\n\n/**\n * Total length of a trajectory: the sum of haversine distances between each\n * consecutive pair of points. Returns `0` for an empty or single-point path.\n *\n * @param points - Ordered coordinates along the path.\n * @returns Cumulative distance in kilometers.\n */\nexport function pathLengthKm(points: readonly Coordinate[]): number {\n    let total = 0;\n    for (let i = 1; i < points.length; i += 1) {\n        total += haversineKm(points[i - 1], points[i]);\n    }\n    return total;\n}\n\n/**\n * Initial bearing (forward azimuth) from `origin` to `destination`, in degrees\n * clockwise from true north, normalized to `[0, 360)`. Useful for orienting a\n * heading arrow on a trajectory.\n *\n * @param origin - Start coordinate.\n * @param destination - End coordinate.\n * @returns Bearing in degrees, `[0, 360)`.\n */\nexport function bearingDeg(origin: Coordinate, destination: Coordinate): number {\n    const lat1 = toRadians(origin.latitude);\n    const lat2 = toRadians(destination.latitude);\n    const deltaLon = toRadians(destination.longitude - origin.longitude);\n\n    const y = Math.sin(deltaLon) * Math.cos(lat2);\n    const x =\n        Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(deltaLon);\n\n    const bearing = (Math.atan2(y, x) * 180) / Math.PI;\n    return (bearing + 360) % 360;\n}\n"],"mappings":"AAMA,IAAa,EAAkB,UAG/B,SAAgB,EAAU,EAAyB,CAC/C,OAAQ,EAAU,KAAK,GAAM,GACjC,CAgBA,SAAgB,EAAY,EAAoB,EAAiC,CAC7E,IAAM,EAAO,EAAU,EAAO,QAAQ,EAChC,EAAO,EAAU,EAAY,QAAQ,EACrC,EAAW,EAAU,EAAY,SAAW,EAAO,QAAQ,EAC3D,EAAW,EAAU,EAAY,UAAY,EAAO,SAAS,EAE7D,EACF,KAAK,IAAI,EAAW,CAAC,GAAK,EAAI,KAAK,IAAI,CAAI,EAAI,KAAK,IAAI,CAAI,EAAI,KAAK,IAAI,EAAW,CAAC,GAAK,EAE9F,MAAO,GAAI,EAAkB,KAAK,KAAK,KAAK,KAAK,CAAC,CAAC,CACvD,CASA,SAAgB,EAAa,EAAuC,CAChE,IAAI,EAAQ,EACZ,IAAK,IAAI,EAAI,EAAG,EAAI,EAAO,OAAQ,GAAK,EACpC,GAAS,EAAY,EAAO,EAAI,GAAI,EAAO,EAAE,EAEjD,OAAO,CACX,CAWA,SAAgB,EAAW,EAAoB,EAAiC,CAC5E,IAAM,EAAO,EAAU,EAAO,QAAQ,EAChC,EAAO,EAAU,EAAY,QAAQ,EACrC,EAAW,EAAU,EAAY,UAAY,EAAO,SAAS,EAE7D,EAAI,KAAK,IAAI,CAAQ,EAAI,KAAK,IAAI,CAAI,EACtC,EACF,KAAK,IAAI,CAAI,EAAI,KAAK,IAAI,CAAI,EAAI,KAAK,IAAI,CAAI,EAAI,KAAK,IAAI,CAAI,EAAI,KAAK,IAAI,CAAQ,EAGzF,OADiB,KAAK,MAAM,EAAG,CAAC,EAAI,IAAO,KAAK,GAC9B,KAAO,GAC7B"}