// Exercise 3: route matcher — simulate the App Router's file-system
// matching without Next.js itself, to understand the mechanism.
//
// Given a list of route "files" (in the format Next.js would see under
// app/) and an incoming URL path, implement matchRoute(routes, path)
// that returns the matching route pattern, or null if none match.
//
// Rules to implement:
//   - a static segment must match exactly ("about" matches "about" only)
//   - a "[param]" segment matches any single non-empty segment
//   - a "[...param]" segment matches one or more remaining segments (must be last)
//   - segment counts must match exactly for static/dynamic routes

function matchRoute(routes: string[], path: string): string | null {
  const pathSegments = path.split("/").filter(Boolean);

  for (const route of routes) {
    const routeSegments = route.split("/").filter(Boolean);
    // TODO: implement matching logic per the rules in the comment above.
    // Return the route string as soon as you find a match.
  }

  return null;
}

// --- self-check: do not modify below this line ---
const routes = ["", "about", "blog/[slug]", "docs/[...slug]"];

const cases: [string, string | null][] = [
  ["/", ""],
  ["/about", "about"],
  ["/blog/hello-world", "blog/[slug]"],
  ["/docs/a/b/c", "docs/[...slug]"],
  ["/nope/nope", null],
];

let allPass = true;
for (const [path, expected] of cases) {
  const result = matchRoute(routes, path);
  const pass = result === expected;
  if (!pass) allPass = false;
  console.log(`matchRoute(${path}) -> ${result}: ${pass ? "PASS" : "FAIL"}`);
}
console.log(allPass ? "ALL PASS" : "SOME FAILED");
