text: import _extends from '@babel/runtime/helpers/esm/extends';
  import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/esm/objectWithoutPropertiesLoose';
  import React, { forwardRef, useState, useMemo, Children, createElement, useLayoutEffect } from 'react';
  import { useThree } from 'react-three-fiber';
  import { Text as Text$1 } from 'r3f-troika';
  
  const Text = /*#__PURE__*/forwardRef((_ref, ref) => {
    let {
      anchorX = 'center',
      anchorY = 'middle',
      children,
      onSync
    } = _ref,
        props = _objectWithoutPropertiesLoose(_ref, ["anchorX", "anchorY", "children", "onSync"]);
  
    const {
      invalidate
    } = useThree();
    const [troikaMesh] = useState(() => new Text$1());
    const [baseMtl, setBaseMtl] = useState();
    const [nodes, text] = useMemo(() => {
      let n = [];
      let t = '';
      Children.forEach(children, (child, index) => {
        if (typeof child === 'string') {
          t += child;
        } else if (child && typeof child === 'object' && child.props.attach === 'material') {
          // Instantiate the base material and grab a reference to it, but don't assign any
          // props, and assign it as the `material`, which Troika will replace behind the scenes.
          n.push( /*#__PURE__*/createElement(child.type, {
            ref: setBaseMtl,
            attach: 'material',
            key: index
          })); // Once the base material has been assigned, grab the resulting upgraded material,
          // and apply the original material props to that.
  
          if (baseMtl) {
            n.push( /*#__PURE__*/React.createElement("primitive", _extends({
              dispose: null,
              object: troikaMesh.material
            }, child.props, {
              key: "baseMtl:" + index,
              attach: null
            })));
          }
        } else {
          n.push(child);
        }
      });
      return [n, t];
    }, [children, baseMtl, troikaMesh.material]);
    useLayoutEffect(() => void troikaMesh.sync(() => {
      invalidate();
      if (onSync) onSync(troikaMesh);
    }));
    return /*#__PURE__*/React.createElement("primitive", _extends({
      dispose: null,
      object: troikaMesh,
      ref: ref,
      text: text,
      anchorX: anchorX,
      anchorY: anchorY
    }, props), nodes);
  });
  
  export { Text };
  