import { d as DocContext, j as jsxRuntimeExports } from "./doenetml-cB_aZGJ5.js";
import React__default, { useContext, useRef } from "react";
import { u as useDoenetRenderer } from "./useDoenetRenderer-DN3Z9CW3.js";
const ref = React__default.memo(function Ref(props) {
  const { id, SVs, children, requestScrollTo } = useDoenetRenderer(props);
  const { doenetViewerUrl } = useContext(DocContext) || {};
  const aRef = useRef(null);
  const { targetForATag, url, scrollOnlyLink } = getURLFromRef({
    url: SVs.url,
    activityId: SVs.activityId,
    activityUrlPostfix: SVs.activityUrlPostfix,
    targetRendererId: SVs.targetRendererId,
    doenetViewerUrl
  });
  function handleLinkClick(e) {
    if (!scrollOnlyLink) {
      return;
    }
    if (requestScrollTo) {
      e.preventDefault();
      const targetId = url.substring(1);
      const targetElement = document.getElementById(targetId);
      if (targetElement) {
        requestScrollTo(targetElement.offsetTop);
      }
    }
  }
  if (SVs.hidden) {
    return null;
  }
  let linkContent = children;
  if (children.length === 0) {
    linkContent = SVs.linkText;
  }
  if (SVs.createButton) {
    const refClass = "ref-button" + (SVs.disabled ? " ref-button-disabled" : "");
    if (targetForATag === "_blank") {
      return /* @__PURE__ */ jsxRuntimeExports.jsx("span", { id, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
        "button",
        {
          id: id + "_button",
          onClick: () => window.open(url, targetForATag || void 0),
          disabled: SVs.disabled,
          className: refClass,
          children: SVs.linkText
        }
      ) });
    } else {
      return /* @__PURE__ */ jsxRuntimeExports.jsx("span", { id, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
        "button",
        {
          id: id + "_button",
          onClick: () => {
            window.location.href = url;
          },
          disabled: SVs.disabled,
          className: refClass,
          children: SVs.linkText
        }
      ) });
    }
  } else {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      "a",
      {
        style: {
          color: "var(--mainBlue)",
          borderRadius: "5px"
        },
        target: targetForATag || void 0,
        id,
        href: url,
        onClick: handleLinkClick,
        ref: aRef,
        children: linkContent
      }
    );
  }
});
function getURLFromRef({
  url,
  activityId,
  activityUrlPostfix,
  targetRendererId,
  doenetViewerUrl = "https://doenet.org/activityViewer"
}) {
  let targetForATag = null;
  let scrollOnlyLink = false;
  if (targetRendererId) {
    url = `#${targetRendererId}`;
    scrollOnlyLink = true;
  } else if (activityId) {
    url = `${doenetViewerUrl}/${activityId}${activityUrlPostfix}`;
    targetForATag = "_blank";
  } else {
    scrollOnlyLink = url.startsWith("#");
    targetForATag = scrollOnlyLink ? null : "_blank";
  }
  return { targetForATag, url, scrollOnlyLink };
}
export {
  ref as default,
  getURLFromRef
};
