/**
 * Rule: naming-convention/type-enum-matches-filename
 * Ensures that type and enum names match the filename
 */
export default {
  meta: {
    type: 'suggestion',
    docs: {
      description: 'Ensures that type and enum names match the filename',
    },
    messages: {
      mismatch: `Type or enum name must match the filename "{{ expectedTypeName }}".`,
    },
  },
  create(context) {
    const filename = context.getFilename();
    const basename = filename.split(/[\\/]/).pop();
    const expectedTypeName = basename.replace(/\.ts$/, '');
    let hasExpectedType = false;

    if (expectedTypeName === 'index') {
      return {};
    }

    return {
      TSTypeAliasDeclaration(node) {
        const typeName = node.id.name;
        if (typeName === expectedTypeName) {
          hasExpectedType = true;
        }
      },
      TSEnumDeclaration(node) {
        const enumName = node.id.name;
        if (enumName === expectedTypeName) {
          hasExpectedType = true;
        }
      },
      'Program:exit'() {
        if (!hasExpectedType) {
          context.report({
            loc: { line: 1, column: 1 },
            messageId: 'mismatch',
            data: { expectedTypeName },
          });
        }
      },
    };
  },
};
