// @wuys/map · math/antimeridian.js
//
// When a polygon crosses the antimeridian (180°/-180° meridian) in a cylindrical
// projection (Equal Earth, Mercator, Robinson, …), naively connecting the projected
// points draws a horizontal streak across the entire map. We detect the jump and
// emit a new subpath instead.

/**
 * Walk projected polygon vertices, emit an SVG path `d` string with subpath breaks
 * inserted wherever consecutive points jump more than `halfW` in viewBox x.
 *
 * @param {Array<[number, number]>} points  ring of [vbx, vby] points
 * @param {number} halfW  half the viewBox width — anything wider is a jump
 * @returns {string} path data
 */
export function splitOnAntimeridian(points, halfW) {
  if (!points || points.length < 2) return "";
  let d = "";
  let prevX = null;
  for (let i = 0; i < points.length; i++) {
    const [x, y] = points[i];
    if (i === 0) {
      d += "M" + x.toFixed(2) + "," + y.toFixed(2);
    } else if (Math.abs(x - prevX) > halfW) {
      d += "Z M" + x.toFixed(2) + "," + y.toFixed(2);
    } else {
      d += "L" + x.toFixed(2) + "," + y.toFixed(2);
    }
    prevX = x;
  }
  d += "Z";
  return d;
}
