File size: 25,995 Bytes
fa9c65f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/**
 * Infrastructure cascade core — "what if corridor X fails".
 *
 * Builds a dependency graph over undersea cables, pipelines, ports, maritime
 * chokepoints and the countries they serve, then walks it breadth-first to
 * estimate how a single failure propagates.
 *
 * Dependency-free by design (no src/ imports, no DOM, no i18n) so it bundles
 * under Edge/esbuild for server-side tools. The dashboard reaches it through
 * src/services/infrastructure-cascade.ts, which supplies the lazily-loaded
 * cable table and the strategic-waterway list.
 */

import { PIPELINES } from './pipelines-data';
import { PORTS } from './ports-data';
import { haversineKm } from './geo-distance';
import type { PipelineRecord } from './pipelines-data';
import type { PortRecord } from './ports-data';

export type InfrastructureNodeType = 'cable' | 'pipeline' | 'port' | 'chokepoint' | 'country' | 'route';

export interface InfrastructureNode {
  id: string;
  type: InfrastructureNodeType;
  name: string;
  coordinates?: [number, number];
  metadata?: Record<string, unknown>;
}

export type DependencyType =
  | 'serves'
  | 'terminates_at'
  | 'transits_through'
  | 'lands_at'
  | 'depends_on'
  | 'shares_risk'
  | 'alternative_to'
  | 'trade_route'
  | 'controls_access'
  | 'trade_dependency';

export interface DependencyEdge {
  from: string;
  to: string;
  type: DependencyType;
  strength: number;       // 0-1 criticality
  redundancy?: number;    // 0-1 how replaceable
  metadata?: {
    capacityShare?: number;
    alternativeRoutes?: number;
    estimatedImpact?: string;
    portType?: string;
    relationship?: string;
  };
}

export type CascadeImpactLevel = 'critical' | 'high' | 'medium' | 'low';

export interface CascadeAffectedNode {
  node: InfrastructureNode;
  impactLevel: CascadeImpactLevel;
  pathLength: number;
  dependencyChain: string[];
  redundancyAvailable: boolean;
  estimatedRecovery?: string;
}

export interface CascadeCountryImpact {
  country: string;
  countryName: string;
  impactLevel: CascadeImpactLevel;
  affectedCapacity: number;
  criticalSectors?: string[];
}

export interface CascadeResult {
  source: InfrastructureNode;
  affectedNodes: CascadeAffectedNode[];
  countriesAffected: CascadeCountryImpact[];
  economicImpact?: {
    dailyTradeLoss?: number;
    affectedThroughput?: number;
  };
  redundancies?: {
    id: string;
    name: string;
    capacityShare: number;
  }[];
}

/** Structural shape of an undersea cable — mirrors the client `UnderseaCable`. */
export interface CableInput {
  id: string;
  name: string;
  points?: [number, number][];
  landingPoints?: { country: string; countryName?: string; city?: string; lat?: number; lon?: number }[];
  countriesServed?: { country: string; capacityShare: number; isRedundant?: boolean }[];
  capacityTbps?: number;
  rfsYear?: number;
  owners?: string[];
}

/** Structural shape of a maritime chokepoint — mirrors `StrategicWaterway`. */
export interface WaterwayInput {
  id: string;
  name: string;
  lat: number;
  lon: number;
  description?: string;
}

export type PipelineInput = PipelineRecord;
export type PortInput = PortRecord;

export interface CascadeGraphInputs {
  cables: CableInput[];
  waterways: WaterwayInput[];
  pipelines?: PipelineInput[];
  ports?: PortInput[];
}

export interface DependencyGraph {
  nodes: Map<string, InfrastructureNode>;
  edges: DependencyEdge[];
  outgoing: Map<string, DependencyEdge[]>;
  incoming: Map<string, DependencyEdge[]>;
  /** The cable table the graph was built from — cascade scoring reads it back. */
  cables: CableInput[];
}

