import { normalizeShipName } from "./normalizeShipName";
import type { LegacyVesselInfo } from "../model/types";

export type LegacyVesselIndex = {
  byName: Map<string, LegacyVesselInfo>;
  byMmsi: Map<number, LegacyVesselInfo>;
};

function score(v: LegacyVesselInfo) {
  // Prefer records that were merged from more sources.
  return Number(!!v.sources.permittedList) + Number(!!v.sources.checklist) + Number(!!v.sources.fleet906);
}

export function buildLegacyVesselIndex(vessels: LegacyVesselInfo[]): LegacyVesselIndex {
  const byName = new Map<string, LegacyVesselInfo>();
  const byMmsi = new Map<number, LegacyVesselInfo>();

  for (const v of vessels) {
    const keys = new Set<string>();
    if (v.shipNameRoman) keys.add(normalizeShipName(v.shipNameRoman));
    if (v.shipNameCn) keys.add(normalizeShipName(v.shipNameCn));
    if (v.callSign) keys.add(normalizeShipName(v.callSign));

    for (const k of keys) {
      if (!k) continue;
      const prev = byName.get(k);
      if (!prev) {
        byName.set(k, v);
        continue;
      }
      if (score(v) > score(prev)) byName.set(k, v);
    }

    if (typeof v.mmsi === 'number' && Number.isFinite(v.mmsi)) {
      const prev = byMmsi.get(v.mmsi);
      if (!prev || score(v) > score(prev)) byMmsi.set(v.mmsi, v);
    }
    for (const m of v.mmsiList || []) {
      if (!Number.isFinite(m)) continue;
      if (byMmsi.has(m)) continue;
      byMmsi.set(m, v);
    }
  }

  return { byName, byMmsi };
}

export type LegacyMatchable = {
  mmsi?: number;
  name?: string;
  callsign?: string;
};

export function matchLegacyVessel(t: LegacyMatchable, idx: LegacyVesselIndex): LegacyVesselInfo | null {
  const mmsi = t.mmsi;
  if (typeof mmsi === "number") {
    const hit = idx.byMmsi.get(mmsi);
    if (hit) return hit;
  }
  return null;
}

