import styles from '../ContentPreview.module.css';

import { useLanguage } from '../../../providers/language/LanguageContext';

// Updated to handle both previewUrl and previewContent

interface TextRendererProps {
  previewUrl?: string;
  previewContent?: string;
  fileName: string;
  mimeType?: string;
  onError: () => void;
}

export function TextRenderer({
  previewUrl, previewContent, fileName, mimeType, onError }: TextRendererProps) {
  const { t } = useLanguage();
  // If we have previewContent directly, display it as text
  if (previewContent && !previewUrl) {
    return (
      <div className={styles.textContainer}>
        <div className={styles.textHeader}>
          <span className={styles.textTitle}>{t('Textvorschau')}</span>
        </div>
        <pre className={styles.textPreview}>
          <code className={styles.textCode}>
            {previewContent}
          </code>
        </pre>
      </div>
    );
  }

  // Otherwise, use iframe with previewUrl
  return (
    <iframe
      src={previewUrl}
      className={styles.previewIframe}
      title={`Preview of ${fileName}`}
      data-mime-type={mimeType}
      onError={onError}
      style={{ background: 'white !important' }}
    />
  );
}