// Country name lookup
const COUNTRY_NAMES: Record<string, string> = {
  US: 'United States', GB: 'United Kingdom', ES: 'Spain', FR: 'France',
  DE: 'Germany', IT: 'Italy', PT: 'Portugal', NO: 'Norway', DK: 'Denmark',
  NL: 'Netherlands', BE: 'Belgium', SE: 'Sweden', FI: 'Finland', IE: 'Ireland',
  AT: 'Austria', CH: 'Switzerland', GR: 'Greece', CZ: 'Czech Republic',
  JP: 'Japan', CN: 'China', TW: 'Taiwan', HK: 'Hong Kong', SG: 'Singapore',
  KR: 'South Korea', AU: 'Australia', NZ: 'New Zealand', IN: 'India', PK: 'Pakistan',
  AE: 'UAE', SA: 'Saudi Arabia', EG: 'Egypt', KW: 'Kuwait', BH: 'Bahrain',
  OM: 'Oman', QA: 'Qatar', IR: 'Iran', IQ: 'Iraq', TR: 'Turkey', IL: 'Israel',
  JO: 'Jordan', LB: 'Lebanon', SY: 'Syria', YE: 'Yemen',
  NG: 'Nigeria', ZA: 'South Africa', KE: 'Kenya', TZ: 'Tanzania',
  MZ: 'Mozambique', MG: 'Madagascar', SN: 'Senegal', GH: 'Ghana',
  CI: 'Ivory Coast', AO: 'Angola', ET: 'Ethiopia', UG: 'Uganda',
  BR: 'Brazil', AR: 'Argentina', CL: 'Chile',
  PE: 'Peru', CO: 'Colombia', MX: 'Mexico', PA: 'Panama', VE: 'Venezuela',
  IS: 'Iceland', FO: 'Faroe Islands', FJ: 'Fiji', ID: 'Indonesia',
  VN: 'Vietnam', TH: 'Thailand', MY: 'Malaysia', PH: 'Philippines',
  RU: 'Russia', UA: 'Ukraine', PL: 'Poland', RO: 'Romania', HU: 'Hungary',
  CA: 'Canada', DJ: 'Djibouti', BD: 'Bangladesh', LK: 'Sri Lanka', MM: 'Myanmar',
};

function addCablesAsNodes(graph: DependencyGraph, cables: CableInput[]): void {
  for (const cable of cables) {
    const firstPoint = cable.points?.[0];
    graph.nodes.set(`cable:${cable.id}`, {
      id: `cable:${cable.id}`,
      type: 'cable',
      name: cable.name,
      coordinates: firstPoint ? [firstPoint[0], firstPoint[1]] : undefined,
      metadata: {
        capacityTbps: cable.capacityTbps,
        rfsYear: cable.rfsYear,
        owners: cable.owners,
        landingPoints: cable.landingPoints,
      },
    });
  }
}

function addPipelinesAsNodes(graph: DependencyGraph, pipelines: PipelineInput[]): void {
  for (const pipeline of pipelines) {
    const firstPoint = pipeline.points?.[0];
    graph.nodes.set(`pipeline:${pipeline.id}`, {
      id: `pipeline:${pipeline.id}`,
      type: 'pipeline',
      name: pipeline.name,
      coordinates: firstPoint ? [firstPoint[0], firstPoint[1]] : undefined,
      metadata: {
        type: pipeline.type,
        status: pipeline.status,
        capacity: pipeline.capacity,
        operator: pipeline.operator,
        countries: pipeline.countries,
      },
    });
  }
}

function addPortsAsNodes(graph: DependencyGraph, ports: PortInput[]): void {
  for (const port of ports) {
    graph.nodes.set(`port:${port.id}`, {
      id: `port:${port.id}`,
      type: 'port',
      name: port.name,
      coordinates: [port.lon, port.lat],
      metadata: {
        country: port.country,
        type: port.type,
        rank: port.rank,
      },
    });
  }
}

function addChokepointsAsNodes(graph: DependencyGraph, waterways: WaterwayInput[]): void {
  for (const waterway of waterways) {
    graph.nodes.set(`chokepoint:${waterway.id}`, {
      id: `chokepoint:${waterway.id}`,
      type: 'chokepoint',
      name: waterway.name,
      coordinates: [waterway.lon, waterway.lat],
      metadata: {
        description: waterway.description,
      },
    });
  }
}

