import { isComponentName, isHookName } from '../utils/index.js';

/**
 * Rule: component/require-arrow-function
 * Enforces the use of arrow functions for component definitions
 */
export default {
  meta: {
    type: 'suggestion',
    docs: {
      description: 'Enforces the use of arrow functions for component definitions',
    },
    messages: {
      useArrowFunction: "Define component '{{componentName}}' using arrow function syntax instead of a function expression.",
    },
  },
  create(context) {
    return {
      // Check function declarations
      FunctionDeclaration(node) {
        // Skip if no name or not a component name
        if (!node.id || !isComponentName(node.id.name) || isHookName(node.id.name)) {
          return;
        }

        const functionName = node.id.name;
        context.report({
          node,
          messageId: 'useArrowFunction',
          data: {
            componentName: functionName,
          },
        });
      },

      // Check variable declarators
      VariableDeclarator(node) {
        // Skip if not a function expression or not a component name
        if (
          node.id?.type !== 'Identifier' ||
          node.init?.type !== 'FunctionExpression' ||
          !isComponentName(node.id.name) ||
          isHookName(node.id.name)
        ) {
          return;
        }

        const variableName = node.id.name;
        context.report({
          node: node.init,
          messageId: 'useArrowFunction',
          data: {
            componentName: variableName,
          },
        });
      },
    };
  },
};
