import "../../assets/index2.css";
import { jsxs as O, jsx as w, Fragment as R } from "react/jsx-runtime";
import M, { useState as _, useRef as N, useEffect as b, Children as P, isValidElement as D, cloneElement as U } from "react";
import { a as G, c as F } from "../../_commonjsHelpers-2ufMB8rV.js";
import { useDeepCompareEffectForMaps as $ } from "../../hooks/useDeepCompareEffect.js";
import x from "../Buttons/index.js";
import K from "../Icons.js";
import { cn as T } from "../../utils/functions.js";
var k = { exports: {} };
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function q(a, e, t, n) {
  function o(r) {
    return r instanceof t ? r : new t(function(i) {
      i(r);
    });
  }
  return new (t || (t = Promise))(function(r, i) {
    function u(l) {
      try {
        p(n.next(l));
      } catch (d) {
        i(d);
      }
    }
    function s(l) {
      try {
        p(n.throw(l));
      } catch (d) {
        i(d);
      }
    }
    function p(l) {
      l.done ? r(l.value) : o(l.value).then(u, s);
    }
    p((n = n.apply(a, e || [])).next());
  });
}
var Z = function a(e, t) {
  if (e === t)
    return !0;
  if (e && t && typeof e == "object" && typeof t == "object") {
    if (e.constructor !== t.constructor)
      return !1;
    var n, o, r;
    if (Array.isArray(e)) {
      if (n = e.length, n != t.length)
        return !1;
      for (o = n; o-- !== 0; )
        if (!a(e[o], t[o]))
          return !1;
      return !0;
    }
    if (e.constructor === RegExp)
      return e.source === t.source && e.flags === t.flags;
    if (e.valueOf !== Object.prototype.valueOf)
      return e.valueOf() === t.valueOf();
    if (e.toString !== Object.prototype.toString)
      return e.toString() === t.toString();
    if (r = Object.keys(e), n = r.length, n !== Object.keys(t).length)
      return !1;
    for (o = n; o-- !== 0; )
      if (!Object.prototype.hasOwnProperty.call(t, r[o]))
        return !1;
    for (o = n; o-- !== 0; ) {
      var i = r[o];
      if (!a(e[i], t[i]))
        return !1;
    }
    return !0;
  }
  return e !== e && t !== t;
};
const A = "__googleMapsScriptId";
var v;
(function(a) {
  a[a.INITIALIZED = 0] = "INITIALIZED", a[a.LOADING = 1] = "LOADING", a[a.SUCCESS = 2] = "SUCCESS", a[a.FAILURE = 3] = "FAILURE";
})(v || (v = {}));
class y {
  /**
   * Creates an instance of Loader using [[LoaderOptions]]. No defaults are set
   * using this library, instead the defaults are set by the Google Maps
   * JavaScript API server.
   *
   * ```
   * const loader = Loader({apiKey, version: 'weekly', libraries: ['places']});
   * ```
   */
  constructor({ apiKey: e, authReferrerPolicy: t, channel: n, client: o, id: r = A, language: i, libraries: u = [], mapIds: s, nonce: p, region: l, retries: d = 3, url: m = "https://maps.googleapis.com/maps/api/js", version: c }) {
    if (this.callbacks = [], this.done = !1, this.loading = !1, this.errors = [], this.apiKey = e, this.authReferrerPolicy = t, this.channel = n, this.client = o, this.id = r || A, this.language = i, this.libraries = u, this.mapIds = s, this.nonce = p, this.region = l, this.retries = d, this.url = m, this.version = c, y.instance) {
      if (!Z(this.options, y.instance.options))
        throw new Error(`Loader must not be called again with different options. ${JSON.stringify(this.options)} !== ${JSON.stringify(y.instance.options)}`);
      return y.instance;
    }
    y.instance = this;
  }
  get options() {
    return {
      version: this.version,
      apiKey: this.apiKey,
      channel: this.channel,
      client: this.client,
      id: this.id,
      libraries: this.libraries,
      language: this.language,
      region: this.region,
      mapIds: this.mapIds,
      nonce: this.nonce,
      url: this.url,
      authReferrerPolicy: this.authReferrerPolicy
    };
  }
  get status() {
    return this.errors.length ? v.FAILURE : this.done ? v.SUCCESS : this.loading ? v.LOADING : v.INITIALIZED;
  }
  get failed() {
    return this.done && !this.loading && this.errors.length >= this.retries + 1;
  }
  /**
   * CreateUrl returns the Google Maps JavaScript API script url given the [[LoaderOptions]].
   *
   * @ignore
   * @deprecated
   */
  createUrl() {
    let e = this.url;
    return e += "?callback=__googleMapsCallback", this.apiKey && (e += `&key=${this.apiKey}`), this.channel && (e += `&channel=${this.channel}`), this.client && (e += `&client=${this.client}`), this.libraries.length > 0 && (e += `&libraries=${this.libraries.join(",")}`), this.language && (e += `&language=${this.language}`), this.region && (e += `&region=${this.region}`), this.version && (e += `&v=${this.version}`), this.mapIds && (e += `&map_ids=${this.mapIds.join(",")}`), this.authReferrerPolicy && (e += `&auth_referrer_policy=${this.authReferrerPolicy}`), e;
  }
  deleteScript() {
    const e = document.getElementById(this.id);
    e && e.remove();
  }
  /**
   * Load the Google Maps JavaScript API script and return a Promise.
   * @deprecated, use importLibrary() instead.
   */
  load() {
    return this.loadPromise();
  }
  /**
   * Load the Google Maps JavaScript API script and return a Promise.
   *
   * @ignore
   * @deprecated, use importLibrary() instead.
   */
  loadPromise() {
    return new Promise((e, t) => {
      this.loadCallback((n) => {
        n ? t(n.error) : e(window.google);
      });
    });
  }
  importLibrary(e) {
    return this.execute(), google.maps.importLibrary(e);
  }
  /**
   * Load the Google Maps JavaScript API script with a callback.
   * @deprecated, use importLibrary() instead.
   */
  loadCallback(e) {
    this.callbacks.push(e), this.execute();
  }
  /**
   * Set the script on document.
   */
  setScript() {
    var e, t;
    if (document.getElementById(this.id)) {
      this.callback();
      return;
    }
    const n = {
      key: this.apiKey,
      channel: this.channel,
      client: this.client,
      libraries: this.libraries.length && this.libraries,
      v: this.version,
      mapIds: this.mapIds,
      language: this.language,
      region: this.region,
      authReferrerPolicy: this.authReferrerPolicy
    };
    Object.keys(n).forEach(
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      (r) => !n[r] && delete n[r]
    ), !((t = (e = window == null ? void 0 : window.google) === null || e === void 0 ? void 0 : e.maps) === null || t === void 0) && t.importLibrary || ((r) => {
      let i, u, s, p = "The Google Maps JavaScript API", l = "google", d = "importLibrary", m = "__ib__", c = document, h = window;
      h = h[l] || (h[l] = {});
      const f = h.maps || (h.maps = {}), g = /* @__PURE__ */ new Set(), S = new URLSearchParams(), I = () => (
        // @ts-ignore
        i || (i = new Promise((E, L) => q(this, void 0, void 0, function* () {
          var C;
          yield u = c.createElement("script"), u.id = this.id, S.set("libraries", [...g] + "");
          for (s in r)
            S.set(s.replace(/[A-Z]/g, (j) => "_" + j[0].toLowerCase()), r[s]);
          S.set("callback", l + ".maps." + m), u.src = this.url + "?" + S, f[m] = E, u.onerror = () => i = L(Error(p + " could not load.")), u.nonce = this.nonce || ((C = c.querySelector("script[nonce]")) === null || C === void 0 ? void 0 : C.nonce) || "", c.head.append(u);
        })))
      );
      f[d] ? console.warn(p + " only loads once. Ignoring:", r) : f[d] = (E, ...L) => g.add(E) && I().then(() => f[d](E, ...L));
    })(n);
    const o = this.libraries.map((r) => this.importLibrary(r));
    o.length || o.push(this.importLibrary("core")), Promise.all(o).then(() => this.callback(), (r) => {
      const i = new ErrorEvent("error", { error: r });
      this.loadErrorCallback(i);
    });
  }
  /**
   * Reset the loader state.
   */
  reset() {
    this.deleteScript(), this.done = !1, this.loading = !1, this.errors = [], this.onerrorEvent = null;
  }
  resetIfRetryingFailed() {
    this.failed && this.reset();
  }
  loadErrorCallback(e) {
    if (this.errors.push(e), this.errors.length <= this.retries) {
      const t = this.errors.length * Math.pow(2, this.errors.length);
      console.error(`Failed to load Google Maps script, retrying in ${t} ms.`), setTimeout(() => {
        this.deleteScript(), this.setScript();
      }, t);
    } else
      this.onerrorEvent = e, this.callback();
  }
  callback() {
    this.done = !0, this.loading = !1, this.callbacks.forEach((e) => {
      e(this.onerrorEvent);
    }), this.callbacks = [];
  }
  execute() {
    if (this.resetIfRetryingFailed(), this.done)
      this.callback();
    else {
      if (window.google && window.google.maps && window.google.maps.version) {
        console.warn("Google Maps already loaded outside @googlemaps/js-api-loader.This may result in undesirable behavior as options and script parameters may not match."), this.callback();
        return;
      }
      this.loading || (this.loading = !0, this.setScript());
    }
  }
}
const W = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  DEFAULT_ID: A,
  Loader: y,
  get LoaderStatus() {
    return v;
  }
}, Symbol.toStringTag, { value: "Module" })), z = /* @__PURE__ */ G(W);
(function(a, e) {
  (function(t, n) {
    n(e, z, M);
  })(F, function(t, n, o) {
    function r(s) {
      return s && typeof s == "object" && "default" in s ? s : { default: s };
    }
    var i = /* @__PURE__ */ r(o);
    t.Status = void 0, function(s) {
      s.LOADING = "LOADING", s.FAILURE = "FAILURE", s.SUCCESS = "SUCCESS";
    }(t.Status || (t.Status = {}));
    const u = ({ children: s, render: p, callback: l, ...d }) => {
      const [m, c] = o.useState(t.Status.LOADING);
      return o.useEffect(() => {
        const h = new n.Loader(d), f = (g) => {
          l && l(g, h), c(g);
        };
        f(t.Status.LOADING), h.load().then(() => f(t.Status.SUCCESS), () => f(t.Status.FAILURE));
      }, []), m === t.Status.SUCCESS && s ? i.default.createElement(i.default.Fragment, null, s) : p ? p(m) : i.default.createElement(i.default.Fragment, null);
    };
    t.Wrapper = u, Object.defineProperty(t, "__esModule", { value: !0 });
  });
})(k, k.exports);
var B = k.exports;
const J = "_map__wrapper_4469b_1", V = {
  map__wrapper: J
}, H = (a) => a == "FAILURE" ? (alert("No se pudo geocodificar la ubicación."), /* @__PURE__ */ w(R, {})) : /* @__PURE__ */ w("div", { className: "loader _h-full _w-full" }), Y = ({ onClick: a, onIdle: e, children: t, style: n, ...o }) => {
  const r = N(null), [i, u] = _();
  return b(() => {
    r.current && !i && u(new window.google.maps.Map(r.current, {}));
  }, [r, i]), $(() => {
    i && i.setOptions(o);
  }, [i, o]), b(() => {
    i && (["click", "idle"].forEach((s) => google.maps.event.clearListeners(i, s)), a && i.addListener("click", a), e && i.addListener("idle", () => e(i)));
  }, [i, a, e]), /* @__PURE__ */ O(R, { children: [
    /* @__PURE__ */ w("div", { ref: r, style: n }),
    P.map(t, (s) => {
      if (D(s))
        return U(s, { map: i });
    })
  ] });
}, Q = (a) => {
  const [e, t] = _();
  return b(() => (e || t(new google.maps.Marker()), () => {
    e && e.setMap(null);
  }), [e]), b(() => {
    e && e.setOptions(a);
  }, [e, a]), null;
}, ae = ({ autoLocalization: a, className: e, onClickFunc: t }) => {
  const [n, o] = _(), [r, i] = _(6), [u, s] = _({
    lat: 4.573156896660341,
    lng: -74.0928881330088
  }), p = (c, h) => {
    new window.google.maps.Geocoder().geocode({ location: { lat: c, lng: h } }, (g, S) => {
      if (S === "OK" && g[0]) {
        const I = g[0];
        t && t(I);
      }
    });
  }, l = (c) => {
    var h, f;
    o(c.latLng), p(((h = c.latLng) == null ? void 0 : h.lat()) ?? 0, ((f = c.latLng) == null ? void 0 : f.lng()) ?? 0);
  }, d = (c) => {
    i(c.getZoom()), s(c.getCenter().toJSON());
  }, m = () => {
    navigator.geolocation && navigator.geolocation.getCurrentPosition((c) => {
      const { latitude: h, longitude: f } = c.coords;
      p(h, f), s({
        lat: h,
        lng: f
      });
      const g = new google.maps.LatLng(h, f);
      i(18), o(g);
    });
  };
  return /* @__PURE__ */ O("div", { className: T(V.map__wrapper, e), children: [
    /* @__PURE__ */ w(B.Wrapper, { apiKey: "AIzaSyDz-Hw499LhtG8xYnsEhqOWLEgMWSyPkmw", render: H, children: /* @__PURE__ */ w(Y, { center: u, onClick: l, onIdle: d, zoom: r, style: { flexGrow: "1" }, children: /* @__PURE__ */ w(Q, { position: n }) }) }),
    a ? /* @__PURE__ */ O(x.Button, { onClick: m, className: "_w-fit _inline-flex _items-center _gap-1 _ml-auto _mt-2", children: [
      /* @__PURE__ */ w(K.GpsIcon, { className: "_h-[20px] _w-[20px]" }),
      " Mi ubicación"
    ] }) : null
  ] });
};
export {
  ae as default
};
