import { useState as m, useRef as h, useEffect as p } from "react";
import { debounce as b } from "es-toolkit/function";
const E = "https://autocomplete.search.hereapi.com/v1/autocomplete", w = async (e, r, l = {}) => {
  const { apiKey: s, fetchUrl: u = E, limit: o = 5, ...f } = l;
  if (!e || !s)
    return [];
  const a = new URLSearchParams({
    q: e,
    apiKey: s,
    limit: String(o),
    // Add any additional options dynamically
    ...f
  }), t = await fetch(`${u}?${a.toString()}`, { signal: r });
  if (!t.ok)
    throw new Error(`API request failed with status ${t.status}`);
  const n = await t.json();
  return Array.isArray(n?.items) ? n.items.map((c) => ({
    id: c.id,
    label: c.title
  })) : [];
}, S = 300, L = (e, r = {}) => {
  const [l, s] = m([]), [u, o] = m(!1), [f, a] = m(null), t = h(null), n = h(
    b(async (c, d) => {
      t.current?.abort();
      const g = new AbortController();
      t.current = g, a(null);
      try {
        const i = await w(c, g.signal, d);
        s(i);
      } catch (i) {
        i.name !== "AbortError" && a(i), s([]);
      } finally {
        o(!1);
      }
    }, S)
  ).current;
  return p(() => {
    if (!e || !r.apiKey) {
      s([]), o(!1), t.current?.abort();
      return;
    }
    o(!0), n(e, r);
  }, [e, n, r.apiKey, r.fetchUrl]), p(
    () => () => {
      t.current?.abort();
    },
    []
  ), { suggestions: l, loading: u, error: f };
};
export {
  L as default
};
//# sourceMappingURL=useLocationSuggestions.js.map
