import { d as DocContext, j as jsxRuntimeExports, M as MathJax } from "./doenetml-BPsLSRHD.js";
import React__default, { useContext } from "react";
import { u as useDoenetRenderer } from "./useDoenetRenderer-BDC0Rur_.js";
import { A as AnswerResponseButton } from "./AnswerResponseButton-Cr6OG_2W.js";
import { c as calculateValidationState, u as useSubmitActionWithDelay, a as createCheckWorkComponent } from "./useSubmitActionWithDelay-bJj16Fl0.js";
import { D as DescriptionPopover } from "./Description-D-NiLUUD.js";
const answer = React__default.memo(function Answer(props) {
  let {
    componentIdx,
    id,
    SVs,
    docId,
    activityId,
    actions,
    children,
    flags,
    callAction
  } = useDoenetRenderer(props);
  const { showAnswerResponseButton, answerResponseCounts } = useContext(DocContext) || {};
  if (SVs.hidden) {
    return null;
  }
  const validationState = calculateValidationState(SVs);
  const { isPending, submitActionWithPending } = useSubmitActionWithDelay({
    actionKey: "submitAnswer",
    actions,
    callAction,
    validationState,
    justSubmitted: SVs.justSubmitted
  });
  let label = SVs.label;
  if (SVs.labelHasLatex) {
    label = /* @__PURE__ */ jsxRuntimeExports.jsx(MathJax, { hideUntilTypeset: "first", inline: true, dynamic: true, children: label });
  }
  const inputChildrenToRender = SVs.inputChildIndices.map(
    (ind) => children[ind]
  );
  const descriptionChild = SVs.descriptionChildInd !== -1 && children[SVs.descriptionChildInd];
  let descriptionId = void 0;
  let description = null;
  if (descriptionChild) {
    descriptionId = `${id}-description-content`;
    description = /* @__PURE__ */ jsxRuntimeExports.jsx(DescriptionPopover, { children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { id: descriptionId, children: descriptionChild }) });
  }
  let answerResponseButton = null;
  if (showAnswerResponseButton) {
    answerResponseButton = /* @__PURE__ */ jsxRuntimeExports.jsx(
      AnswerResponseButton,
      {
        answerId: id,
        answerComponentIdx: componentIdx,
        docId,
        activityId,
        numResponses: answerResponseCounts?.[id],
        flags
      }
    );
  }
  let checkWorkComponent = createCheckWorkComponent(
    SVs,
    id,
    validationState,
    submitActionWithPending,
    SVs.forceFullCheckWorkButton || !SVs.forceSmallCheckWorkButton,
    isPending
  );
  if (checkWorkComponent) {
    if (description) {
      checkWorkComponent = /* @__PURE__ */ jsxRuntimeExports.jsx(
        "span",
        {
          "aria-details": descriptionId,
          "data-test": "Details Associated",
          children: checkWorkComponent
        }
      );
    }
    if (label) {
      checkWorkComponent = /* @__PURE__ */ jsxRuntimeExports.jsxs(
        "label",
        {
          style: {
            display: "inline-flex",
            maxWidth: "100%"
          },
          children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx("span", { style: { marginRight: "4px" }, children: label }),
            checkWorkComponent
          ]
        }
      );
    }
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(
    "span",
    {
      id,
      style: {
        marginBottom: "4px",
        display: SVs.inline || !SVs.haveBlockInputChild ? "inline-flex" : "flex",
        alignItems: "start"
      },
      children: [
        inputChildrenToRender,
        checkWorkComponent,
        answerResponseButton,
        description
      ]
    }
  );
});
export {
  answer as default
};
