import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class ConsultaCepService {

constructor(private http: HttpClient) { }

consultaCep(cep: string){
  // retira todos os caracteres que não sejam números
  cep = cep.replace(/\D/g, '');
  if (cep !== ''){
    const validacep = /^[0-9]{8}$/;
    if (validacep.test(cep)) {
      const url = environment.url+`cep/find/viacep/${cep}`
      return this.http.get<any>(url);
    }
  }
}

googleGeocode(address: string, addressNumber: string, neighborboard: string, city: string, state: string) {
  const url = environment.url+`cep/google`;
  const params = new HttpParams(
    {
      fromObject: {
        'address': address,
        'addressNumber': addressNumber,
        'neighborboard': neighborboard,
        'city': city,
        'state': state
      }
    });
    const response = this.http.get(url, {params});
    return response;
}

}
