"use strict";
"use client";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Element_exports = {};
__export(Element_exports, {
  Element: () => Element
});
module.exports = __toCommonJS(Element_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_react_page = require("../../../state/react-page");
var import_ElementRegistration = require("./ElementRegistration");
var import_ElementReference = require("./ElementReference");
var import_ElementData = require("./ElementData");
var import_element_imperative_handle = require("../element-imperative-handle");
var import_find_dom_node = require("../find-dom-node");
var import_FallbackComponent = require("../../../components/shared/FallbackComponent");
var import_ErrorBoundary = require("../../../components/shared/ErrorBoundary");
const Element = (0, import_react.memo)(
  (0, import_react.forwardRef)(function Element2({ element }, ref) {
    const useFindDomNodeRef = (0, import_react.useRef)(true);
    const imperativeHandleRef = (0, import_react.useRef)(new import_element_imperative_handle.ElementImperativeHandle());
    const findDomNodeCallbackRef = (0, import_react.useCallback)((current) => {
      if (useFindDomNodeRef.current === true) {
        imperativeHandleRef.current.callback(() => current?.() ?? null);
      }
    }, []);
    const elementCallbackRef = (0, import_react.useCallback)((current) => {
      useFindDomNodeRef.current = false;
      imperativeHandleRef.current.callback(() => current);
    }, []);
    (0, import_react.useImperativeHandle)(ref, () => imperativeHandleRef.current, []);
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ElementRegistration.ElementRegistration, { componentHandle: imperativeHandleRef.current, elementKey: element.key, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_find_dom_node.FindDomNode, { ref: findDomNodeCallbackRef, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ErrorBoundary.ErrorBoundary, { FallbackComponent: ErrorFallback, children: (0, import_react_page.isElementReference)(element) ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_ElementReference.ElementReference,
      {
        ref: elementCallbackRef,
        elementReference: element
      },
      element.key
    ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ElementData.ElementData, { ref: elementCallbackRef, elementData: element }, element.key) }) }) });
  })
);
function ErrorFallback() {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_FallbackComponent.FallbackComponent, { text: `Error rendering component` });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Element
});
//# sourceMappingURL=Element.js.map