export function makeMmsiPairHighlightExpr(aField: string, bField: string, hoveredMmsiList: number[]) {
  if (!Array.isArray(hoveredMmsiList) || hoveredMmsiList.length < 2) {
    return false;
  }
  const inA = ['in', ['to-number', ['get', aField]], ['literal', hoveredMmsiList]] as unknown[];
  const inB = ['in', ['to-number', ['get', bField]], ['literal', hoveredMmsiList]] as unknown[];
  return ['all', inA, inB] as unknown[];
}

export function makeMmsiAnyEndpointExpr(aField: string, bField: string, hoveredMmsiList: number[]) {
  if (!Array.isArray(hoveredMmsiList) || hoveredMmsiList.length === 0) {
    return false;
  }
  const literal = ['literal', hoveredMmsiList] as unknown[];
  return [
    'any',
    ['in', ['to-number', ['get', aField]], literal],
    ['in', ['to-number', ['get', bField]], literal],
  ] as unknown[];
}

export function makeFleetOwnerMatchExpr(hoveredOwnerKeys: string[]) {
  if (!Array.isArray(hoveredOwnerKeys) || hoveredOwnerKeys.length === 0) {
    return false;
  }
  const expr = ['match', ['to-string', ['coalesce', ['get', 'ownerKey'], '']]] as unknown[];
  for (const ownerKey of hoveredOwnerKeys) {
    expr.push(String(ownerKey), true);
  }
  expr.push(false);
  return expr;
}

export function makeFleetMemberMatchExpr(hoveredFleetMmsiList: number[]) {
  if (!Array.isArray(hoveredFleetMmsiList) || hoveredFleetMmsiList.length === 0) {
    return false;
  }
  const clauses = hoveredFleetMmsiList.map((mmsi) =>
    ['in', mmsi, ['coalesce', ['get', 'vesselMmsis'], ['literal', []]]] as unknown[],
  );
  return ['any', ...clauses] as unknown[];
}

// ── Globe circle radius zoom stops ──
// MapLibre 제약: expression 당 zoom-based interpolate는 1개만 허용
// → 하나의 interpolate 안에서 각 stop 값을 case로 분기
const ZOOM_LEVELS = [3, 7, 10, 14, 18] as const;
const BASE_VALUES = [4, 6, 8, 12, 32] as const;
const SELECTED_VALUES = [4.6, 6.8, 9.0, 13.5, 36] as const;
const HIGHLIGHTED_VALUES = [4.2, 6.2, 8.2, 12.6, 34] as const;

function buildStopsWithCase(getter: (key: string) => unknown[]) {
  const stops: unknown[] = ['interpolate', ['linear'], ['zoom']];
  for (let i = 0; i < ZOOM_LEVELS.length; i++) {
    stops.push(ZOOM_LEVELS[i]);
    stops.push([
      'case',
      ['==', getter('selected'), 1], SELECTED_VALUES[i],
      ['==', getter('highlighted'), 1], HIGHLIGHTED_VALUES[i],
      BASE_VALUES[i],
    ]);
  }
  return stops;
}

/** feature-state 기반 — 메인 선박 레이어 (halo, outline) */
export function makeGlobeCircleRadiusExpr() {
  return buildStopsWithCase((key) => ['feature-state', key]);
}

/** GeoJSON property 기반 — hover overlay 레이어 */
export function makeGlobeCircleRadiusPropExpr() {
  const stops: unknown[] = ['interpolate', ['linear'], ['zoom']];
  for (let i = 0; i < ZOOM_LEVELS.length; i++) {
    stops.push(ZOOM_LEVELS[i]);
    stops.push([
      'case',
      ['==', ['get', 'selected'], 1], SELECTED_VALUES[i],
      HIGHLIGHTED_VALUES[i],
    ]);
  }
  return stops;
}

export const GLOBE_SHIP_CIRCLE_RADIUS_EXPR = makeGlobeCircleRadiusExpr() as never;
export const GLOBE_SHIP_CIRCLE_RADIUS_PROP_EXPR = makeGlobeCircleRadiusPropExpr() as never;
