import {
  LoqateAddressLookupError
} from "./chunk-EDSRI6SV.js";
import {
  __privateAdd,
  __privateGet,
  __privateSet
} from "./chunk-VHAA22YE.js";

// src/components/AddressLookup/LoqateAddressLookupService.tsx
var LOQATE_BASE_URL = "https://api.addressy.com/Capture/Interactive";
var LOQATE_FIND_URL = "/Find/v1.00/json3.ws";
var LOQATE_RETRIEVE_URL = "/Retrieve/v1.2/json3.ws";
var DEFAULT_COUNTRIES = ["GB"];
var _baseUrl, _apiKey, _countries, _filters;
var LoqateAddressLookupService = class {
  constructor({
    baseUrl,
    apiKey,
    countries,
    filters
  }) {
    /**
     * Base URL for the Loqate API
     */
    __privateAdd(this, _baseUrl);
    /**
     * API key for the Loqate API
     */
    __privateAdd(this, _apiKey);
    /**
     * List of allowed country codes for the Loqate API
     * 2 or 3 character ISO country codes
     */
    __privateAdd(this, _countries);
    /**
     * Optional filters for the Loqate API
     * E.g., { AdministrativeArea: "CA", PostalCode: "90210" }
     */
    __privateAdd(this, _filters);
    __privateSet(this, _apiKey, apiKey);
    __privateSet(this, _baseUrl, baseUrl || LOQATE_BASE_URL);
    __privateSet(this, _countries, countries || DEFAULT_COUNTRIES);
    __privateSet(this, _filters, filters);
  }
  fetchFromApi(url) {
    return fetch(`${__privateGet(this, _baseUrl)}${url}`).then((response) => response.json()).then((json) => {
      if (json.Items?.some((item) => item.Error)) {
        throw new LoqateAddressLookupError(json);
      }
      return json;
    });
  }
  /**
   * Builds the Filters query parameter for Loqate API requests.
   * - Filter keys (e.g., "AdministrativeArea", "PostalCode") are predefined by Loqate API (no need to encode)
   * - Filter values (e.g., "New York", "90210") contain user input that may have special characters (need encoding)
   *
   * @returns Empty string if no filters, otherwise "&Filters=key1:value1&key2:value2" (Loqate's expected format for Filters)
   */
  buildFiltersQuery() {
    if (!__privateGet(this, _filters) || Object.keys(__privateGet(this, _filters)).length === 0) {
      return "";
    }
    const encodedFilters = Object.entries(__privateGet(this, _filters)).map(([key, value]) => `${key}:${encodeURIComponent(value)}`).join("&");
    return `&Filters=${encodedFilters}`;
  }
  search(searchTerm) {
    let url = `${LOQATE_FIND_URL}?Key=${__privateGet(this, _apiKey)}&Text=${searchTerm}&Countries=${__privateGet(this, _countries)?.join(",")}`;
    url += this.buildFiltersQuery();
    return this.fetchFromApi(url);
  }
  findById(id) {
    let url = `${LOQATE_FIND_URL}?Key=${__privateGet(this, _apiKey)}&Container=${id}&Countries=${__privateGet(this, _countries)?.join(",")}`;
    url += this.buildFiltersQuery();
    return this.fetchFromApi(url);
  }
  async get(id) {
    const url = `${LOQATE_RETRIEVE_URL}?Key=${__privateGet(this, _apiKey)}&Id=${id}`;
    const response = await this.fetchFromApi(url);
    return response.Items[0];
  }
};
_baseUrl = new WeakMap();
_apiKey = new WeakMap();
_countries = new WeakMap();
_filters = new WeakMap();

export {
  LoqateAddressLookupService
};
//# sourceMappingURL=chunk-VVL4B2KD.js.map
