import axios from 'axios';

const widgets = {
  absorptionRate: 'absorption-rate',
  crime: 'crime',
  hazards: 'hazards',
  mapPreview: 'map-preview',
  marketTrends: 'market-trends',
  noise: 'noise',
  population: 'population',
  propertyTypes: 'property-types',
  rentEstimate: 'rent-estimate',
  thEstimate: 'th-estimate',
  turnover: 'turnover',
  walkability: 'walkability',
  zipCodeMap: 'zip-code-map',
};

export default class PropertyService {
  #dataApi;

  #widgetApi;

  constructor(config) {
    const { dataBaseURL, widgetBaseURL, apiKey } = config;
    this.#dataApi = axios.create({
      baseURL: dataBaseURL,
      headers: { 'X-API-Key': apiKey },
    });
    this.#widgetApi = axios.create({
      baseURL: widgetBaseURL,
      params: {
        sid: apiKey,
      },
    });
  }

  fetchProperty(params) {
    return this.#dataApi.get('/property', { params });
  }

  search(body) {
    return this.#dataApi.post('/search', body);
  }

  fetchComparables(params) {
    return this.#dataApi.get('/comparables', { params });
  }

  fetchWidgetByPropertyId(widget, propertyId, params) {
    return this.#widgetApi.get(`${widget}/${propertyId}`, { params });
  }

  fetchWidgetByAddress(widget, params) {
    return this.#widgetApi.get(`${widget}/address`, { params });
  }
}
