import { jsxs, jsx, Fragment } from "react/jsx-runtime";
import * as React from "react";
import { useState } from "react";
import { useRouter, isRedirect } from "@tanstack/react-router";
import { T as TSS_SERVER_FUNCTION, g as getServerFnById, a as createServerFn } from "./server-CWcU6oWp.js";
import { z } from "zod";
import { w as whatsappHref } from "./router-Dahr8H-r.js";
import { toast } from "sonner";
function useServerFn(serverFn) {
  const router = useRouter();
  return React.useCallback(async (...args) => {
    try {
      const res = await serverFn(...args);
      if (isRedirect(res)) throw res;
      return res;
    } catch (err) {
      if (isRedirect(err)) {
        err.options._fromLocation = router.stores.location.get();
        return router.navigate(router.resolveRedirect(err).options);
      }
      throw err;
    }
  }, [router, serverFn]);
}
var createSsrRpc = (functionId) => {
  const url = "/_serverFn/" + functionId;
  const serverFnMeta = { id: functionId };
  const fn = async (...args) => {
    return (await getServerFnById(functionId))(...args);
  };
  return Object.assign(fn, {
    url,
    serverFnMeta,
    [TSS_SERVER_FUNCTION]: true
  });
};
const leadSchema = z.object({
  full_name: z.string().trim().min(1, "Required").max(120),
  phone: z.string().trim().min(4, "Required").max(40),
  email: z.string().trim().email("Invalid email").max(200),
  budget: z.string().trim().max(80).optional().or(z.literal("")),
  property_interest: z.string().trim().max(120).optional().or(z.literal("")),
  preferred_location: z.string().trim().max(120).optional().or(z.literal("")),
  message: z.string().trim().max(2e3).optional().or(z.literal("")),
  source_page: z.string().trim().max(120).optional().or(z.literal("")),
  // honeypot — must be empty
  company: z.string().max(0).optional().or(z.literal(""))
});
const submitLead = createServerFn({
  method: "POST"
}).inputValidator((data) => leadSchema.parse(data)).handler(createSsrRpc("e211e8c27eee0a1053129b7769a37cb2e31b3dc869a84a1c647eee19cfb57993"));
function LeadForm({
  sourcePage,
  variant = "full",
  prefilledInterest,
  projectName,
  ctaLabel = "Download Brochure",
  onSuccess,
  onMessageChange
}) {
  const send = useServerFn(submitLead);
  const [submitting, setSubmitting] = useState(false);
  const [done, setDone] = useState(false);
  async function onSubmit(e) {
    e.preventDefault();
    if (submitting) return;
    const fd = new FormData(e.currentTarget);
    const raw = Object.fromEntries(fd.entries());
    const parsed = leadSchema.safeParse({ ...raw, source_page: sourcePage });
    if (!parsed.success) {
      toast.error(parsed.error.issues[0]?.message ?? "Please check the form");
      return;
    }
    setSubmitting(true);
    try {
      await send({ data: parsed.data });
      setDone(true);
      e.target.reset();
      toast.success("Opening WhatsApp to send your details…");
      const d = parsed.data;
      const lines = [
        projectName ? `Hi Bridgefield Properties, I'd like the *${projectName}* brochure.` : `Hi Bridgefield Properties, I'd like more information.`,
        ``,
        `*Name:* ${d.full_name}`,
        `*Phone:* ${d.phone}`,
        `*Email:* ${d.email}`,
        d.budget ? `*Budget:* ${d.budget}` : "",
        d.property_interest ? `*Interest:* ${d.property_interest}` : "",
        d.preferred_location ? `*Location:* ${d.preferred_location}` : "",
        d.message ? `*Message:* ${d.message}` : ""
      ].filter(Boolean);
      const url = whatsappHref(lines.join("\n"));
      window.open(url, "_blank", "noopener,noreferrer");
      onSuccess?.();
    } catch (err) {
      toast.error(err instanceof Error ? err.message : "Could not submit. Please try again.");
    } finally {
      setSubmitting(false);
    }
  }
  if (done && variant === "compact") {
    return /* @__PURE__ */ jsxs("div", { className: "bg-white p-6 text-center ring-1 ring-navy/10", children: [
      /* @__PURE__ */ jsx("p", { className: "font-display text-lg font-bold text-navy", children: "Request received" }),
      /* @__PURE__ */ jsx("p", { className: "mt-2 text-sm text-navy/75", children: "Continue your chat on WhatsApp — a senior advisor will reply shortly." })
    ] });
  }
  const inputCls = "w-full bg-ivory/40 border-b border-navy/20 px-0 py-3 text-base text-navy placeholder:text-navy/45 focus:border-gold outline-none transition-colors sm:text-sm";
  const labelCls = "block text-[11px] font-semibold uppercase tracking-widest text-navy/75 mb-1.5";
  return /* @__PURE__ */ jsxs("form", { onSubmit, className: "space-y-4", children: [
    /* @__PURE__ */ jsx("input", { type: "text", name: "company", tabIndex: -1, autoComplete: "off", className: "hidden", "aria-hidden": true }),
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4 sm:grid-cols-2", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("label", { className: labelCls, children: "Full Name *" }),
        /* @__PURE__ */ jsx("input", { required: true, name: "full_name", type: "text", autoComplete: "name", className: inputCls })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("label", { className: labelCls, children: "Phone *" }),
        /* @__PURE__ */ jsx("input", { required: true, name: "phone", type: "tel", autoComplete: "tel", inputMode: "tel", className: inputCls })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx("label", { className: labelCls, children: "Email *" }),
      /* @__PURE__ */ jsx("input", { required: true, name: "email", type: "email", autoComplete: "email", inputMode: "email", className: inputCls })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4 sm:grid-cols-2", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("label", { className: labelCls, children: "Budget" }),
        /* @__PURE__ */ jsxs("select", { name: "budget", className: inputCls, defaultValue: "", children: [
          /* @__PURE__ */ jsx("option", { value: "", children: "Select" }),
          /* @__PURE__ */ jsx("option", { children: "AED 1M – 3M" }),
          /* @__PURE__ */ jsx("option", { children: "AED 3M – 5M" }),
          /* @__PURE__ */ jsx("option", { children: "AED 5M – 10M" }),
          /* @__PURE__ */ jsx("option", { children: "AED 10M+" })
        ] })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("label", { className: labelCls, children: "Property Interest" }),
        /* @__PURE__ */ jsxs("select", { name: "property_interest", className: inputCls, defaultValue: prefilledInterest ?? "", children: [
          /* @__PURE__ */ jsx("option", { value: "", children: "Select" }),
          /* @__PURE__ */ jsx("option", { children: "Townhouse" }),
          /* @__PURE__ */ jsx("option", { children: "Villa" }),
          /* @__PURE__ */ jsx("option", { children: "Off-Plan" }),
          /* @__PURE__ */ jsx("option", { children: "Ready Property" })
        ] })
      ] })
    ] }),
    variant === "full" && /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("label", { className: labelCls, children: "Preferred Location" }),
        /* @__PURE__ */ jsx("input", { name: "preferred_location", type: "text", className: inputCls, placeholder: "e.g. MBR City" })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("label", { className: labelCls, children: "Message" }),
        /* @__PURE__ */ jsx(
          "textarea",
          {
            name: "message",
            rows: 3,
            className: inputCls + " resize-none",
            onChange: (e) => onMessageChange?.(e.target.value)
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsxs(
      "button",
      {
        type: "submit",
        disabled: submitting,
        className: "flex w-full items-center justify-center gap-2 bg-gold py-4 text-[11px] font-bold uppercase tracking-[0.2em] text-navy transition-all hover:bg-navy hover:text-ivory disabled:opacity-60",
        children: [
          /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", className: "size-4", fill: "currentColor", "aria-hidden": true, children: /* @__PURE__ */ jsx("path", { d: "M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487 1.7.733 2.504.794 3.404.667.547-.077 1.758-.718 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347Z" }) }),
          submitting ? "Sending…" : ctaLabel
        ]
      }
    ),
    /* @__PURE__ */ jsx("p", { className: "text-center text-[11px] text-navy/65", children: "Your details are sent to our advisor on WhatsApp. We respect your privacy." })
  ] });
}
export {
  LeadForm as L
};
