import { cache } from 'react';
import { promises as fs } from 'fs';
import path from 'path';
import { Register, parseNextReg } from '@/utils/register_parser';

/**
 * Gets all registers, with request-level deduplication via React cache().
 * Multiple calls within the same server request (e.g. generateMetadata + page)
 * share a single parse result.
 */
export const getRegisters = cache(async (): Promise<Register[]> => {
  const filePath = path.join(process.cwd(), 'data', 'nextreg.txt');
  const fileContent = await fs.readFile(filePath, 'utf8');
  return parseNextReg(fileContent);
});
