import { i as useAppSelector, k as mainSlice, l as renderersLoadComponent, _ as __variableDynamicImportRuntimeHelper } from "./doenetml-cB_aZGJ5.js";
import React__default, { useState, useEffect } from "react";
function useDoenetRenderer(props, initializeChildrenOnConstruction = true) {
  const actions = props.componentInstructions.actions;
  const componentIdx = props.componentInstructions.componentIdx;
  const effectiveIdx = props.componentInstructions.effectiveIdx;
  const id = props.componentInstructions.id;
  const rendererName = props.coreId + componentIdx;
  const [renderersToLoad, setRenderersToLoad] = useState({});
  const componentInfo = useAppSelector(
    (state) => mainSlice.selectors.componentInfo(state)[rendererName]
  );
  const {
    stateValues,
    sourceOfUpdate = {},
    ignoreUpdate,
    childrenInstructions,
    prefixForIds
  } = componentInfo || {
    stateValues: {},
    sourceOfUpdate: {},
    ignoreUpdate: false,
    childrenInstructions: [],
    prefixForIds: ""
  };
  let children = [];
  const loadMoreRenderers = Object.keys(renderersToLoad).length === 0;
  for (let childInstructions of childrenInstructions) {
    let child = createChildFromInstructions(
      childInstructions,
      loadMoreRenderers
    );
    children.push(child);
  }
  useEffect(() => {
    if (Object.keys(renderersToLoad).length > 0) {
      renderersLoadComponent(
        Object.values(renderersToLoad),
        Object.keys(renderersToLoad)
      ).then((newRendererClasses) => {
        Object.assign(props.rendererClasses, newRendererClasses);
        setRenderersToLoad({});
      });
    }
  }, [renderersToLoad, props.rendererClasses]);
  function createChildFromInstructions(childInstructions, loadMoreRenderers2) {
    if (typeof childInstructions === "string" || childInstructions === null) {
      return childInstructions;
    }
    let propsForChild = {
      key: props.coreId + childInstructions.componentIdx,
      componentInstructions: childInstructions,
      rendererClasses: props.rendererClasses,
      coreId: props.coreId,
      docId: props.docId,
      activityId: props.activityId,
      callAction: props.callAction,
      fetchExternalDoenetML: props.fetchExternalDoenetML,
      requestScrollTo: props.requestScrollTo,
      flags: props.flags
    };
    let rendererClass = props.rendererClasses[childInstructions.rendererType];
    if (!rendererClass) {
      if (loadMoreRenderers2) {
        setRenderersToLoad((old) => {
          let rendererPromises = { ...old };
          if (!(childInstructions.rendererType in rendererPromises)) {
            rendererPromises[childInstructions.rendererType] = __variableDynamicImportRuntimeHelper(/* @__PURE__ */ Object.assign({ "./renderers/_error.tsx": () => import("./_error-B2Gfg26n.js"), "./renderers/alert.tsx": () => import("./alert-BgUNPTas.js"), "./renderers/angle.tsx": () => import("./angle-4khi3Gpz.js"), "./renderers/answer.tsx": () => import("./answer-ADd1Odq6.js"), "./renderers/asList.tsx": () => import("./asList-nL7pKQdO.js"), "./renderers/blockQuote.tsx": () => import("./blockQuote-D5VP0SQA.js"), "./renderers/boolean.tsx": () => import("./boolean-BYGLOrf6.js"), "./renderers/booleanInput.tsx": () => import("./booleanInput-BIEiTh5B.js"), "./renderers/br.tsx": () => import("./br-CaaRdiad.js"), "./renderers/button.tsx": () => import("./button-Gs_zAYln.js"), "./renderers/c.tsx": () => import("./c-BqaUyNjh.js"), "./renderers/cell.tsx": () => import("./cell-BxX2pLNE.js"), "./renderers/choiceInput.tsx": () => import("./choiceInput-sdr8Da04.js"), "./renderers/circle.tsx": () => import("./circle-C_mD6R3l.js"), "./renderers/cobwebPolyline.tsx": () => import("./cobwebPolyline-MS8lszzW.js"), "./renderers/codeEditor.tsx": () => import("./codeEditor-ijUhZSId.js"), "./renderers/containerBlock.tsx": () => import("./containerBlock-BBo7WyHi.js"), "./renderers/containerInline.tsx": () => import("./containerInline-g0ttabia.js"), "./renderers/curve.tsx": () => import("./curve-DSJzwzZJ.js"), "./renderers/ellipsis.tsx": () => import("./ellipsis-tKHkJX4W.js"), "./renderers/em.tsx": () => import("./em-BUO9A18x.js"), "./renderers/embed.tsx": () => import("./embed-DgxxjdNv.js"), "./renderers/feedback.tsx": () => import("./feedback-CRU2JHCi.js"), "./renderers/figure.tsx": () => import("./figure-DEVe_Mtn.js"), "./renderers/footnote.tsx": () => import("./footnote-BUt1mH7u.js"), "./renderers/graph.tsx": () => import("./graph-DQks9OE0.js"), "./renderers/hint.tsx": () => import("./hint-DjYQ5RwY.js"), "./renderers/hr.tsx": () => import("./hr-Counw2pT.js"), "./renderers/image.tsx": () => import("./image-BTCwHbHq.js"), "./renderers/label.tsx": () => import("./label-B3yo9TQ9.js"), "./renderers/legend.tsx": () => import("./legend-BIoAdplT.js"), "./renderers/line.tsx": () => import("./line-BTPCMkD8.js"), "./renderers/lineSegment.tsx": () => import("./lineSegment-By_vAjAS.js"), "./renderers/list.tsx": () => import("./list-DnFTOI2u.js"), "./renderers/lq.tsx": () => import("./lq-dy3_odnM.js"), "./renderers/lsq.tsx": () => import("./lsq-ge2Qsxti.js"), "./renderers/math.tsx": () => import("./math-CpW1Pa8j.js"), "./renderers/mathInput.tsx": () => import("./mathInput-Tuj1PiT1.js"), "./renderers/matrixInput.tsx": () => import("./matrixInput-CdybE5Jy.js"), "./renderers/mdash.tsx": () => import("./mdash-YkDtsE-G.js"), "./renderers/nbsp.tsx": () => import("./nbsp-CacQ_bq-.js"), "./renderers/ndash.tsx": () => import("./ndash-CA8MhCn6.js"), "./renderers/number.tsx": () => import("./number-DGXv4ocr.js"), "./renderers/orbitalDiagram.tsx": () => import("./orbitalDiagram-CfgRUQdn.js"), "./renderers/orbitalDiagramInput.tsx": () => import("./orbitalDiagramInput-BWWnt2oL.js"), "./renderers/p.tsx": () => import("./p-Bwjsum0A.js"), "./renderers/paginatorControls.tsx": () => import("./paginatorControls-DWGC6uPQ.js"), "./renderers/pegboard.tsx": () => import("./pegboard-Be2DM5oA.js"), "./renderers/point.tsx": () => import("./point-DL7IWzbe.js"), "./renderers/polygon.tsx": () => import("./polygon-DHZqDszE.js"), "./renderers/polyline.tsx": () => import("./polyline-YxjKGuPg.js"), "./renderers/pre.tsx": () => import("./pre-MMU3dRLr.js"), "./renderers/pretzel.tsx": () => import("./pretzel-CLvuBleI.js"), "./renderers/q.tsx": () => import("./q-tnH4DYXH.js"), "./renderers/ray.tsx": () => import("./ray-Ap0xvzfA.js"), "./renderers/ref.tsx": () => import("./ref-CxnHiFWN.js"), "./renderers/regionBetweenCurveXAxis.tsx": () => import("./regionBetweenCurveXAxis-LGGxHH9y.js"), "./renderers/regionBetweenCurves.tsx": () => import("./regionBetweenCurves-BTGZlAVG.js"), "./renderers/row.tsx": () => import("./row-B3--oUHC.js"), "./renderers/rq.tsx": () => import("./rq-BO-GAhSI.js"), "./renderers/rsq.tsx": () => import("./rsq--qlWMgo9.js"), "./renderers/section.tsx": () => import("./section-DmGHiO1J.js"), "./renderers/sideBySide.tsx": () => import("./sideBySide-BnaEeFhG.js"), "./renderers/slider.tsx": () => import("./slider-CnqVSSeX.js"), "./renderers/solution.tsx": () => import("./solution-BGWKkKdA.js"), "./renderers/spreadsheet.tsx": () => import("./spreadsheet-BOtQ90f-.js"), "./renderers/sq.tsx": () => import("./sq-L-z3Jev3.js"), "./renderers/subsetOfRealsInput.tsx": () => import("./subsetOfRealsInput-Dtctl-KL.js"), "./renderers/summaryStatistics.tsx": () => import("./summaryStatistics-CpKTA9AS.js"), "./renderers/table.tsx": () => import("./table-CcQSRsPg.js"), "./renderers/tabular.tsx": () => import("./tabular-Dj0P8hbh.js"), "./renderers/tag.tsx": () => import("./tag-xuEcsArh.js"), "./renderers/text.tsx": () => import("./text-Dp6e2NvT.js"), "./renderers/textInput.tsx": () => import("./textInput-BoKu6vhZ.js"), "./renderers/vector.tsx": () => import("./vector-Blp4W9Em.js"), "./renderers/video.tsx": () => import("./video-DX6V7bh6.js") }), `./renderers/${childInstructions.rendererType}.tsx`, 3);
          }
          return rendererPromises;
        });
      }
      return null;
    }
    let child = React__default.createElement(rendererClass, propsForChild);
    return child;
  }
  let rendererType = props.componentInstructions.rendererType;
  const callAction = (argObj) => {
    if (!argObj.componentIdx) {
      argObj = { ...argObj };
      argObj.componentIdx = componentIdx;
    }
    if (!argObj.rendererType) {
      argObj = { ...argObj };
      argObj.rendererType = rendererType;
    }
    return props.callAction(argObj);
  };
  return {
    componentIdx: effectiveIdx,
    id: prefixForIds + id,
    SVs: stateValues,
    docId: props.docId,
    activityId: props.activityId,
    actions,
    children,
    sourceOfUpdate,
    ignoreUpdate,
    rendererName,
    initializeChildren: () => {
    },
    callAction,
    doenetViewerUrl: props.doenetViewerUrl,
    fetchExternalDoenetML: props.fetchExternalDoenetML,
    requestScrollTo: props.requestScrollTo,
    flags: props.flags
  };
}
export {
  useDoenetRenderer as u
};