function addCountriesAsNodes(
  graph: DependencyGraph,
  cables: CableInput[],
  pipelines: PipelineInput[],
): void {
  const countries = new Set<string>();

  for (const cable of cables) {
    cable.countriesServed?.forEach(c => countries.add(c.country));
    cable.landingPoints?.forEach(lp => countries.add(lp.country));
  }

  for (const pipeline of pipelines) {
    pipeline.countries?.forEach(c => {
      const code = c === 'USA' ? 'US' : c === 'Canada' ? 'CA' : c;
      countries.add(code);
    });
  }

  for (const code of countries) {
    graph.nodes.set(`country:${code}`, {
      id: `country:${code}`,
      type: 'country',
      name: COUNTRY_NAMES[code] || code,
      metadata: { code },
    });
  }
}

function addEdge(graph: DependencyGraph, edge: DependencyEdge): void {
  graph.edges.push(edge);

  if (!graph.outgoing.has(edge.from)) graph.outgoing.set(edge.from, []);
  graph.outgoing.get(edge.from)!.push(edge);

  if (!graph.incoming.has(edge.to)) graph.incoming.set(edge.to, []);
  graph.incoming.get(edge.to)!.push(edge);
}

function buildCableCountryEdges(graph: DependencyGraph, cables: CableInput[]): void {
  for (const cable of cables) {
    const cableId = `cable:${cable.id}`;

    cable.countriesServed?.forEach(cs => {
      const countryId = `country:${cs.country}`;
      addEdge(graph, {
        from: cableId,
        to: countryId,
        type: 'serves',
        strength: cs.capacityShare,
        redundancy: cs.isRedundant ? 0.5 : 0,
        metadata: {
          capacityShare: cs.capacityShare,
          estimatedImpact: cs.isRedundant ? 'Medium - redundancy available' : 'High - limited redundancy',
        },
      });
    });

    cable.landingPoints?.forEach(lp => {
      const countryId = `country:${lp.country}`;
      addEdge(graph, {
        from: cableId,
        to: countryId,
        type: 'lands_at',
        strength: 0.3,
        redundancy: 0.5,
      });
    });
  }
}

function buildPipelineCountryEdges(graph: DependencyGraph, pipelines: PipelineInput[]): void {
  for (const pipeline of pipelines) {
    const pipelineId = `pipeline:${pipeline.id}`;

    pipeline.countries?.forEach(country => {
      const code = country === 'USA' ? 'US' : country === 'Canada' ? 'CA' : country;
      const countryId = `country:${code}`;

      if (graph.nodes.has(countryId)) {
        addEdge(graph, {
          from: pipelineId,
          to: countryId,
          type: 'serves',
          strength: 0.2,
          redundancy: 0.3,
        });
      }
    });
  }
}

// Country code normalization for ports
function normalizeCountryCode(country: string): string {
  const mappings: Record<string, string> = {
    'USA': 'US', 'China': 'CN', 'China (SAR)': 'CN', 'Taiwan': 'TW',
    'South Korea': 'KR', 'Netherlands': 'NL', 'Belgium': 'BE',
    'Malaysia': 'MY', 'Thailand': 'TH', 'Greece': 'GR',
    'Saudi Arabia': 'SA', 'Iran': 'IR', 'Qatar': 'QA', 'Russia': 'RU',
    'Egypt': 'EG', 'UK (Gibraltar)': 'GB', 'Djibouti': 'DJ',
    'Yemen': 'YE', 'Panama': 'PA', 'Spain': 'ES', 'Pakistan': 'PK',
    'Sri Lanka': 'LK', 'Japan': 'JP', 'UK': 'GB', 'France': 'FR',
    'Brazil': 'BR', 'India': 'IN', 'Singapore': 'SG', 'Germany': 'DE',
    'UAE': 'AE',
  };
  return mappings[country] || country;
}

// Port importance by type for impact calculation
function getPortImportance(port: PortInput): number {
  const typeWeight: Record<string, number> = {
    'oil': 0.9,     // Oil disruption = major
    'lng': 0.85,    // LNG disruption = major
    'container': 0.7,
    'mixed': 0.6,
    'bulk': 0.5,
    'naval': 0.4,   // Naval = geopolitical but less economic
  };
  const baseWeight = typeWeight[port.type] || 0.5;
  // Higher rank = more important (rank 1-10 get boost)
  const rankBoost = port.rank ? Math.max(0, (20 - port.rank) / 20) * 0.3 : 0;
  return Math.min(1, baseWeight + rankBoost);
}

