/**
 * EEZ / NLL 경계선 레이어 (deck.gl PathLayer)
 */
import { PathLayer } from 'deck.gl';
import { EEZ_BOUNDARY, NLL_LINE, EEZ_STYLE, NLL_STYLE } from '../constants';

/** [lat, lng][] → PathLayer 경로 [lng, lat][] */
function toPath(points: readonly (readonly [number, number])[]): [number, number][] {
  return points.map(([lat, lng]) => [lng, lat]);
}

export function createEEZLayer(id = 'eez-boundary') {
  return new PathLayer({
    id,
    data: [{ path: toPath(EEZ_BOUNDARY) }],
    getPath: (d: { path: [number, number][] }) => d.path,
    getColor: hexToRgba(EEZ_STYLE.color, EEZ_STYLE.opacity),
    getWidth: EEZ_STYLE.weight * 2,
    widthUnits: 'pixels',
    getDashArray: [6, 4],
    dashJustified: true,
    extensions: [],
  });
}

export function createNLLLayer(id = 'nll-line') {
  return new PathLayer({
    id,
    data: [{ path: toPath(NLL_LINE) }],
    getPath: (d: { path: [number, number][] }) => d.path,
    getColor: hexToRgba(NLL_STYLE.color, NLL_STYLE.opacity),
    getWidth: NLL_STYLE.weight * 2,
    widthUnits: 'pixels',
    getDashArray: [8, 4],
    dashJustified: true,
    extensions: [],
  });
}

/** 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)];
}
