export interface ExtractedResearchFileText {
  extractedText?: string;
  extractedTextSource?: 'liteparse';
  extractedTextTruncated?: boolean;
}

const LITEPARSE_EXTENSIONS = new Set(['pdf']);
const LITEPARSE_MIME_TYPES = new Set(['application/pdf']);

const fileExtension = (name: string) => {
  const trimmed = name.trim().toLowerCase();
  const index = trimmed.lastIndexOf('.');
  return index >= 0 ? trimmed.slice(index + 1) : '';
};

const shouldUseLiteParse = (file: File) => {
  const mimeType = file.type.toLowerCase();
  return LITEPARSE_MIME_TYPES.has(mimeType) || LITEPARSE_EXTENSIONS.has(fileExtension(file.name));
};

export async function extractResearchFileText(
  file: File,
): Promise<ExtractedResearchFileText> {
  if (!shouldUseLiteParse(file)) {
    return {};
  }

  try {
    const { LiteParse } = await import('@llamaindex/liteparse');
    const parser = new LiteParse({ ocrEnabled: true, outputFormat: 'text' });
    const result = await parser.parse(new Uint8Array(await file.arrayBuffer()), true);
    const text = result.text.trim();
    if (!text) {
      return {};
    }

    return {
      extractedText: text,
      extractedTextSource: 'liteparse',
      extractedTextTruncated: false,
    };
  } catch {
    return {};
  }
}
