// Custom ESLint rules shared across the workspace.

const FORBIDDEN_UNICODE_SYMBOLS = Object.freeze([
  { char: '\u2013', name: 'Unicode en dash (–)', replacement: '-' },
  { char: '\u2014', name: 'Unicode em dash (—)', replacement: '-' },
  { char: '\u2026', name: 'Unicode ellipsis (…)', replacement: '...' },
  { char: '\u2192', name: 'Unicode right arrow (→)', replacement: '->' },
  { char: '\u2190', name: 'Unicode left arrow (←)', replacement: '<-' },
  { char: '\u2194', name: 'Unicode left-right arrow (↔)', replacement: '<->' },
  { char: '\u21d2', name: 'Unicode right double arrow (⇒)', replacement: '=>' },
  { char: '\u21d0', name: 'Unicode left double arrow (⇐)', replacement: '<=' },
  { char: '\u21d4', name: 'Unicode left-right double arrow (⇔)', replacement: '<=>' }
]);

function createReplaceFix(range, replacement) {
  return (fixer) => fixer.replaceTextRange(range, replacement);
}

module.exports = {
  rules: {
    'angular-template-spacing': {
      meta: {
        type: 'layout',
        docs: {
          description: 'Enforce spacing between elements and property grouping in Angular templates',
          category: 'Stylistic Issues',
          recommended: true
        },
        fixable: 'whitespace',
        schema: []
      },
      create() {
        // This is a placeholder for custom rule implementation.
        // ESLint's template rules are limited, so manual formatting is recommended.
        return {};
      }
    },
    'no-unicode-symbols': {
      meta: {
        type: 'suggestion',
        docs: {
          description: 'Disallow AI/LLM-style Unicode symbols in source files'
        },
        fixable: 'code',
        hasSuggestions: true,
        schema: [],
        messages: {
          forbiddenSymbol: '{{name}} is not allowed. Use ASCII "{{replacement}}" instead.',
          replaceSymbol: 'Replace with "{{replacement}}"'
        }
      },
      create(context) {
        const sourceCode = context.getSourceCode();

        return {
          Program() {
            const sourceText = sourceCode.getText();

            for (const symbol of FORBIDDEN_UNICODE_SYMBOLS) {
              let index = sourceText.indexOf(symbol.char);

              while (index !== -1) {
                const range = [index, index + symbol.char.length];
                const loc = {
                  start: sourceCode.getLocFromIndex(range[0]),
                  end: sourceCode.getLocFromIndex(range[1])
                };

                context.report({
                  loc,
                  messageId: 'forbiddenSymbol',
                  data: {
                    name: symbol.name,
                    replacement: symbol.replacement
                  },
                  fix: createReplaceFix(range, symbol.replacement),
                  suggest: [
                    {
                      messageId: 'replaceSymbol',
                      data: {
                        replacement: symbol.replacement
                      },
                      fix: createReplaceFix(range, symbol.replacement)
                    }
                  ]
                });

                index = sourceText.indexOf(symbol.char, index + symbol.char.length);
              }
            }
          }
        };
      }
    }
  },
  FORBIDDEN_UNICODE_SYMBOLS
};