function buildPortCountryEdges(graph: DependencyGraph, ports: PortInput[]): void {
  for (const port of ports) {
    const portId = `port:${port.id}`;
    const countryCode = normalizeCountryCode(port.country);
    const countryId = `country:${countryCode}`;

    // Create country node if it doesn't exist
    if (!graph.nodes.has(countryId)) {
      graph.nodes.set(countryId, {
        id: countryId,
        type: 'country',
        name: COUNTRY_NAMES[countryCode] || port.country,
        metadata: { code: countryCode },
      });
    }

    const importance = getPortImportance(port);

    // Port → Country edge
    addEdge(graph, {
      from: portId,
      to: countryId,
      type: 'serves',
      strength: importance,
      redundancy: port.rank && port.rank <= 5 ? 0.2 : 0.4, // Major ports harder to replace
      metadata: {
        portType: port.type,
        estimatedImpact: importance > 0.7 ? 'Critical port for country' : 'Regional port',
      },
    });

    // Add dependencies for countries this port serves beyond its own
    // Strategic ports affect multiple countries
    const affectedCountries = getAffectedCountries(port);
    for (const affected of affectedCountries) {
      const affectedCountryId = `country:${affected.code}`;
      if (!graph.nodes.has(affectedCountryId)) {
        graph.nodes.set(affectedCountryId, {
          id: affectedCountryId,
          type: 'country',
          name: COUNTRY_NAMES[affected.code] || affected.code,
          metadata: { code: affected.code },
        });
      }
      addEdge(graph, {
        from: portId,
        to: affectedCountryId,
        type: 'trade_route',
        strength: affected.strength,
        redundancy: 0.5,
        metadata: {
          relationship: affected.reason,
        },
      });
    }
  }
}

// Strategic ports affect countries beyond their location
function getAffectedCountries(port: PortInput): { code: string; strength: number; reason: string }[] {
  const affected: { code: string; strength: number; reason: string }[] = [];

  // Suez Canal ports affect Europe-Asia trade
  if (port.id === 'port_said' || port.id === 'suez_port') {
    affected.push(
      { code: 'DE', strength: 0.6, reason: 'Major EU importer via Suez' },
      { code: 'GB', strength: 0.5, reason: 'UK-Asia trade' },
      { code: 'NL', strength: 0.5, reason: 'Rotterdam connection' },
      { code: 'CN', strength: 0.4, reason: 'China-EU trade route' },
      { code: 'IT', strength: 0.4, reason: 'Mediterranean trade' },
    );
  }

  // Strait of Hormuz ports
  if (port.id === 'bandar_abbas' || port.id === 'fujairah' || port.id === 'ras_tanura') {
    affected.push(
      { code: 'JP', strength: 0.7, reason: 'Oil import dependency' },
      { code: 'KR', strength: 0.6, reason: 'Oil import dependency' },
      { code: 'IN', strength: 0.5, reason: 'Oil imports' },
      { code: 'CN', strength: 0.5, reason: 'Oil imports' },
    );
  }

  // Malacca Strait ports
  if (port.id === 'singapore' || port.id === 'klang' || port.id === 'tanjung_pelepas') {
    affected.push(
      { code: 'CN', strength: 0.6, reason: 'Trade route dependency' },
      { code: 'JP', strength: 0.5, reason: 'Trade route' },
      { code: 'KR', strength: 0.5, reason: 'Trade route' },
    );
  }

  // Panama Canal ports
  if (port.id === 'colon' || port.id === 'balboa') {
    affected.push(
      { code: 'US', strength: 0.5, reason: 'East-West coast shipping' },
      { code: 'CN', strength: 0.4, reason: 'Trade route to US East Coast' },
    );
  }

  // Red Sea/Aden ports (especially relevant with Houthi disruptions)
  if (port.id === 'aden' || port.id === 'djibouti' || port.id === 'hodeidah') {
    affected.push(
      { code: 'DE', strength: 0.5, reason: 'Europe-Asia shipping route' },
      { code: 'GB', strength: 0.5, reason: 'Shipping route' },
      { code: 'IT', strength: 0.4, reason: 'Mediterranean access' },
      { code: 'SA', strength: 0.4, reason: 'Regional trade' },
    );
  }

  return affected;
}

