/**
 * 폴리라인 레이어 (deck.gl PathLayer)
 */
import { PathLayer } from 'deck.gl';

interface PolylineOptions {
  color?: string;
  width?: number;
  opacity?: number;
  dashArray?: [number, number];
}

/** hex + opacity → [r, g, b, a] */
function hexToRgba(hex: string, opacity: number): [number, number, number, number] {
  const r = parseInt(hex.slice(1, 3), 16);
  const g = parseInt(hex.slice(3, 5), 16);
  const b = parseInt(hex.slice(5, 7), 16);
  return [r, g, b, Math.round(opacity * 255)];
}

/** PathLayer 생성: points는 [lat, lng][] */
export function createPolylineLayer(
  id: string,
  points: [number, number][],
  options: PolylineOptions = {},
) {
  const { color = '#3b82f6', width = 2, opacity = 0.8, dashArray } = options;
  const path = points.map(([lat, lng]) => [lng, lat]);

  return new PathLayer({
    id,
    data: [{ path }],
    getPath: (d: { path: [number, number][] }) => d.path,
    getColor: hexToRgba(color, opacity),
    getWidth: width * 2,
    widthUnits: 'pixels',
    ...(dashArray ? { getDashArray: dashArray, dashJustified: true } : {}),
  });
}
