/**
 * Test file for map URL parsing functionality
 * Run with: npm test -- mapUrlParser.test.ts
 */

import { parseMapUrl, parseMapyCzUrl, parseGoogleMapsUrl } from './mapUrlParser';

describe('Map URL Parser', () => {
  describe('parseMapyCzUrl', () => {
    it('should parse mapy.cz URL with coordinates', () => {
      const url = 'https://mapy.com/en/letecka?q=krnov%20stadion&source=firm&id=12954454&ds=2&x=17.6996859&y=50.0947150&z=19';
      const result = parseMapyCzUrl(url);
      
      expect(result).not.toBeNull();
      expect(result?.latitude).toBeCloseTo(50.0947150, 6);
      expect(result?.longitude).toBeCloseTo(17.6996859, 6);
      expect(result?.zoom).toBe(19);
      expect(result?.address).toBe('krnov stadion');
      expect(result?.source).toBe('mapy.cz');
    });

    it('should handle URL without protocol', () => {
      const url = 'mapy.com/en/letecka?x=17.6996859&y=50.0947150&z=19';
      const result = parseMapUrl(url); // Using main parser which adds protocol
      
      expect(result).not.toBeNull();
      expect(result?.latitude).toBeCloseTo(50.0947150, 6);
      expect(result?.longitude).toBeCloseTo(17.6996859, 6);
    });
  });

  describe('parseGoogleMapsUrl', () => {
    it('should parse Google Maps URL with @lat,lng format', () => {
      const url = 'https://www.google.com/maps/place/Fotbalový+stadión/@50.0946232,17.6987331,226m/data=!3m1!1e3!4m10!1m2!2m1!1skrnov+stadion!3m6!1s0x4711826a3559a643:0x4d71cca773c76501!8m2!3d50.0948669!4d17.7001456!15sCg1rcm5vdiBzdGFkaW9ukgEMc29jY2VyX2ZpZWxkqgFDEAEqCyIHc3RhZGlvbigRMh8QASIba573MJA7DqmP6r90HLrUDwgCBKFvnrXeffL6MhEQAiINa3Jub3Ygc3RhZGlvbuABAA!16s%2Fg%2F11cllydltw?entry=ttu&g_ep=EgoyMDI1MTAwMS4wIKXMDSoASAFQAw%3D%3D';
      const result = parseGoogleMapsUrl(url);
      
      expect(result).not.toBeNull();
      expect(result?.latitude).toBeCloseTo(50.0946232, 6);
      expect(result?.longitude).toBeCloseTo(17.6987331, 6);
      expect(result?.zoom).toBe(226);
      expect(result?.source).toBe('google-maps');
    });

    it('should parse compact Google Maps URL', () => {
      const url = 'https://maps.google.com/@50.0948669,17.7001456,15z';
      const result = parseGoogleMapsUrl(url);
      
      expect(result).not.toBeNull();
      expect(result?.latitude).toBeCloseTo(50.0948669, 6);
      expect(result?.longitude).toBeCloseTo(17.7001456, 6);
      expect(result?.zoom).toBe(15);
    });
  });

  describe('parseMapUrl', () => {
    it('should detect and parse mapy.cz URL', () => {
      const url = 'mapy.com/en/letecka?x=17.6996859&y=50.0947150&z=19';
      const result = parseMapUrl(url);
      
      expect(result).not.toBeNull();
      expect(result?.source).toBe('mapy.cz');
    });

    it('should detect and parse Google Maps URL', () => {
      const url = 'google.com/maps/@50.0948669,17.7001456,15z';
      const result = parseMapUrl(url);
      
      expect(result).not.toBeNull();
      expect(result?.source).toBe('google-maps');
    });

    it('should return null for invalid URL', () => {
      const url = 'https://example.com/not-a-map';
      const result = parseMapUrl(url);
      
      expect(result).toBeNull();
    });

    it('should return null for empty string', () => {
      const result = parseMapUrl('');
      
      expect(result).toBeNull();
    });
  });
});

// Manual test function for console testing
export function testMapUrlParser() {
  console.log('Testing Map URL Parser...\n');

  const mapyCzUrl = 'mapy.com/en/letecka?q=krnov%20stadion&source=firm&id=12954454&ds=2&x=17.6996859&y=50.0947150&z=19';
  const googleMapsUrl = 'www.google.com/maps/place/Fotbalový+stadión/@50.0946232,17.6987331,226m/data=!3m1!1e3!4m10!1m2!2m1!1skrnov+stadion!3m6!1s0x4711826a3559a643:0x4d71cca773c76501!8m2!3d50.0948669!4d17.7001456';

  console.log('1. Testing mapy.cz URL:');
  console.log('Input:', mapyCzUrl);
  const result1 = parseMapUrl(mapyCzUrl);
  console.log('Result:', JSON.stringify(result1, null, 2));

  console.log('\n2. Testing Google Maps URL:');
  console.log('Input:', googleMapsUrl);
  const result2 = parseMapUrl(googleMapsUrl);
  console.log('Result:', JSON.stringify(result2, null, 2));

  console.log('\n✓ Tests completed!');
}
