type StructuredCloneFallback = <T>(value: T) => T;

/**
 * Installs browser compatibility shims required by Tinyflow.
 */
export function installTinyflowBrowserCompat() {
  if (
    typeof globalThis.structuredClone === 'function' ||
    typeof JSON === 'undefined'
  ) {
    return;
  }

  const fallback: StructuredCloneFallback = (value) =>
    value == null ? value : JSON.parse(JSON.stringify(value));

  Object.defineProperty(globalThis, 'structuredClone', {
    configurable: true,
    value: fallback,
    writable: true,
  });
}
