import type { Token } from './types';

export function tokensEqual(a: Token, b: Token): boolean {
  // If using Tiktoken (BPE), we relax the comparison to ignore whitespace differences
  // and newlines by trimming and stripping them. This helps when the patch might
  // have slightly different indentation or spacing than the source.
  if (a.type === 'bpe' || b.type === 'bpe') {
    return (
      a.text.replace(/\r?\n/g, '').trim() ===
      b.text.replace(/\r?\n/g, '').trim()
    );
  }
  return a.text === b.text;
}

export function formatAnchor(anchor: Token[]): string {
  return anchor.map((t) => t.text).join(' ');
}

export function findAllSequences(haystack: Token[], needle: Token[]): number[] {
  if (needle.length === 0) return [];
  const indices: number[] = [];
  outer: for (let i = 0; i <= haystack.length - needle.length; i++) {
    for (let j = 0; j < needle.length; j++) {
      if (!tokensEqual(haystack[i + j], needle[j])) {
        continue outer;
      }
    }
    indices.push(i);
  }
  return indices;
}