"use client";
import {
  useSelfFetch
} from "./chunk-L4LAAA5Q.js";
import {
  ChevronButton,
  buildSuggestionUrl,
  serializeJsonLd
} from "./chunk-NR535XIY.js";
import {
  PAGE_HEADING_CLASS,
  SECTION_HEADING_CLASS,
  StatusBadge,
  TAG_BADGE_CLASS
} from "./chunk-FREG2KXV.js";
import {
  cn,
  init_cn
} from "./chunk-XTCBRQN2.js";

// src/components/faq/faq-section.tsx
import { useMemo } from "react";

// src/components/faq-accordion.tsx
import { useRef, useState, useEffect, useCallback } from "react";
init_cn();
import { jsx, jsxs } from "react/jsx-runtime";
var useMeasuredHeight = (isOpen) => {
  const ref = useRef(null);
  const [maxHeight, setMaxHeight] = useState("0px");
  const measure = useCallback(() => {
    if (ref.current) {
      const height = ref.current.scrollHeight;
      setMaxHeight(`${height}px`);
    }
  }, []);
  useEffect(() => {
    if (isOpen) {
      measure();
    } else {
      setMaxHeight("0px");
    }
  }, [isOpen, measure]);
  return { ref, maxHeight };
};
function FaqAccordion({ items, defaultOpenIds = [] }) {
  const [openSet, setOpenSet] = useState(new Set(defaultOpenIds));
  const toggle = (id) => {
    setOpenSet((prev) => {
      const next = new Set(prev);
      if (next.has(id)) next.delete(id);
      else next.add(id);
      return next;
    });
  };
  return /* @__PURE__ */ jsx("div", { className: "rounded-3xl border border-ods-border divide-y divide-ods-border bg-ods-card overflow-hidden", children: items.map((item) => {
    const isOpen = openSet.has(item.id);
    const { ref, maxHeight } = useMeasuredHeight(isOpen);
    return /* @__PURE__ */ jsxs(
      "div",
      {
        className: cn("group transition-colors hover:bg-[#1E1E1E]", isOpen ? "bg-ods-bg" : "bg-transparent"),
        children: [
          /* @__PURE__ */ jsxs(
            "div",
            {
              role: "button",
              tabIndex: 0,
              onClick: () => toggle(item.id),
              onKeyDown: (e) => {
                if (e.key === "Enter" || e.key === " ") {
                  e.preventDefault();
                  toggle(item.id);
                }
              },
              "aria-expanded": isOpen,
              className: "flex w-full items-center justify-between px-6 md:px-8 py-6 text-left focus:outline-none transition-colors cursor-pointer",
              children: [
                /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 flex-col items-start gap-2 pr-4", children: [
                  /* @__PURE__ */ jsx("h3", { children: item.question }),
                  item.badge && /* @__PURE__ */ jsx(
                    StatusBadge,
                    {
                      text: item.badge.toUpperCase(),
                      variant: "card",
                      className: TAG_BADGE_CLASS
                    }
                  )
                ] }),
                /* @__PURE__ */ jsx("div", { className: "flex-shrink-0", children: /* @__PURE__ */ jsx(
                  ChevronButton,
                  {
                    "aria-label": isOpen ? "Collapse question" : "Expand question",
                    size: "md",
                    isExpanded: isOpen,
                    backgroundColor: "transparent",
                    borderColor: "#3A3A3A"
                  }
                ) })
              ]
            }
          ),
          /* @__PURE__ */ jsx(
            "div",
            {
              style: { maxHeight, transition: "max-height 0.35s ease-in-out, opacity 0.35s ease-in-out", opacity: isOpen ? 1 : 0 },
              className: "overflow-hidden group-hover:bg-[#1E1E1E]/30",
              children: /* @__PURE__ */ jsx("div", { ref, className: "px-6 md:px-8 pb-6 text-ods-text-primary text-h4", children: item.answer })
            }
          )
        ]
      },
      item.id
    );
  }) });
}

// src/components/faq/json-ld.ts
var DEFAULT_NAME = "Frequently Asked Questions";
var DEFAULT_DESCRIPTION = "Answers to common questions.";
function baseFaqSchema(opts = {}) {
  return {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    name: opts.name ?? DEFAULT_NAME,
    description: opts.description ?? DEFAULT_DESCRIPTION,
    ...opts.url ? { url: opts.url } : {}
  };
}
function buildFaqJsonLdFromFaqs(faqs, opts = {}) {
  return {
    ...baseFaqSchema(opts),
    mainEntity: faqs.map((faq) => ({
      "@type": "Question",
      name: faq.question,
      acceptedAnswer: {
        "@type": "Answer",
        text: faq.answer
      }
    }))
  };
}

