import { j as jsxRuntimeExports } from "./doenetml-cB_aZGJ5.js";
import { F as FontAwesomeIcon } from "./index.es-DKppzeUo.js";
import { c as faLevelDownAlt, d as faCheck, e as faTimes, g as faCloud } from "./index-D_-RBdRX.js";
function calculateValidationState(SVs) {
  let validationState = "unvalidated";
  if (SVs.justSubmitted || SVs.numAttemptsLeft < 1) {
    if (SVs.creditAchieved === 1) {
      validationState = "correct";
    } else if (SVs.creditAchieved === 0) {
      validationState = "incorrect";
    } else {
      validationState = "partialcorrect";
    }
  }
  return validationState;
}
function createCheckWorkComponent(SVs, id, validationState, submitAnswer, showText) {
  if (!SVs.showCheckWork) {
    return null;
  }
  const buttonClassNames = ["check-work"];
  const tabIndex = SVs.disabled ? -1 : 0;
  let buttonContent = null;
  let liveLabel = void 0;
  let otherLabel = void 0;
  if (validationState === "unvalidated") {
    buttonClassNames.push("check-work-unvalidated");
    const checkWorkText = SVs.showCorrectness ? SVs.submitLabel : SVs.submitLabelNoCorrectness;
    otherLabel = checkWorkText;
    buttonContent = showText ? /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      "  ",
      checkWorkText
    ] }) : null;
    buttonContent = /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { "aria-hidden": true, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(
        FontAwesomeIcon,
        {
          icon: faLevelDownAlt,
          transform: { rotate: 90 },
          title: otherLabel
        }
      ),
      buttonContent
    ] });
  } else if (SVs.showCorrectness) {
    buttonClassNames.push(`check-work-${validationState}`);
    if (validationState === "correct") {
      liveLabel = "Correct";
      buttonContent = showText ? /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: "  Correct" }) : null;
      buttonContent = /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { "aria-hidden": true, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          FontAwesomeIcon,
          {
            icon: faCheck,
            title: liveLabel
          }
        ),
        buttonContent
      ] });
    } else if (validationState === "incorrect") {
      liveLabel = "Incorrect";
      buttonContent = showText ? /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: "  Incorrect" }) : null;
      buttonContent = /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { "aria-hidden": true, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          FontAwesomeIcon,
          {
            icon: faTimes,
            title: liveLabel
          }
        ),
        buttonContent
      ] });
    } else {
      const percent = Math.round(SVs.creditAchieved * 100);
      const partialText = SVs.creditIsReducedByAttempt ? `${percent}% Credit` : `${percent}% Correct`;
      buttonContent = /* @__PURE__ */ jsxRuntimeExports.jsx("span", { "aria-hidden": true, children: showText ? partialText : `${percent} %` });
      liveLabel = partialText;
    }
  } else {
    buttonClassNames.push("check-work-response-saved");
    liveLabel = "Response Saved";
    buttonContent = showText ? /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: "  Response Saved" }) : null;
    buttonContent = /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { "aria-hidden": true, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(FontAwesomeIcon, { icon: faCloud, title: liveLabel }),
      buttonContent
    ] });
  }
  let button = /* @__PURE__ */ jsxRuntimeExports.jsxs(
    "button",
    {
      className: buttonClassNames.join(" "),
      id: id + "_button",
      tabIndex,
      disabled: SVs.disabled,
      onClick: submitAnswer,
      children: [
        buttonContent,
        /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { className: "visually-hidden", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx("span", { "aria-live": "polite", "aria-atomic": true, children: liveLabel }),
          /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: otherLabel })
        ] })
      ]
    }
  );
  let messages = [];
  if (SVs.creditIsReducedByAttempt) {
    if (SVs.numIncorrectSubmissions === 0) {
      messages.push("Max credit available: 100%");
    } else if (SVs.creditAchieved > 0) {
      messages.push(
        `Max credit available: ${Math.round(100 * SVs.creditFactorUsed)}%`
      );
    } else {
      messages.push(
        `Max credit available: ${Math.round(100 * SVs.nextCreditFactor)}%`
      );
    }
  }
  if (SVs.numAttemptsLeft < 1) {
    messages.push("no attempts remaining");
  } else if (SVs.numAttemptsLeft === 1) {
    messages.push("1 attempt remaining");
  } else if (Number.isFinite(SVs.numAttemptsLeft)) {
    messages.push(`${SVs.numAttemptsLeft} attempts remaining`);
  }
  if (messages.length > 0) {
    const message = messages.join("; ");
    button = /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      button,
      /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { children: [
        "(",
        message,
        ")"
      ] })
    ] });
  }
  return button;
}
export {
  createCheckWorkComponent as a,
  calculateValidationState as c
};
