#!/bin/sh
set -ex

# Create a temporary directory and ensure cleanup
TMPDIR="$(mktemp -d)"
trap 'rm -rf "$TMPDIR"' EXIT INT TERM

cd "$TMPDIR"

pkgjs-ln "d3"
pkgjs-ln "@types/d3"
pkgjs-ln "geojson"
pkgjs-ln "@types/geojson" || true

# Create a tiny TS project
cat > tsconfig.json <<EOF
{
  "compilerOptions": {
    "strict": true,
    "noEmit": true,
    "moduleResolution": "node",
    "target": "ES2020",
    "module": "ES2020",
    "types": ["d3"]
  }
}
EOF

cat > test.ts <<EOF
import * as d3 from "d3";

// Touch both d3-array and d3-collection types
const arr = [1, 2, 3];
const nested = d3.nest()
  .key(String)
  .entries(arr);

console.log(nested);
EOF

# Run TypeScript compiler against Debian-installed @types/d3
tsc

