import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fail, pass, repositoryRoot, walk } from './lib.mjs';

const cssFiles = await walk(path.join(repositoryRoot, 'src'), (file) => file.endsWith('.css'));
const errors = [];
const forbidden = [
  /(^|[;{]\s*)(left|right)\s*:/gim,
  /(^|[;{]\s*)(margin|padding|border)-(left|right)(?:-[a-z]+)?\s*:/gim,
  /(^|[;{]\s*)inset-(left|right)\s*:/gim,
  /(^|[;{]\s*)text-align\s*:\s*(left|right)/gim,
  /(^|[;{]\s*)float\s*:\s*(left|right)/gim,
];
for (const file of cssFiles) {
  const text = await readFile(file, 'utf8');
  for (const expression of forbidden) {
    const matches = text.match(expression);
    if (matches)
      errors.push(
        `${path.relative(repositoryRoot, file)} uses physical CSS: ${matches.join(', ')}`,
      );
  }
}

if (errors.length > 0) fail(errors);
else pass(`${cssFiles.length} CSS files use direction-safe logical declarations.`);