function buildChokepointEdges(
  graph: DependencyGraph,
  waterways: WaterwayInput[],
  ports: PortInput[],
): void {
  // Connect chokepoints to nearby ports and countries they affect
  for (const waterway of waterways) {
    const chokepointId = `chokepoint:${waterway.id}`;

    // Find ports near this chokepoint
    const nearbyPorts = ports.filter(port => {
      const dist = haversineKm(waterway.lat, waterway.lon, port.lat, port.lon);
      return dist < 500; // Within 500km
    });

    for (const port of nearbyPorts) {
      addEdge(graph, {
        from: chokepointId,
        to: `port:${port.id}`,
        type: 'controls_access',
        strength: 0.7,
        redundancy: 0.2,
        metadata: {
          relationship: 'Access controlled by chokepoint',
        },
      });
    }

    // Add dependent countries based on chokepoint
    const dependentCountries = getChokepointDependentCountries(waterway.id);
    for (const dep of dependentCountries) {
      const countryId = `country:${dep.code}`;
      if (!graph.nodes.has(countryId)) {
        graph.nodes.set(countryId, {
          id: countryId,
          type: 'country',
          name: COUNTRY_NAMES[dep.code] || dep.code,
          metadata: { code: dep.code },
        });
      }
      addEdge(graph, {
        from: chokepointId,
        to: countryId,
        type: 'trade_dependency',
        strength: dep.strength,
        redundancy: dep.redundancy,
        metadata: {
          relationship: dep.reason,
        },
      });
    }
  }
}

function getChokepointDependentCountries(chokepointId: string): { code: string; strength: number; redundancy: number; reason: string }[] {
  // Map using actual IDs from STRATEGIC_WATERWAYS
  const dependencies: Record<string, { code: string; strength: number; redundancy: number; reason: string }[]> = {
    'suez': [
      { code: 'DE', strength: 0.6, redundancy: 0.3, reason: 'EU-Asia trade' },
      { code: 'IT', strength: 0.5, redundancy: 0.3, reason: 'Mediterranean' },
      { code: 'GB', strength: 0.5, redundancy: 0.4, reason: 'UK-Asia trade' },
      { code: 'CN', strength: 0.4, redundancy: 0.5, reason: 'China-EU exports' },
    ],
    'hormuz_strait': [
      { code: 'JP', strength: 0.8, redundancy: 0.2, reason: '80% oil imports' },
      { code: 'KR', strength: 0.7, redundancy: 0.2, reason: '70% oil imports' },
      { code: 'IN', strength: 0.6, redundancy: 0.3, reason: '60% oil imports' },
      { code: 'CN', strength: 0.5, redundancy: 0.4, reason: '40% oil imports' },
    ],
    'malacca_strait': [
      { code: 'CN', strength: 0.7, redundancy: 0.3, reason: '80% oil imports transit' },
      { code: 'JP', strength: 0.6, redundancy: 0.3, reason: 'Trade route' },
      { code: 'KR', strength: 0.6, redundancy: 0.3, reason: 'Trade route' },
    ],
    'bab_el_mandeb': [
      { code: 'DE', strength: 0.5, redundancy: 0.4, reason: 'EU shipping' },
      { code: 'GB', strength: 0.5, redundancy: 0.4, reason: 'UK shipping' },
      { code: 'SA', strength: 0.4, redundancy: 0.5, reason: 'Red Sea access' },
    ],
    'panama': [
      { code: 'US', strength: 0.5, redundancy: 0.4, reason: 'Inter-coast shipping' },
      { code: 'CN', strength: 0.4, redundancy: 0.5, reason: 'US East trade' },
    ],
    'gibraltar': [
      { code: 'ES', strength: 0.4, redundancy: 0.5, reason: 'Med access' },
      { code: 'IT', strength: 0.3, redundancy: 0.5, reason: 'Atlantic trade' },
    ],
    'bosphorus': [
      { code: 'RU', strength: 0.6, redundancy: 0.3, reason: 'Black Sea access' },
      { code: 'UA', strength: 0.6, redundancy: 0.3, reason: 'Grain exports' },
      { code: 'RO', strength: 0.4, redundancy: 0.4, reason: 'Black Sea trade' },
    ],
    'dardanelles': [
      { code: 'RU', strength: 0.5, redundancy: 0.3, reason: 'Black Sea access' },
      { code: 'UA', strength: 0.5, redundancy: 0.3, reason: 'Grain exports' },
    ],
    'taiwan_strait': [
      { code: 'TW', strength: 0.9, redundancy: 0.1, reason: 'Taiwan trade lifeline' },
      { code: 'JP', strength: 0.5, redundancy: 0.4, reason: 'Trade route' },
      { code: 'KR', strength: 0.4, redundancy: 0.4, reason: 'Trade route' },
    ],
  };
  return dependencies[chokepointId] || [];
}

