"use client";
import {
  ChatAttachmentAddButton,
  ChatAttachmentChipStrip,
  Label,
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
  Textarea,
  useChatAttachments
} from "./chunk-ZTJVRSN5.js";
import {
  useContactSubmission
} from "./chunk-HICZPTRR.js";
import {
  Button,
  Input
} from "./chunk-MX5MIFWA.js";

// src/components/contact/contact-form.tsx
import { useState } from "react";
import { useForm, Controller } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";

// src/schemas/contact-schema.ts
import { z } from "zod";
var companySizeOptions = [
  "1-10",
  "11-50",
  "51-200",
  "201-500",
  "501-1000",
  "1001+"
];
var referralSourceOptions = [
  "Google",
  "LinkedIn",
  "Twitter/X",
  "Reddit",
  "Friend / Colleague",
  "Other"
];
var defaultHelpCategoryOptions = [
  "Open-Source Alternatives",
  "Vendor Cost Reduction",
  "MSP Best Practices",
  "Partnerships",
  "Press",
  "Other"
];
var LinkedInUrlSchema = z.string().url({ message: "Please enter a valid LinkedIn URL" }).refine(
  (url) => {
    try {
      const host = new URL(url).hostname.toLowerCase();
      return host === "linkedin.com" || host.endsWith(".linkedin.com");
    } catch {
      return false;
    }
  },
  {
    message: "Please enter a valid LinkedIn profile URL"
  }
).optional().or(z.literal(""));
var ContactBaseSchema = z.object({
  name: z.string().min(2, { message: "Name must be at least 2 characters" }).max(255, { message: "Name is too long" }),
  email: z.string().email({ message: "Please enter a valid email address" }).max(255),
  linkedin_url: LinkedInUrlSchema,
  helpCategory: z.string().min(1, { message: "Please select what we can help you with" }).max(255, { message: "Help category is too long" }),
  message: z.string().min(10, { message: "Message must be at least 10 characters" }).max(5e3, { message: "Message is too long (5,000 character limit)" }),
  rdt_cid: z.string().optional()
});
var ContactSchema = ContactBaseSchema.extend({
  companySize: z.string().optional().refine((val) => !val || companySizeOptions.includes(val), {
    message: "Please select a valid company size"
  }),
  referralSource: z.string().optional().refine((val) => !val || referralSourceOptions.includes(val), {
    message: "Please select a valid referral source"
  })
});

