import { describe, it, expect } from 'vitest'

function matchRoute(currentPath: string, pattern: string): Record<string, string> | null {
  const patternParts = pattern.split('/').filter(Boolean)
  const pathParts = currentPath.split('/').filter(Boolean)
  if (pattern === '/' && pathParts.length === 0) return {}
  if (patternParts.length !== pathParts.length) return null
  const params: Record<string, string> = {}
  for (let i = 0; i < patternParts.length; i++) {
    const patternPart = patternParts[i]!
    const pathPart = pathParts[i]!
    if (patternPart.startsWith(':')) {
      params[patternPart.slice(1)] = pathPart
    } else if (patternPart !== pathPart) {
      return null
    }
  }
  return params
}

describe('matchRoute', () => {
  it('matches the root pattern against an empty path', () => {
    expect(matchRoute('/', '/')).toEqual({})
  })

  it('matches static multi-segment paths', () => {
    expect(matchRoute('/collections/posts', '/collections/posts')).toEqual({})
  })

  it('extracts named parameters from dynamic segments', () => {
    expect(matchRoute('/collections/posts/abc-123', '/collections/posts/:id')).toEqual({
      id: 'abc-123',
    })
    expect(matchRoute('/users/u1/settings', '/users/:userId/settings')).toEqual({ userId: 'u1' })
  })

  it('returns null when a static segment does not match', () => {
    expect(matchRoute('/collections/pages', '/collections/posts')).toBeNull()
  })

  it('returns null when segment counts differ', () => {
    expect(matchRoute('/a', '/a/b')).toBeNull()
    expect(matchRoute('/a/b', '/a')).toBeNull()
  })
})