// src/components/faq/faq-section.tsx
import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var DEFAULT_HEADING_TEXT = "Frequently Asked Questions";
function buildFaqsUrl(entityType, entityId, minResults, apiBaseUrl = "") {
  return buildSuggestionUrl("/api/faqs", { apiBaseUrl, entityType, entityId, count: minResults });
}
function toAccordionItems(faqs) {
  return faqs.map((faq) => ({
    id: faq.id,
    question: faq.question,
    answer: faq.answer,
    badge: faq.section || void 0
  }));
}
function FaqSkeleton() {
  return /* @__PURE__ */ jsxs2("div", { className: "space-y-8 animate-pulse", children: [
    /* @__PURE__ */ jsx2("div", { className: "h-12 md:h-14 w-2/3 rounded bg-ods-border" }),
    /* @__PURE__ */ jsx2("div", { className: "rounded-3xl border border-ods-border overflow-hidden bg-ods-card divide-y divide-ods-border w-full", children: Array.from({ length: 8 }).map((_, idx) => /* @__PURE__ */ jsxs2("div", { className: "flex items-center justify-between px-6 md:px-8 py-6", children: [
      /* @__PURE__ */ jsx2("div", { className: "h-6 w-5/6 rounded bg-ods-border" }),
      /* @__PURE__ */ jsx2("div", { className: "h-10 w-10 rounded-md bg-ods-border" })
    ] }, idx)) })
  ] });
}
function FaqSection({
  initialFaqs,
  entityType,
  entityId,
  heading,
  variant,
  emitJsonLd = false,
  jsonLd,
  className,
  minResults,
  apiBaseUrl = ""
}) {
  const url = buildFaqsUrl(entityType, entityId, minResults, apiBaseUrl);
  const initialData = useMemo(
    () => initialFaqs ? { faqs: initialFaqs } : void 0,
    [initialFaqs]
  );
  const { data, isLoading, error } = useSelfFetch(url, { initialData });
  const faqs = data?.faqs ?? [];
  const isEmbedded = variant ? variant === "embedded" : heading === null;
  const headingNode = heading === void 0 ? isEmbedded ? (
    // Embedded default is an <h2> — a host page already owns the <h1>,
    // and the schema/heading pair must read "Frequently Asked Questions"
    // (one FAQ heading per page, never tag/section names).
    /* @__PURE__ */ jsx2("h2", { className: SECTION_HEADING_CLASS, children: DEFAULT_HEADING_TEXT })
  ) : /* @__PURE__ */ jsx2("h1", { className: PAGE_HEADING_CLASS, children: DEFAULT_HEADING_TEXT }) : heading;
  if (error && isEmbedded) {
    return null;
  }
  if (!isLoading && !error && faqs.length === 0 && isEmbedded) {
    return null;
  }
  if (isLoading && faqs.length === 0) {
    if (isEmbedded) {
      return /* @__PURE__ */ jsx2("div", { className, children: /* @__PURE__ */ jsx2(FaqSkeleton, {}) });
    }
    return /* @__PURE__ */ jsx2("main", { className: className ?? "bg-ods-bg", children: /* @__PURE__ */ jsx2("div", { className: "max-w-[1920px] px-6 md:px-20 py-6 md:py-10 mx-auto", children: /* @__PURE__ */ jsx2(FaqSkeleton, {}) }) });
  }
  if (error && faqs.length === 0) {
    return /* @__PURE__ */ jsx2("main", { className: className ?? "bg-ods-bg flex items-center justify-center py-20", children: /* @__PURE__ */ jsx2("p", { className: "text-ods-text-primary font-medium", children: "Failed to load FAQs. Please try again later." }) });
  }
  const schema = emitJsonLd ? buildFaqJsonLdFromFaqs(faqs, jsonLd) : null;
  const accordion = /* @__PURE__ */ jsx2(FaqAccordion, { items: toAccordionItems(faqs) });
  const body = isEmbedded ? /* @__PURE__ */ jsxs2("section", { className: className ?? "space-y-10", children: [
    headingNode,
    accordion
  ] }) : /* @__PURE__ */ jsx2("main", { className: className ?? "bg-ods-bg", children: /* @__PURE__ */ jsxs2("div", { className: "max-w-[1920px] px-6 md:px-20 py-6 md:py-10 mx-auto space-y-10", children: [
    headingNode,
    accordion
  ] }) });
  return /* @__PURE__ */ jsxs2(Fragment, { children: [
    body,
    schema && /* @__PURE__ */ jsx2(
      "script",
      {
        type: "application/ld+json",
        dangerouslySetInnerHTML: { __html: serializeJsonLd(schema) }
      }
    )
  ] });
}

export {
  FaqAccordion,
  FaqSection
};
//# sourceMappingURL=chunk-K4K4X62E.js.map