export function buildDependencyGraph(inputs: CascadeGraphInputs): DependencyGraph {
  const cables = inputs.cables;
  const pipelines = inputs.pipelines ?? PIPELINES;
  const ports = inputs.ports ?? PORTS;
  const waterways = inputs.waterways;

  const graph: DependencyGraph = {
    nodes: new Map(),
    edges: [],
    outgoing: new Map(),
    incoming: new Map(),
    cables,
  };

  // Add all infrastructure nodes
  addCablesAsNodes(graph, cables);
  addPipelinesAsNodes(graph, pipelines);
  addPortsAsNodes(graph, ports);
  addChokepointsAsNodes(graph, waterways);
  addCountriesAsNodes(graph, cables, pipelines);

  // Build dependency edges
  buildCableCountryEdges(graph, cables);
  buildPipelineCountryEdges(graph, pipelines);
  buildPortCountryEdges(graph, ports);        // Port → Country dependencies
  buildChokepointEdges(graph, waterways, ports); // Chokepoint → Port/Country dependencies

  return graph;
}

function categorizeImpact(strength: number): CascadeImpactLevel {
  if (strength > 0.8) return 'critical';
  if (strength > 0.5) return 'high';
  if (strength > 0.2) return 'medium';
  return 'low';
}

export function calculateCascade(
  graph: DependencyGraph,
  sourceId: string,
  disruptionLevel: number = 1.0,
): CascadeResult | null {
  const source = graph.nodes.get(sourceId);

  if (!source) return null;

  const affected: Map<string, CascadeAffectedNode> = new Map();
  const visited = new Set<string>();
  visited.add(sourceId);

  const queue: { nodeId: string; depth: number; path: string[] }[] = [
    { nodeId: sourceId, depth: 0, path: [sourceId] },
  ];

  while (queue.length > 0) {
    const { nodeId, depth, path } = queue.shift()!;
    if (depth >= 3) continue;

    const dependents = graph.outgoing.get(nodeId) || [];

    for (const edge of dependents) {
      if (visited.has(edge.to)) continue;
      visited.add(edge.to);

      const impactStrength = edge.strength * disruptionLevel * (1 - (edge.redundancy || 0));
      const targetNode = graph.nodes.get(edge.to);

      if (!targetNode || impactStrength < 0.05) continue;

      affected.set(edge.to, {
        node: targetNode,
        impactLevel: categorizeImpact(impactStrength),
        pathLength: depth + 1,
        dependencyChain: [...path, edge.to],
        redundancyAvailable: (edge.redundancy || 0) > 0.3,
        estimatedRecovery: edge.metadata?.estimatedImpact,
      });

      queue.push({
        nodeId: edge.to,
        depth: depth + 1,
        path: [...path, edge.to],
      });
    }
  }

  const countriesAffected: CascadeCountryImpact[] = [];
  for (const [nodeId, affectedNode] of affected) {
    if (affectedNode.node.type === 'country') {
      const code = (affectedNode.node.metadata?.code as string) || nodeId.replace('country:', '');
      countriesAffected.push({
        country: code,
        countryName: affectedNode.node.name,
        impactLevel: affectedNode.impactLevel,
        affectedCapacity: getCapacityForCountry(sourceId, code, graph, affectedNode.dependencyChain),
      });
    }
  }

  countriesAffected.sort((a, b) => {
    const order = { critical: 0, high: 1, medium: 2, low: 3 };
    return (order[a.impactLevel] - order[b.impactLevel]) || (b.affectedCapacity - a.affectedCapacity);
  });

  const redundancies = findRedundancies(graph, sourceId);

  return {
    source,
    affectedNodes: Array.from(affected.values()),
    countriesAffected,
    redundancies,
  };
}

