import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class AutocompleteService {
  private apiUrl = 'https://nominatim.openstreetmap.org';

  constructor(private http: HttpClient) {}

  getAddressSuggestions(query: string): Observable<any> {
    return this.http.get(this.apiUrl + '/search', {
      params: {
        q: query,
        format: 'json',
        addressdetails: '1',
        limit: '5',
      },
    });
  }

  getAdressCoordinates(query: string): Observable<any> {
    return this.http.get(this.apiUrl + '/search', {
      params: {
        q: query,
        format: 'json',
        addressdetails: '1',
        limit: '1',
      },
    });
  }

  getAddressFromCoordinates(lat: number, lon: number): Observable<any> {
    return this.http.get(this.apiUrl + '/reverse', {
      params: {
        lat: lat.toString(),
        lon: lon.toString(),
        format: 'json',
        addressdetails: '1',
      },
    });
  }
  
}
