const LEARNITIVE_EDITABLE_TEXT_EXTENSIONS = new Set([
  '.bib',
  '.bst',
  '.cls',
  '.ltx',
  '.sty',
  '.tex',
  '.txt',
]);

/**
 * Text files Learnitive may edit as source without converting them through the
 * Markdown/ProseMirror document model. Matching is intentionally extension
 * based so arbitrary binary project files remain opaque and byte-preserved.
 */
export function isLearnitiveEditableTextPath(path: string): boolean {
  const normalized = path.trim().toLowerCase();
  const slash = normalized.lastIndexOf('/');
  const leaf = slash < 0 ? normalized : normalized.slice(slash + 1);
  const dot = leaf.lastIndexOf('.');
  if (dot <= 0) {
    return false;
  }
  return LEARNITIVE_EDITABLE_TEXT_EXTENSIONS.has(leaf.slice(dot));
}


const PROJECT_CODE_TEXT_EXTENSIONS = new Set([
  '.c', '.cc', '.cpp', '.cxx', '.h', '.hh', '.hpp',
  '.py', '.pyi', '.r', '.jl', '.m', '.matlab', '.sh', '.bash', '.zsh', '.fish',
  '.js', '.jsx', '.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts',
  '.html', '.htm', '.css', '.scss', '.sass', '.less', '.vue', '.svelte',
  '.java', '.kt', '.kts', '.go', '.rs', '.swift', '.scala', '.lua', '.pl', '.pm', '.rb', '.php',
  '.sql', '.graphql', '.gql',
  '.csv', '.tsv', '.json', '.jsonl', '.ndjson', '.xml',
  '.yaml', '.yml', '.toml', '.ini', '.cfg', '.conf', '.env', '.properties',
  '.dockerfile', '.makefile',
]);

const PROJECT_ADDITIONAL_READABLE_TEXT_EXTENSIONS = new Set([
  '.md', '.markdown', '.rst', '.adoc', '.log', '.out',
]);

/** Scientific/code/data text files edited by the generic CodeMirror surface. */
export function isProjectCodeTextPath(path: string): boolean {
  const normalized = path.trim().toLowerCase();
  const leaf = normalized.slice(normalized.lastIndexOf('/') + 1);
  if (leaf === 'dockerfile' || leaf === 'makefile' || leaf === 'cmakelists.txt') return true;
  const dot = leaf.lastIndexOf('.');
  return dot > 0 && PROJECT_CODE_TEXT_EXTENSIONS.has(leaf.slice(dot));
}

/**
 * Text files that Learnitive research features may safely decode and inspect.
 * This intentionally combines first-class LaTeX/text sources with the
 * generic scientific/code editor allow-list while continuing to reject
 * arbitrary/binary project data.
 */
export function isProjectReadableTextPath(path: string): boolean {
  if (isLearnitiveEditableTextPath(path) || isProjectCodeTextPath(path)) return true;
  const normalized = path.trim().toLowerCase();
  const leaf = normalized.slice(normalized.lastIndexOf('/') + 1);
  const dot = leaf.lastIndexOf('.');
  return dot > 0 && PROJECT_ADDITIONAL_READABLE_TEXT_EXTENSIONS.has(leaf.slice(dot));
}
