export const extractFileDiff = (diff: string | undefined, filePath: string) => {
  if (!diff?.trim() || filePath === '.') return '';
  const lines = diff.split('\n');
  const sections: string[][] = [];
  let current: string[] | null = null;
  for (const line of lines) {
    if (line.startsWith('diff --git ')) {
      if (current) sections.push(current);
      current = [line];
      continue;
    }
    current?.push(line);
  }
  if (current) sections.push(current);
  const normalizedPath = filePath.replace(/^\.\/+/, '');
  const section = sections.find((item) => {
    const header = item[0] ?? '';
    return (
      header.includes(` a/${normalizedPath} `) ||
      header.endsWith(` a/${normalizedPath}`) ||
      header.includes(` b/${normalizedPath}`) ||
      header.endsWith(` b/${normalizedPath}`)
    );
  });
  return section?.join('\n') ?? '';
};
