import { defineComponent as S, useSlots as A, inject as T, computed as h, ref as n, watch as I, openBlock as P, createBlock as j, unref as y, createSlots as k, withCtx as D, renderSlot as G } from "vue";
import { debounce as K } from "es-toolkit";
import { uniqueId as C } from "es-toolkit/compat";
import U from "./useGoogleMaps.js";
import { DEBOUNCE as q } from "./constants.js";
import F from "./Select.js";
const _ = "address-select-", $ = /* @__PURE__ */ S({
  __name: "AddressSelect",
  props: {
    apiKey: { default: void 0 },
    disabled: { type: Boolean, default: !1 },
    placeholder: { default: "Type an address" },
    debounceTime: { default: q.MEDIUM },
    modelValue: { default: () => ({
      street_address: void 0,
      extended_address: void 0,
      city: void 0,
      state: void 0,
      postal_code: void 0,
      country: void 0
    }) },
    hintText: { default: void 0 },
    errorText: { default: void 0 },
    label: { default: void 0 },
    preventEmpty: { type: Boolean, default: !1 }
  },
  emits: ["update:modelValue", "error"],
  setup(g, { emit: V }) {
    const t = g, l = V, b = A(), s = T("stashOptions"), u = h(() => t.apiKey || (s == null ? void 0 : s.googleMapsApiKey));
    if (!u.value)
      throw new Error("Google Maps API key is required");
    const { getPlaceDetails: x, getPlacePredictions: E } = U(u.value), r = n(!1), a = n(), d = n(), w = K(B, t.debounceTime), i = h(() => {
      var e, c, m, p, f, v;
      return [
        [(e = t.modelValue) == null ? void 0 : e.street_address, (c = t.modelValue) == null ? void 0 : c.extended_address].filter(Boolean).join(" "),
        (m = t.modelValue) == null ? void 0 : m.city,
        [(p = t.modelValue) == null ? void 0 : p.state, (f = t.modelValue) == null ? void 0 : f.postal_code].filter(Boolean).join(" "),
        (v = t.modelValue) == null ? void 0 : v.country
      ].filter(Boolean).join(", ");
    });
    I(
      () => t.modelValue,
      () => {
        var o;
        if (i.value && !((o = a.value) != null && o.length)) {
          const e = {
            id: t.modelValue.place_id || C(_),
            name: i.value,
            address: t.modelValue
          };
          a.value = [e], d.value = e;
        } else i.value || (d.value = void 0, a.value = []);
      },
      { immediate: !0 }
    );
    async function B(o) {
      r.value = !0;
      try {
        a.value = await E(o);
      } catch (e) {
        if (a.value = [], e != null && e.isGoogleMapsError)
          l("error", e);
        else
          throw e;
      } finally {
        r.value = !1;
      }
    }
    async function M(o) {
      if (d.value = o, !o) {
        l("update:modelValue", {
          street_address: void 0,
          extended_address: void 0,
          city: void 0,
          state: void 0,
          postal_code: void 0,
          country: void 0
        });
        return;
      }
      if (o.id.startsWith(_) && o.address) {
        l("update:modelValue", o.address);
        return;
      }
      try {
        const e = await x(o.id);
        l("update:modelValue", e);
      } catch (e) {
        if (e != null && e.isGoogleMapsError)
          l("error", e);
        else
          throw e;
      }
    }
    return (o, e) => (P(), j(F, {
      single: "",
      "data-test": "select|address",
      disabled: t.disabled,
      loading: r.value,
      options: a.value,
      placeholder: t.placeholder,
      label: t.label,
      "hint-text": t.hintText,
      error: t.errorText,
      "model-value": d.value,
      "prevent-empty": t.preventEmpty,
      "disable-filtering": "",
      onSearch: y(w),
      "onUpdate:modelValue": M
    }, k({ _: 2 }, [
      y(b).hint ? {
        name: "hint",
        fn: D(() => [
          G(o.$slots, "hint")
        ]),
        key: "0"
      } : void 0
    ]), 1032, ["disabled", "loading", "options", "placeholder", "label", "hint-text", "error", "model-value", "prevent-empty", "onSearch"]));
  }
});
export {
  $ as default
};
//# sourceMappingURL=AddressSelect.js.map
