import { i as useAppSelector, k as mainSlice, l as renderersLoadComponent, _ as __variableDynamicImportRuntimeHelper } from "./doenetml-BPsLSRHD.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/GraphFrame.tsx": () => import("./GraphFrame-J0M02sWi.js"), "./renderers/JSXGraphRenderer.tsx": () => import("./JSXGraphRenderer-DO77WbpG.js").then((n) => n.c), "./renderers/_error.tsx": () => import("./_error-D-B0QE1R.js"), "./renderers/alert.tsx": () => import("./alert-CtpSGNTE.js"), "./renderers/angle.tsx": () => import("./angle-C3nTxBpA.js"), "./renderers/answer.tsx": () => import("./answer-iherF_Ue.js"), "./renderers/asList.tsx": () => import("./asList-CWV42Rnb.js"), "./renderers/blockQuote.tsx": () => import("./blockQuote-DkmEqdnL.js"), "./renderers/boolean.tsx": () => import("./boolean-BzWMY9qH.js"), "./renderers/booleanInput.tsx": () => import("./booleanInput-e9QY4Yeg.js"), "./renderers/br.tsx": () => import("./br-CnzmPT4W.js"), "./renderers/button.tsx": () => import("./button-BccjDsuu.js"), "./renderers/c.tsx": () => import("./c-DDyz2883.js"), "./renderers/cell.tsx": () => import("./cell-M7jAfFZu.js"), "./renderers/choiceInput.tsx": () => import("./choiceInput-CXKWjd-w.js"), "./renderers/circle.tsx": () => import("./circle-DnTFrUlZ.js"), "./renderers/cobwebPolyline.tsx": () => import("./cobwebPolyline-BCQP5_8-.js"), "./renderers/codeEditor.tsx": () => import("./codeEditor-Bt7fhXur.js"), "./renderers/containerBlock.tsx": () => import("./containerBlock-BAxMM_GX.js"), "./renderers/containerInline.tsx": () => import("./containerInline-PzGVSe0F.js"), "./renderers/curve.tsx": () => import("./curve-B_6yPCQ-.js"), "./renderers/ellipsis.tsx": () => import("./ellipsis-CNj-fTMC.js"), "./renderers/em.tsx": () => import("./em-CrDqwajk.js"), "./renderers/embed.tsx": () => import("./embed-BgbZV8O0.js"), "./renderers/feedback.tsx": () => import("./feedback-Cm_ODofK.js"), "./renderers/figure.tsx": () => import("./figure-bIux9QCx.js"), "./renderers/footnote.tsx": () => import("./footnote-kbY8OOx4.js"), "./renderers/graph.tsx": () => import("./graph-BgpHyjPe.js"), "./renderers/hint.tsx": () => import("./hint-Bt5vzlbI.js"), "./renderers/hr.tsx": () => import("./hr-wFUTpsfi.js"), "./renderers/image.tsx": () => import("./image-CodSP273.js"), "./renderers/label.tsx": () => import("./label-CcNymF_o.js"), "./renderers/legend.tsx": () => import("./legend-DwaWSOy_.js"), "./renderers/line.tsx": () => import("./line-CJD7vj9G.js"), "./renderers/lineSegment.tsx": () => import("./lineSegment-BZ0N-EpL.js"), "./renderers/list.tsx": () => import("./list-BrYFba57.js"), "./renderers/lq.tsx": () => import("./lq-CTzxQBCk.js"), "./renderers/lsq.tsx": () => import("./lsq-kwvDKP-v.js"), "./renderers/math.tsx": () => import("./math-DVCDP784.js"), "./renderers/mathInput.tsx": () => import("./mathInput-BUVoci2u.js"), "./renderers/matrixInput.tsx": () => import("./matrixInput-CpRPq5ks.js"), "./renderers/mdash.tsx": () => import("./mdash-B_7Cu0Ri.js"), "./renderers/nbsp.tsx": () => import("./nbsp-CHf8Q8DF.js"), "./renderers/ndash.tsx": () => import("./ndash-0aYL8Qkv.js"), "./renderers/number.tsx": () => import("./number-CgQHq3R4.js"), "./renderers/orbitalDiagram.tsx": () => import("./orbitalDiagram-DDA71Be8.js"), "./renderers/orbitalDiagramInput.tsx": () => import("./orbitalDiagramInput-kqjMVbLP.js"), "./renderers/p.tsx": () => import("./p-B9GhSbKR.js"), "./renderers/paginatorControls.tsx": () => import("./paginatorControls-DZPW8xk7.js"), "./renderers/pegboard.tsx": () => import("./pegboard-mT5S8hJV.js"), "./renderers/point.tsx": () => import("./point-Bs0EaL8K.js"), "./renderers/polygon.tsx": () => import("./polygon-Dbs82CAL.js"), "./renderers/polyline.tsx": () => import("./polyline-BwRbywva.js"), "./renderers/pre.tsx": () => import("./pre--_c4mBV-.js"), "./renderers/prefigure.tsx": () => import("./prefigure-Kgg90sn6.js"), "./renderers/pretzel.tsx": () => import("./pretzel-BbzEjac4.js"), "./renderers/q.tsx": () => import("./q-DdQ8b-90.js"), "./renderers/ray.tsx": () => import("./ray-ClsUUEFD.js"), "./renderers/ref.tsx": () => import("./ref-pIsZ5-wj.js"), "./renderers/regionBetweenCurveXAxis.tsx": () => import("./regionBetweenCurveXAxis-Dy5VNGtv.js"), "./renderers/regionBetweenCurves.tsx": () => import("./regionBetweenCurves-DLhTdWI2.js"), "./renderers/row.tsx": () => import("./row-DMlJfEtZ.js"), "./renderers/rq.tsx": () => import("./rq-BUqpE3s1.js"), "./renderers/rsq.tsx": () => import("./rsq-DY_dCroy.js"), "./renderers/section.tsx": () => import("./section-CCxr1crY.js"), "./renderers/sideBySide.tsx": () => import("./sideBySide-IxyhzD-7.js"), "./renderers/slider.tsx": () => import("./slider-BXLnW13x.js"), "./renderers/solution.tsx": () => import("./solution-GvScJFyJ.js"), "./renderers/spreadsheet.tsx": () => import("./spreadsheet-DE4vIcri.js"), "./renderers/sq.tsx": () => import("./sq-BTlGh4S4.js"), "./renderers/subsetOfRealsInput.tsx": () => import("./subsetOfRealsInput-DR6KFWZT.js"), "./renderers/summaryStatistics.tsx": () => import("./summaryStatistics-BlJNiPrY.js"), "./renderers/table.tsx": () => import("./table-CQVKMMBB.js"), "./renderers/tabular.tsx": () => import("./tabular-DhGdx4wj.js"), "./renderers/tag.tsx": () => import("./tag-2NM36H-e.js"), "./renderers/text.tsx": () => import("./text-DGW_HTGd.js"), "./renderers/textInput.tsx": () => import("./textInput-zyQx9XHH.js"), "./renderers/vector.tsx": () => import("./vector-Cow4NTCP.js"), "./renderers/video.tsx": () => import("./video-CwMZw3JE.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
};
