import { useCallback, useEffect, useRef, useState } from "react";
import { SurveyContainerProps } from "@formbricks/types/formbricks-surveys";
import { getEndingCloseDelay } from "@/lib/ending-close";
import { getSurveyDisplayName, hasSurveyInstructions } from "@/lib/survey-page";
import { getSurveyLanguageTag, isRTLLanguage } from "@/lib/utils";
import { SurveyContainer } from "../wrappers/survey-container";
import { Survey } from "./survey";

export function RenderSurvey(props: Readonly<SurveyContainerProps>) {
  const [isOpen, setIsOpen] = useState(true);
  const onFinishedTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  const closeTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  const { onClose, onLanguageChange } = props;
  const [activeLanguageCode, setActiveLanguageCode] = useState(props.languageCode);

  // Direction is derived during render from the ACTIVE language and the survey, the same inputs the
  // lang attribute below resolves from, so the two cannot disagree. Keying it on props.languageCode
  // instead would miss every change that does not come from the host: a language switch, or a
  // fallback when the active language is no longer configured. That combination is what produced
  // conflicting attributes — lang="en-US" next to dir="rtl" — on a survey whose selected language
  // had been removed. isRTLLanguage reads the survey's language list, and for a survey with none
  // configured it sniffs the direction from the content itself.
  const dir = isRTLLanguage(props.survey, activeLanguageCode) ? "rtl" : "ltr";

  // Survey declares its own language on the #fbjs root (WCAG 3.1.1). This is the only place that
  // covers embedded and app surveys: a link survey's host also sets <html lang>, but the JS widget
  // is dropped into someone else's document and must never touch it, so without this it inherits
  // the host page's language no matter what language the survey is in.
  //
  // The active language lives in Survey's own state, and it already reports every change through
  // onLanguageChange. Wrapping that callback keeps a single source of truth rather than adding a
  // second channel out of Survey.
  //
  // Only the CODE is held in state; the tag is resolved during render. That keeps this callback's
  // identity stable — `onLanguageChange` is a public prop and Survey's reporting effect is keyed on it,
  // so a callback rebuilt on every survey-object change would re-fire that effect on renders where no
  // language changed (the editor preview builds a fresh survey object on every keystroke). It also
  // means a survey edited mid-session — a language disabled or removed — re-resolves the tag on the
  // next render rather than leaving a stale one on the DOM.
  const handleLanguageChange = useCallback(
    (languageCode: string) => {
      setActiveLanguageCode(languageCode);
      onLanguageChange?.(languageCode);
    },
    [onLanguageChange]
  );

  const languageTag = getSurveyLanguageTag(props.survey, activeLanguageCode);

  const close = useCallback(() => {
    if (onFinishedTimeoutRef.current) {
      clearTimeout(onFinishedTimeoutRef.current);
      onFinishedTimeoutRef.current = null;
    }

    if (closeTimeoutRef.current) {
      clearTimeout(closeTimeoutRef.current);
      closeTimeoutRef.current = null;
    }

    setIsOpen(false);

    closeTimeoutRef.current = setTimeout(() => {
      onClose?.();
    }, 1000);
  }, [onClose]);

  useEffect(() => {
    return () => {
      if (onFinishedTimeoutRef.current) {
        clearTimeout(onFinishedTimeoutRef.current);
      }

      if (closeTimeoutRef.current) {
        clearTimeout(closeTimeoutRef.current);
      }
    };
  }, []);

  if (!isOpen) {
    return null;
  }

  const mode = props.mode ?? "modal";
  const hasOverlay = props.overlay && props.overlay !== "none";
  // A modal survey with no overlay appears over a page the user is still working in, often mid-form.
  // Moving the caret into the survey on open would pull them out of the field they are typing in, so
  // leave focus where it is. With an overlay the page is blocked anyway, so taking focus is correct.
  // Only ever force this off — everything else keeps the existing default (see survey.tsx).
  const autoFocus = props.autoFocus ?? (mode === "modal" && !hasOverlay ? false : undefined);

  return (
    <SurveyContainer
      mode={mode}
      placement={props.placement}
      overlay={props.overlay}
      overlayAppearance={props.survey.overlayAppearance}
      clickOutside={props.clickOutside}
      onClose={close}
      onCardRectChange={props.onCardRectChange}
      isOpen={isOpen}
      dir={dir}
      surveyName={getSurveyDisplayName(props.survey.name)}
      hasInstructions={hasSurveyInstructions(props.survey)}
      lang={languageTag}>
      <Survey
        {...props}
        onLanguageChange={handleLanguageChange}
        // eslint-disable-next-line jsx-a11y/no-autofocus -- renamed to autoFocusEnabled; focus is imperative
        autoFocus={autoFocus}
        clickOutside={hasOverlay ? props.clickOutside : true}
        onClose={close}
        onFinished={(responseId?: string) => {
          props.onFinished?.(responseId);

          const closeDelay = getEndingCloseDelay({ survey: props.survey, mode });
          if (closeDelay !== null) {
            onFinishedTimeoutRef.current = setTimeout(close, closeDelay);
          }
        }}
        dir={dir}
        // The language switch reports through onLanguageChange, which moves `dir` above; a host's
        // own setDir is still not forwarded.
        setDir={undefined}
      />
    </SurveyContainer>
  );
}
