import { j as e } from "../../../../../../../_virtual/jsx-runtime.js";
import { useState as k, useRef as y } from "react";
import "../../../../../../../node_modules/.pnpm/@formkit_auto-animate@0.8.2/node_modules/@formkit/auto-animate/index.js";
import "../../../../../../../node_modules/.pnpm/react-hot-toast@2.5.2_react-dom@18.3.0-canary-3d9b20132-20240124_react@18.3.0-canary-3d_c02c1e17bf76c343fd22d1f22394ecf5/node_modules/react-hot-toast/dist/index.js";
import { TbSlash as V } from "../../../../../../../node_modules/.pnpm/react-icons@5.3.0_react@18.3.0-canary-3d9b20132-20240124/node_modules/react-icons/tb/index.js";
import { useDebounceCallback as v } from "../../../../../../../node_modules/.pnpm/usehooks-ts@3.1.1_react@18.3.0-canary-3d9b20132-20240124/node_modules/usehooks-ts/dist/index.js";
import { FieldTitle as x } from "../field-factory.js";
import { tx as D } from "../../../../../../style-system/dist/twind.js";
import { usePageQueries as w, useSitemap as E, usePagePathParams as S, useDraftHelpers as R } from "../../../../../../../use-page-data.js";
import { useDynamicTextEditor as I } from "../../../hooks/use-editable-text.js";
import { TextArea as T } from "../../../../../../../node_modules/.pnpm/@radix-ui_themes@3.1.4_@types_react-dom@18.3.6_@types_react@18.3.3__@types_react@18.3.3_46b1dabc193c5ff5696ee783983c694e/node_modules/@radix-ui/themes/dist/esm/components/text-area.js";
import { Root as j, Slot as A } from "../../../../../../../node_modules/.pnpm/@radix-ui_themes@3.1.4_@types_react-dom@18.3.6_@types_react@18.3.3__@types_react@18.3.3_46b1dabc193c5ff5696ee783983c694e/node_modules/@radix-ui/themes/dist/esm/components/text-field.js";
import { Root as P, Item as N } from "../../../../../../../node_modules/.pnpm/@radix-ui_themes@3.1.4_@types_react-dom@18.3.6_@types_react@18.3.3__@types_react@18.3.3_46b1dabc193c5ff5696ee783983c694e/node_modules/@radix-ui/themes/dist/esm/components/segmented-control.js";
import { Root as z, Trigger as F, Content as _, Group as q, Item as G } from "../../../../../../../node_modules/.pnpm/@radix-ui_themes@3.1.4_@types_react-dom@18.3.6_@types_react@18.3.3__@types_react@18.3.3_46b1dabc193c5ff5696ee783983c694e/node_modules/@radix-ui/themes/dist/esm/components/select.js";
const te = (c) => {
  const { currentValue: l, onChange: d, title: a, description: s, placeholder: h, schema: u, brickId: p, noDynamic: r } = c, n = w(), o = v(d, 300), m = I(c);
  return n.length && !r && !u["ui:no-dynamic"] ? /* @__PURE__ */ e.jsxs("div", { className: "field field-string basis-full flex flex-col gap-1 max-w-full", children: [
    /* @__PURE__ */ e.jsx(x, { title: a, description: s }),
    /* @__PURE__ */ e.jsx("div", { className: "field field-string flex items-start gap-1.5", children: m })
  ] }) : /* @__PURE__ */ e.jsxs("div", { className: "field field-string basis-full", children: [
    /* @__PURE__ */ e.jsx(x, { title: a, description: s }),
    u["ui:multiline"] ? /* @__PURE__ */ e.jsx(
      T,
      {
        defaultValue: l,
        onChange: (f) => o(f.target.value),
        className: D("!mt-1.5 scrollbar-thin", u["ui:textarea-class"] ?? "min-h-24"),
        placeholder: h,
        size: u["ui:textarea-font-size"] ?? "2",
        spellCheck: !!u["ui:spellcheck"]
      }
    ) : /* @__PURE__ */ e.jsx(
      j,
      {
        defaultValue: l,
        onChange: (f) => o(f.target.value),
        className: "!mt-1",
        size: "2",
        placeholder: h,
        spellCheck: !!u["ui:spellcheck"]
      }
    )
  ] });
}, le = (c) => {
  const { currentValue: l, onChange: d, title: a, description: s, placeholder: h } = c, u = v(d, 300), p = (l || "").toString().replace(/^\//, ""), r = S();
  return /* @__PURE__ */ e.jsxs("div", { className: "field field-path basis-full", children: [
    /* @__PURE__ */ e.jsx(x, { title: a, description: s }),
    /* @__PURE__ */ e.jsx(
      j,
      {
        defaultValue: p,
        onChange: (n) => u(n.target.value),
        className: "!mt-1.5",
        placeholder: h,
        children: /* @__PURE__ */ e.jsx(A, { children: /* @__PURE__ */ e.jsx(V, { className: "bg-transparent h-5 w-5 rounded-md stroke-1 !-ml-1 !-mr-2 -rotate-[20deg]" }) })
      }
    ),
    r.length > 0 && /* @__PURE__ */ e.jsxs("div", { className: "mt-1.5 text-xs text-gray-500", children: [
      /* @__PURE__ */ e.jsxs("span", { children: [
        "You'll be able to use the variable",
        r.length > 1 ? "s" : "",
        " "
      ] }),
      " ",
      r.map((n, o) => /* @__PURE__ */ e.jsxs("span", { className: "font-medium", children: [
        n,
        o === r.length - 2 ? " and " : o < r.length - 1 ? ", " : "",
        " "
      ] }, n)),
      "in queries."
    ] })
  ] });
}, ae = (c) => {
  const { currentValue: l, onChange: d, title: a, description: s, placeholder: h, schema: u, brickId: p } = c, r = E(), [n, o] = k(
    l?.startsWith("http") || r.length === 1 ? "url" : "pageId"
  ), m = w(), f = m.length ? "External / Dynamic" : "External link", b = I(c);
  return /* @__PURE__ */ e.jsxs("div", { className: "flex-1 max-w-full", children: [
    /* @__PURE__ */ e.jsxs("div", { className: "flex justify-between flex-1 gap-1", children: [
      /* @__PURE__ */ e.jsx(x, { title: a, description: s }),
      /* @__PURE__ */ e.jsxs(
        P,
        {
          onValueChange: (t) => {
            o(t), d(null);
          },
          defaultValue: n,
          size: "1",
          className: "mt-0.5",
          radius: "medium",
          children: [
            /* @__PURE__ */ e.jsx(N, { value: "url", className: "[&_.rt-SegmentedControlItemLabel]:!px-2.5", children: f }),
            /* @__PURE__ */ e.jsx(N, { value: "pageId", className: "[&_.rt-SegmentedControlItemLabel]:!px-0", children: "Internal" })
          ]
        }
      )
    ] }),
    n === "url" ? m.length > 0 ? /* @__PURE__ */ e.jsx("div", { className: "field field-string basis-full flex items-start gap-1.5 mt-2", children: b }) : /* @__PURE__ */ e.jsx(
      j,
      {
        defaultValue: l?.startsWith("http") ? l : "",
        onChange: (t) => d(t.target.value),
        className: "!mt-2 max-w-full",
        placeholder: "https://example.com",
        spellCheck: !!u["ui:spellcheck"]
      }
    ) : /* @__PURE__ */ e.jsxs(
      z,
      {
        defaultValue: l ?? void 0,
        size: "2",
        onValueChange: (t) => d(t),
        children: [
          /* @__PURE__ */ e.jsx(
            F,
            {
              radius: "large",
              variant: "surface",
              className: "!mt-2 !w-full !flex-1 !max-w-full truncate",
              placeholder: "Select a page"
            }
          ),
          /* @__PURE__ */ e.jsx(_, { position: "popper", children: /* @__PURE__ */ e.jsx(q, { children: Object.entries(r).map(([, t]) => /* @__PURE__ */ e.jsx(G, { value: t.label, children: t.label }, t.id)) }) })
        ]
      }
    )
  ] });
}, se = (c) => {
  const { currentValue: l, onChange: d, title: a, description: s, placeholder: h, formData: u, brickId: p } = c, { updateBrickProps: r } = R(), [n, o] = k([]), m = y(null), f = (t) => {
    const C = t.target.value, i = n.find((g) => g.label === C);
    if (i) {
      console.log("Matched result:", i), d(i.label), console.log("address changed for brick %s", p), p && r(p, {
        address: i.label,
        lat: i.lat,
        lng: i.lng,
        tooltip: ""
      });
      const g = new CustomEvent("geoAddressSelected", {
        detail: {
          address: i.label,
          placeId: i.placeId,
          lat: i.lat,
          lng: i.lng
        }
      });
      window.dispatchEvent(g), o([]);
      return;
    }
    console.log("No match found for %s, continuing search...", C), b(t);
  }, b = v((t) => {
    if (!t.target.value.trim()) {
      o([]);
      return;
    }
    m.current && m.current.abort("New search initiated");
    const i = new AbortController();
    m.current = i, L(t.target.value, m.current.signal).then((g) => {
      o(g);
    }).catch((g) => {
      console.error("Error fetching geolocation data:", g), o([]);
    });
  }, 300);
  return /* @__PURE__ */ e.jsxs("div", { className: "field field-address basis-full", children: [
    /* @__PURE__ */ e.jsx(x, { title: a, description: s }),
    /* @__PURE__ */ e.jsx("datalist", { id: "search-results", children: n.map((t) => /* @__PURE__ */ e.jsx("option", { value: t.label }, t.placeId)) }),
    /* @__PURE__ */ e.jsx(
      j,
      {
        defaultValue: l,
        onChange: f,
        className: "!mt-1",
        placeholder: h,
        list: "search-results"
      }
    )
  ] });
};
function L(c, l = null) {
  const d = `https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(c)}&format=jsonv2&layer=address&limit=8`;
  return fetch(d, {
    signal: l
  }).then((a) => {
    if (!a.ok)
      throw new Error("Network response was not ok");
    return a.json();
  }).then((a) => a.map((s) => ({
    label: s.display_name,
    placeId: s.place_id,
    lat: s.lat,
    lng: s.lon
  })));
}
export {
  se as GeoAddressField,
  le as PathField,
  te as StringField,
  ae as UrlOrPageIdField
};
//# sourceMappingURL=string.js.map