// src/components/contact/contact-form.tsx
import { jsx, jsxs } from "react/jsx-runtime";
function ContactForm({
  userId,
  helpCategoryOptions = defaultHelpCategoryOptions,
  rdtCid,
  onSubmitSuccess,
  prefilledReason,
  prefilledMessage,
  hideFields = [],
  defaultValues: defaultValuesProp,
  onCustomSubmit,
  extraTopField,
  attachmentsEnabled = false,
  title = "Hit Us Up",
  subtitle,
  footerText = "We typically respond within 24 hours. We respect your privacy \u2013 no spam, ever.",
  noBorder = false,
  noPadding = false,
  buttonVariant = "accent",
  buttonClassName = "",
  submitLabel = "Send Message",
  submitSuccessLabel = "Message Sent!",
  successRedirectUrl = "/blog#community",
  successToastMessage = "Redirecting you to join our community..."
} = {}) {
  const attachments = useChatAttachments();
  const builtInSubmission = useContactSubmission({
    userId,
    successRedirectUrl,
    successToastMessage
  });
  const [customSubmitting, setCustomSubmitting] = useState(false);
  const isSubmitting = onCustomSubmit ? customSubmitting : builtInSubmission.isSubmitting;
  const isSuccess = onCustomSubmit ? false : builtInSubmission.isSuccess;
  const {
    register,
    handleSubmit,
    control,
    formState: { errors },
    reset
  } = useForm({
    resolver: zodResolver(ContactSchema),
    defaultValues: {
      ...prefilledReason && { helpCategory: prefilledReason },
      ...prefilledMessage && { message: prefilledMessage },
      // Caller-supplied defaults win over the legacy `prefilled*` props
      // (they're the authoritative seed for hidden fields).
      ...defaultValuesProp
    }
  });
  const handleFormSubmit = async (data) => {
    if (isSubmitting) return;
    if (attachmentsEnabled && attachments.hasInflightUploads) return;
    try {
      const payload = { ...data, ...rdtCid && { rdt_cid: rdtCid } };
      const readyAttachments = attachmentsEnabled ? attachments.readyAttachments : [];
      if (onCustomSubmit) {
        setCustomSubmitting(true);
        try {
          await onCustomSubmit(payload, readyAttachments);
        } finally {
          setCustomSubmitting(false);
        }
      } else {
        await builtInSubmission.submit(payload);
      }
      onSubmitSuccess?.();
      reset();
      if (attachmentsEnabled) attachments.clear();
    } catch {
    }
  };
  const showName = !hideFields.includes("name");
  const showEmail = !hideFields.includes("email");
  const showNameEmailRow = showName || showEmail;
  const showCompanySize = !hideFields.includes("companySize");
  const showReferralSource = !hideFields.includes("referralSource");
  const showHelpCategory = !hideFields.includes("helpCategory");
  const showMessage = !hideFields.includes("message");
  return /* @__PURE__ */ jsxs(
    "div",
    {
      className: `h-full flex flex-col ${!noBorder ? "border border-ods-border rounded-2xl md:rounded-3xl" : ""} ${!noPadding ? "p-6 md:p-8 lg:p-10" : ""}`,
      children: [
        (title || subtitle) && /* @__PURE__ */ jsxs("div", { className: "mb-6 md:mb-8", children: [
          title && /* @__PURE__ */ jsx("h2", { className: "text-h2 tracking-[-0.04em] text-ods-text-primary mb-3 md:mb-4", children: title }),
          subtitle && /* @__PURE__ */ jsx("p", { className: "font-['DM_Sans'] font-medium text-[16px] md:text-[18px] leading-[24px] text-ods-text-primary", children: subtitle })
        ] }),
        /* @__PURE__ */ jsxs(
          "form",
          {
            onSubmit: handleSubmit(handleFormSubmit, (validationErrors) => {
              console.warn(
                "[ContactForm] submit blocked by validation:",
                Object.fromEntries(
                  Object.entries(validationErrors).map(([k, v]) => [k, v?.message ?? v])
                )
              );
            }),
            className: "flex flex-col flex-grow space-y-4 md:space-y-6",
            children: [
              !showName && /* @__PURE__ */ jsx("input", { type: "hidden", ...register("name") }),
              !showEmail && /* @__PURE__ */ jsx("input", { type: "hidden", ...register("email") }),
              !showHelpCategory && /* @__PURE__ */ jsx("input", { type: "hidden", ...register("helpCategory") }),
              !showMessage && /* @__PURE__ */ jsx("input", { type: "hidden", ...register("message") }),
              extraTopField,
              showNameEmailRow && /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 md:grid-cols-2 gap-4 md:gap-6", children: [
                showName && /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
                  /* @__PURE__ */ jsxs(Label, { htmlFor: "name", children: [
                    "Your Name",
                    /* @__PURE__ */ jsx("span", { className: "text-ods-accent", children: "*" })
                  ] }),
                  /* @__PURE__ */ jsx(
                    Input,
                    {
                      id: "name",
                      type: "text",
                      ...register("name"),
                      placeholder: "Jane Doe",
                      "aria-invalid": !!errors.name,
                      "aria-describedby": "name-error",
                      className: "bg-ods-card border-ods-border text-ods-text-primary placeholder-ods-text-secondary px-3 h-12"
                    }
                  ),
                  errors.name && /* @__PURE__ */ jsx("span", { id: "name-error", className: "text-ods-error text-xs font-['DM_Sans'] mt-1", children: errors.name.message })
                ] }),
                showEmail && /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
                  /* @__PURE__ */ jsxs(Label, { htmlFor: "email", children: [
                    "Email",
                    /* @__PURE__ */ jsx("span", { className: "text-ods-accent", children: "*" })
                  ] }),
                  /* @__PURE__ */ jsx(
                    Input,
                    {
                      id: "email",
                      type: "email",
                      ...register("email"),
                      placeholder: "jane@company.com",
                      "aria-invalid": !!errors.email,
                      "aria-describedby": "email-error",
                      className: "bg-ods-card border-ods-border text-ods-text-primary placeholder-ods-text-secondary px-3 h-12"
                    }
                  ),
                  errors.email && /* @__PURE__ */ jsx("span", { id: "email-error", className: "text-ods-error text-xs font-['DM_Sans'] mt-1", children: errors.email.message })
                ] })
              ] }),
              (showCompanySize || showReferralSource) && /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 md:grid-cols-2 gap-4 md:gap-6", children: [
                showCompanySize && /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
                  /* @__PURE__ */ jsx(Label, { htmlFor: "companySize", children: "Company Size" }),
                  /* @__PURE__ */ jsx(
                    Controller,
                    {
                      control,
                      name: "companySize",
                      render: ({ field }) => /* @__PURE__ */ jsxs(Select, { onValueChange: field.onChange, defaultValue: field.value, children: [
                        /* @__PURE__ */ jsx(
                          SelectTrigger,
                          {
                            id: "companySize",
                            "aria-label": "Company Size",
                            className: "bg-ods-card border-ods-border text-ods-text-primary h-12 px-3",
                            children: /* @__PURE__ */ jsx(SelectValue, { placeholder: "Select company size" })
                          }
                        ),
                        /* @__PURE__ */ jsx(SelectContent, { children: companySizeOptions.map((opt) => /* @__PURE__ */ jsx(SelectItem, { value: opt, children: opt }, opt)) })
                      ] })
                    }
                  ),
                  errors.companySize && /* @__PURE__ */ jsx("span", { id: "companySize-error", className: "text-ods-error text-xs font-['DM_Sans'] mt-1", children: errors.companySize.message })
                ] }),
                showReferralSource && /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
                  /* @__PURE__ */ jsx(Label, { htmlFor: "referralSource", children: "How did you hear about us?" }),
                  /* @__PURE__ */ jsx(
                    Controller,
                    {
                      control,
                      name: "referralSource",
                      render: ({ field }) => /* @__PURE__ */ jsxs(Select, { onValueChange: field.onChange, defaultValue: field.value, children: [
                        /* @__PURE__ */ jsx(
                          SelectTrigger,
                          {
                            id: "referralSource",
                            "aria-label": "Referral Source",
                            className: "bg-ods-card border-ods-border text-ods-text-primary h-12 px-3",
                            children: /* @__PURE__ */ jsx(SelectValue, { placeholder: "Select an option" })
                          }
                        ),
                        /* @__PURE__ */ jsx(SelectContent, { children: referralSourceOptions.map((opt) => /* @__PURE__ */ jsx(SelectItem, { value: opt, children: opt }, opt)) })
                      ] })
                    }
                  ),
                  errors.referralSource && /* @__PURE__ */ jsx("span", { id: "referralSource-error", className: "text-ods-error text-xs font-['DM_Sans'] mt-1", children: errors.referralSource.message })
                ] })
              ] }),
              showHelpCategory && /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
                /* @__PURE__ */ jsxs(Label, { htmlFor: "helpCategory", children: [
                  "Choose your main interest",
                  /* @__PURE__ */ jsx("span", { className: "text-ods-accent", children: "*" })
                ] }),
                /* @__PURE__ */ jsx(
                  Controller,
                  {
                    control,
                    name: "helpCategory",
                    render: ({ field }) => /* @__PURE__ */ jsxs(Select, { onValueChange: field.onChange, defaultValue: field.value, children: [
                      /* @__PURE__ */ jsx(
                        SelectTrigger,
                        {
                          id: "helpCategory",
                          "aria-label": "Help Category",
                          className: "bg-ods-card border-ods-border text-ods-text-primary h-12 px-3",
                          children: /* @__PURE__ */ jsx(SelectValue, { placeholder: "Choose your main interest" })
                        }
                      ),
                      /* @__PURE__ */ jsx(SelectContent, { children: helpCategoryOptions.map((opt) => /* @__PURE__ */ jsx(SelectItem, { value: opt, children: opt }, opt)) })
                    ] })
                  }
                ),
                errors.helpCategory && /* @__PURE__ */ jsx("span", { id: "helpCategory-error", className: "text-ods-error text-xs font-['DM_Sans'] mt-1", children: errors.helpCategory.message })
              ] }),
              showMessage && /* @__PURE__ */ jsxs("div", { className: "flex flex-col flex-grow", children: [
                /* @__PURE__ */ jsxs(Label, { htmlFor: "message", children: [
                  "Your Message",
                  /* @__PURE__ */ jsx("span", { className: "text-ods-accent", children: "*" })
                ] }),
                /* @__PURE__ */ jsx(
                  Textarea,
                  {
                    id: "message",
                    ...register("message"),
                    placeholder: "Share your current challenges or questions about open-source alternatives...",
                    "aria-invalid": !!errors.message,
                    "aria-describedby": "message-error",
                    className: "bg-ods-card border-ods-border text-ods-text-primary placeholder-ods-text-secondary h-full flex-grow"
                  }
                ),
                errors.message && /* @__PURE__ */ jsx("span", { id: "message-error", className: "text-ods-error text-xs font-['DM_Sans'] mt-1", children: errors.message.message })
              ] }),
              attachmentsEnabled && /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-2", children: [
                /* @__PURE__ */ jsx(
                  ChatAttachmentChipStrip,
                  {
                    attachments: attachments.attachments,
                    onRemove: attachments.removeAttachment,
                    disabled: isSubmitting
                  }
                ),
                /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
                  /* @__PURE__ */ jsx(
                    ChatAttachmentAddButton,
                    {
                      attachmentsEnabled: true,
                      attachmentsCount: attachments.attachments.length,
                      onAddFiles: attachments.addFiles,
                      disabled: isSubmitting
                    }
                  ),
                  /* @__PURE__ */ jsx("span", { className: "text-xs text-ods-text-secondary", children: "Attach files (optional)" })
                ] })
              ] }),
              /* @__PURE__ */ jsxs("div", { className: "flex flex-col md:flex-row gap-4 md:gap-6 items-center justify-end w-full pt-2 mt-auto", children: [
                footerText && /* @__PURE__ */ jsx("p", { className: "font-['DM_Sans'] text-ods-text-secondary text-xs md:text-sm leading-relaxed text-center md:text-left", children: footerText }),
                /* @__PURE__ */ jsx(
                  Button,
                  {
                    type: "submit",
                    loading: isSubmitting,
                    disabled: isSubmitting || isSuccess || attachmentsEnabled && attachments.hasInflightUploads,
                    variant: buttonVariant,
                    className: `w-full md:w-auto ${buttonClassName}`,
                    children: isSuccess ? submitSuccessLabel : submitLabel
                  }
                )
              ] })
            ]
          }
        )
      ]
    }
  );
}

export {
  ContactForm
};
//# sourceMappingURL=chunk-IK2X5YJU.js.map