import { jsxs as A, jsx as v, Fragment as R } from "react/jsx-runtime";
import N, { useState as S, useRef as M, useEffect as E, 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";
import "../../index.esm-BQxOvVZs.js";
import "../../context/generalContext/index.js";
import "../../context/index.js";
import "../Buttons/variants.js";
import "../../index-xVF7D3FM.js";
import "../../clsx-7d8NmkQq.js";
var O = { 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, o) {
  function n(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(o.next(l));
      } catch (d) {
        i(d);
      }
    }
    function s(l) {
      try {
        p(o.throw(l));
      } catch (d) {
        i(d);
      }
    }
    function p(l) {
      l.done ? r(l.value) : n(l.value).then(u, s);
    }
    p((o = o.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 o, n, r;
    if (Array.isArray(e)) {
      if (o = e.length, o != t.length)
        return !1;
      for (n = o; n-- !== 0; )
        if (!a(e[n], t[n]))
          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), o = r.length, o !== Object.keys(t).length)
      return !1;
    for (n = o; n-- !== 0; )
      if (!Object.prototype.hasOwnProperty.call(t, r[n]))
        return !1;
    for (n = o; n-- !== 0; ) {
      var i = r[n];
      if (!a(e[i], t[i]))
        return !1;
    }
    return !0;
  }
  return e !== e && t !== t;
};
const k = "__googleMapsScriptId";
var w;
(function(a) {
  a[a.INITIALIZED = 0] = "INITIALIZED", a[a.LOADING = 1] = "LOADING", a[a.SUCCESS = 2] = "SUCCESS", a[a.FAILURE = 3] = "FAILURE";
})(w || (w = {}));
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: o, client: n, id: r = k, 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 = o, this.client = n, this.id = r || k, 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 ? w.FAILURE : this.done ? w.SUCCESS : this.loading ? w.LOADING : w.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((o) => {
        o ? t(o.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 o = {
      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(o).forEach(
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      (r) => !o[r] && delete o[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(), _ = new URLSearchParams(), I = () => (
        // @ts-ignore
        i || (i = new Promise((b, C) => q(this, void 0, void 0, function* () {
          var L;
          yield u = c.createElement("script"), u.id = this.id, _.set("libraries", [...g] + "");
          for (s in r)
            _.set(s.replace(/[A-Z]/g, (j) => "_" + j[0].toLowerCase()), r[s]);
          _.set("callback", l + ".maps." + m), u.src = this.url + "?" + _, f[m] = b, u.onerror = () => i = C(Error(p + " could not load.")), u.nonce = this.nonce || ((L = c.querySelector("script[nonce]")) === null || L === void 0 ? void 0 : L.nonce) || "", c.head.append(u);
        })))
      );
      f[d] ? console.warn(p + " only loads once. Ignoring:", r) : f[d] = (b, ...C) => g.add(b) && I().then(() => f[d](b, ...C));
    })(o);
    const n = this.libraries.map((r) => this.importLibrary(r));
    n.length || n.push(this.importLibrary("core")), Promise.all(n).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 B = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  DEFAULT_ID: k,
  Loader: y,
  get LoaderStatus() {
    return w;
  }
}, Symbol.toStringTag, { value: "Module" })), J = /* @__PURE__ */ G(B);
(function(a, e) {
  (function(t, o) {
    o(e, J, N);
  })(F, function(t, o, n) {
    function r(s) {
      return s && typeof s == "object" && "default" in s ? s : { default: s };
    }
    var i = /* @__PURE__ */ r(n);
    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] = n.useState(t.Status.LOADING);
      return n.useEffect(() => {
        const h = new o.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 });
  });
})(O, O.exports);
var V = O.exports;
const z = "_map__wrapper_4469b_1", W = {
  map__wrapper: z
}, H = (a) => a == "FAILURE" ? (alert("No se pudo geocodificar la ubicación."), /* @__PURE__ */ v(R, {})) : /* @__PURE__ */ v("div", { className: "loader _h-full _w-full" }), Y = ({ onClick: a, onIdle: e, children: t, style: o, ...n }) => {
  const r = M(null), [i, u] = S();
  return E(() => {
    r.current && !i && u(new window.google.maps.Map(r.current, {}));
  }, [r, i]), $(() => {
    i && i.setOptions(n);
  }, [i, n]), E(() => {
    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__ */ A(R, { children: [
    /* @__PURE__ */ v("div", { ref: r, style: o }),
    P.map(t, (s) => {
      if (D(s))
        return U(s, { map: i });
    })
  ] });
}, Q = (a) => {
  const [e, t] = S();
  return E(() => (e || t(new google.maps.Marker()), () => {
    e && e.setMap(null);
  }), [e]), E(() => {
    e && e.setOptions(a);
  }, [e, a]), null;
}, fe = ({ autoLocalization: a, className: e, onClickFunc: t }) => {
  const [o, n] = S(), [r, i] = S(6), [u, s] = S({
    lat: 4.573156896660341,
    lng: -74.0928881330088
  }), p = (c, h) => {
    new window.google.maps.Geocoder().geocode({ location: { lat: c, lng: h } }, (g, _) => {
      if (_ === "OK" && g[0]) {
        const I = g[0];
        t && t(I);
      }
    });
  }, l = (c) => {
    var h, f;
    n(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), n(g);
    });
  };
  return /* @__PURE__ */ A("div", { className: T(W.map__wrapper, e), children: [
    /* @__PURE__ */ v(V.Wrapper, { apiKey: "AIzaSyDAGvCdi_ieHCMYjVyVi-Rk3ARvNqu_4vE", render: H, children: /* @__PURE__ */ v(Y, { center: u, onClick: l, onIdle: d, zoom: r, style: { flexGrow: "1" }, children: /* @__PURE__ */ v(Q, { position: o }) }) }),
    a ? /* @__PURE__ */ A(x.Button, { onClick: m, className: "_w-fit _inline-flex _items-center _gap-1 _ml-auto _mt-2", children: [
      /* @__PURE__ */ v(K.GpsIcon, { className: "_h-[20px] _w-[20px]" }),
      " Mi ubicación"
    ] }) : null
  ] });
};
export {
  fe as default
};
