// Generated by @harness-engineering/linter-gen
// Do not edit manually - regenerate from harness-linter.yml
// Config: {{meta.configPath}}

import { ESLintUtils, type TSESTree } from '@typescript-eslint/utils';
import { minimatch } from 'minimatch';
import * as path from 'path';

const createRule = ESLintUtils.RuleCreator(
  () => 'https://github.com/harness-engineering/linter-gen'
);

type MessageIds = 'forbidden';

const SOURCE_PATTERN = '{{{json config.source}}}';
const FORBIDDEN_IMPORTS: string[] = {{{json config.forbiddenImports}}};
const MESSAGE = '{{{config.message}}}';

/**
 * Normalize path separators to forward slashes
 */
function normalizePath(filePath: string): string {
  return filePath.replace(/\\/g, '/');
}

/**
 * Check if a path matches a glob pattern
 */
function matchesPattern(filePath: string, pattern: string): boolean {
  const normalized = normalizePath(filePath);
  return minimatch(normalized, pattern, { matchBase: true });
}

export default createRule<[], MessageIds>({
  name: '{{name}}',
  meta: {
    type: 'problem',
    docs: {
      description: MESSAGE,
    },
    messages: {
      forbidden: MESSAGE,
    },
    schema: [],
  },
  defaultOptions: [],
  create(context) {
    const filePath = normalizePath(context.filename);

    // Only apply to files matching source pattern
    if (!matchesPattern(filePath, SOURCE_PATTERN)) {
      return {};
    }

    return {
      ImportDeclaration(node: TSESTree.ImportDeclaration) {
        const importPath = String(node.source.value);

        for (const forbidden of FORBIDDEN_IMPORTS) {
          // Check exact match or pattern match
          if (importPath === forbidden || matchesPattern(importPath, forbidden)) {
            context.report({
              node,
              messageId: 'forbidden',
            });
            return;
          }
        }
      },
    };
  },
});