function getCapacityForCountry(
  sourceId: string,
  countryCode: string,
  graph: DependencyGraph,
  dependencyChain: string[],
): number {
  if (sourceId.startsWith('cable:')) {
    const cableId = sourceId.replace('cable:', '');
    const cable = graph.cables.find(c => c.id === cableId);
    const countryData = cable?.countriesServed?.find(cs => cs.country === countryCode);
    return countryData?.capacityShare || 0;
  }

  // Check direct edges from source → country
  const countryId = `country:${countryCode}`;
  const outgoing = graph.outgoing.get(sourceId) || [];
  const direct = outgoing.filter(e => e.to === countryId);
  if (direct.length > 0) {
    const effective = direct.map(e => e.strength * (1 - (e.redundancy || 0)));
    return Math.max(...effective);
  }

  // Walk the BFS dependency chain for indirect impacts (e.g. chokepoint → port → country)
  if (dependencyChain.length > 2) {
    let pathCapacity = 1;
    for (let i = 0; i < dependencyChain.length - 1; i++) {
      const from = dependencyChain[i]!;
      const to = dependencyChain[i + 1]!;
      const stepEdges = graph.outgoing.get(from) || [];
      const edge = stepEdges.find(e => e.to === to);
      if (edge) {
        pathCapacity *= edge.strength * (1 - (edge.redundancy || 0));
      } else {
        pathCapacity = 0;
        break;
      }
    }
    if (pathCapacity > 0) return pathCapacity;
  }

  return 0;
}

function findRedundancies(graph: DependencyGraph, sourceId: string): CascadeResult['redundancies'] {
  if (!sourceId.startsWith('cable:')) return [];

  const cableId = sourceId.replace('cable:', '');
  const sourceCable = graph.cables.find(c => c.id === cableId);
  if (!sourceCable) return [];

  const sourceCountries = new Set(sourceCable.countriesServed?.map(c => c.country) || []);
  const alternatives: NonNullable<CascadeResult['redundancies']> = [];

  for (const cable of graph.cables) {
    if (cable.id === cableId) continue;

    const sharedCountries = cable.countriesServed?.filter(c => sourceCountries.has(c.country)) || [];
    if (sharedCountries.length > 0) {
      const avgCapacity = sharedCountries.reduce((sum, c) => sum + c.capacityShare, 0) / sharedCountries.length;
      alternatives.push({
        id: cable.id,
        name: cable.name,
        capacityShare: avgCapacity,
      });
    }
  }

  return alternatives.slice(0, 5);
}

export function getCableById(id: string, cables: CableInput[]): CableInput | undefined {
  return cables.find(c => c.id === id);
}

export function getPipelineById(id: string, pipelines: PipelineInput[] = PIPELINES): PipelineInput | undefined {
  return pipelines.find(p => p.id === id);
}

export function getPortById(id: string, ports: PortInput[] = PORTS): PortInput | undefined {
  return ports.find(p => p.id === id);
}

export interface CascadeGraphStats {
  nodes: number;
  edges: number;
  cables: number;
  pipelines: number;
  ports: number;
  chokepoints: number;
  countries: number;
}

export function getGraphStats(graph: DependencyGraph): CascadeGraphStats {
  let cables = 0, pipelines = 0, ports = 0, chokepoints = 0, countries = 0;

  for (const node of graph.nodes.values()) {
    if (node.type === 'cable') cables++;
    else if (node.type === 'pipeline') pipelines++;
    else if (node.type === 'port') ports++;
    else if (node.type === 'chokepoint') chokepoints++;
    else if (node.type === 'country') countries++;
  }

  return {
    nodes: graph.nodes.size,
    edges: graph.edges.length,
    cables,
    pipelines,
    ports,
    chokepoints,
    countries,
  };
}