import React from 'react';

interface SvgTextLogoProps {
  text: string;
  className?: string;
  fontSize?: number;
  fontWeight?: number | string;
  letterSpacing?: number;
  dominantBaseline?: 'auto' | 'baseline' | 'before-edge' | 'text-before-edge' | 'middle' | 'central' | 'after-edge' | 'text-after-edge' | 'ideographic' | 'alphabetic' | 'hanging' | 'mathematical' | 'inherit';
}

const SvgTextLogo: React.FC<SvgTextLogoProps> = ({
  text,
  className = '',
  fontSize = 32,
  fontWeight = 700,
  letterSpacing = 0,
  dominantBaseline = 'central',
}) => {
  const textElement = React.useRef<SVGTextElement>(null);
  const [bbox, setBbox] = React.useState({ x: 0, y: 0, width: 0, height: 0 });

  React.useEffect(() => {
    if (textElement.current) {
      try {
        const bboxValue = textElement.current.getBBox();
        setBbox(bboxValue);
      } catch (error) {
        console.error('Error getting text bbox:', error);
      }
    }
  }, [text, fontSize, fontWeight, letterSpacing]);

  const padding = 16;
  const viewBoxX = bbox.x - padding;
  const viewBoxY = bbox.y - padding;
  const viewBoxWidth = bbox.width + padding * 2;
  const viewBoxHeight = bbox.height + padding * 2;

  return (
    <svg
      viewBox={`${viewBoxX} ${viewBoxY} ${viewBoxWidth} ${viewBoxHeight}`}
      className={`w-auto h-auto ${className}`}
      xmlns="http://www.w3.org/2000/svg"
      role="img"
      aria-label={text}
    >
      <text
        ref={textElement}
        x={bbox.width / 2 + viewBoxX + padding}
        y={bbox.height / 2 + viewBoxY + padding}
        fontSize={fontSize}
        fontWeight={fontWeight}
        letterSpacing={letterSpacing}
        dominantBaseline={dominantBaseline}
        textAnchor="middle"
        fill="currentColor"
      >
        {text}
      </text>
    </svg>
  );
};

export default SvgTextLogo;