"use strict";

var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SSRRenderMeasure = void 0;
var _react = _interopRequireWildcard(require("react"));
var _isSsr = require("../core-utils/is-ssr");
var _RenderMarker = require("./RenderMarker");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
/**
 * Props for SSRRenderMeasure component
 */

function SSRRenderMeasureImpl(_ref) {
  var onSSRMeasure = _ref.onSSRMeasure,
    segmentName = _ref.segmentName,
    startTimestampRef = _ref.startTimestampRef,
    children = _ref.children;
  var wasMeasured = (0, _react.useRef)(false);
  var handleOnRender = (0, _react.useCallback)(function () {
    if (wasMeasured.current) {
      return;
    }
    onSSRMeasure === null || onSSRMeasure === void 0 || onSSRMeasure({
      segmentName: segmentName,
      startTimestamp: startTimestampRef.current,
      endTimestamp: performance.now()
    });
    wasMeasured.current = true;
  }, [onSSRMeasure, segmentName, startTimestampRef]);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, children, /*#__PURE__*/_react.default.createElement(_RenderMarker.RenderMarker, {
    onRender: handleOnRender
  }));
}
var SSRRenderMeasureNoOp = /*#__PURE__*/(0, _react.memo)(function (_ref2) {
  var children = _ref2.children;
  return children;
});

/**
 * Component for measuring render performance during Server-Side Rendering (SSR).
 *
 * This component wraps content to measure its render duration in SSR mode.
 * On client builds, it's optimized to a no-op component with zero performance overhead.
 * On SSR builds, it captures timing data and reports it via the `onSSRMeasure` callback.
 *
 * **How it works:**
 * - Measures from `startTimestampRef.current` (component start) to when `RenderMarker` is rendered
 * - Uses `RenderMarker` to detect when the render completes successfully
 * - Only reports the measurement once (protected by `wasMeasured` ref)
 * - If child components throw errors during render, the measurement will not be reported
 *
 * **Usage pattern:**
 * 1. Create `startTimestampRef` as the **first line** in your component using `useRef(performance.now())`
 * 2. Wrap your content with `SSRRenderMeasure` at the end of your component
 * 3. Provide the `onSSRMeasure` callback to receive timing data
 *
 * @example
 * ```tsx
 * function FullPageEditor({ onSSRMeasure }) {
 *   // CRITICAL: Must be the first line for accurate timing
 *   const startTimestampRef = useRef(performance.now());
 *
 *   // ...component logic, hooks, etc.
 *
 *   return (
 *     <SSRRenderMeasure
 *       segmentName="ssr-app/render/fullPageEditor"
 *       startTimestampRef={startTimestampRef}
 *       onSSRMeasure={onSSRMeasure}
 *     >
 *       <EditorContent />
 *     </SSRRenderMeasure>
 *   );
 * }
 * ```
 *
 * @example
 * ```tsx
 * // Handling the measurement callback
 * const handleSSRMeasure = (measure) => {
 *   const duration = measure.endTimestamp - measure.startTimestamp;
 *   console.log(`${measure.segmentName} rendered in ${duration}ms`);
 *   // Send to analytics, logging, etc.
 * };
 * ```
 */
var SSRRenderMeasure = exports.SSRRenderMeasure = (0, _isSsr.isSSR)() ? SSRRenderMeasureImpl : SSRRenderMeasureNoOp;