"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __typeError = (msg) => {
  throw TypeError(msg);
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);

// src/components/AddressLookup/LoqateAddressLookupService.tsx
var LoqateAddressLookupService_exports = {};
__export(LoqateAddressLookupService_exports, {
  LoqateAddressLookupService: () => LoqateAddressLookupService
});
module.exports = __toCommonJS(LoqateAddressLookupService_exports);

// src/components/AddressLookup/LoqateAddressLookupError.tsx
var LoqateAddressLookupError = class extends Error {
  constructor(response) {
    super(response.Description);
    this.name = "LoqateAddressLookupError";
    this.response = response;
  }
};

// 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();
//# sourceMappingURL=LoqateAddressLookupService.js.map
