/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

/**
 * Generates src/translator/stepNames.ts from the Gremlin.g4 grammar.
 *
 * Extracts all traversalMethod_xxx and traversalSourceSpawnMethod_xxx rule names,
 * derives camelCase canonical step names and their PascalCase equivalents used by
 * Go and .NET dialects.
 *
 * Run via: npm run generate-step-names
 */

import { readFileSync, writeFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));

const grammarPath = join(
  __dirname,
  '../../../gremlin-language/src/main/antlr4/Gremlin.g4'
);
const outputPath = join(__dirname, '../src/translator/stepNames.ts');

const grammar = readFileSync(grammarPath, 'utf-8');

const stepNames = new Set();
for (const line of grammar.split('\n')) {
  const match = line.match(/^traversal(?:Method|SourceSpawnMethod)_([a-zA-Z]+)/);
  if (match) {
    stepNames.add(match[1]);
  }
}

const sorted = [...stepNames].sort();

const camelCaseEntries = sorted.map(n => `  '${n}',`).join('\n');
const pascalCaseEntries = sorted
  .map(n => `  '${n[0].toUpperCase()}${n.slice(1)}',`)
  .join('\n');

const content = `/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

// Generated by scripts/generate-step-names.js — do not edit manually.
// Re-run \`npm run generate-step-names\` after changes to Gremlin.g4.

/**
 * Canonical camelCase Gremlin step names derived from the gremlin-language grammar.
 * Used to safely identify step names during normalization.
 */
export const GREMLIN_STEP_NAMES: ReadonlySet<string> = new Set([
${camelCaseEntries}
]);

/**
 * PascalCase equivalents of Gremlin step names, as used by Go and .NET dialects.
 * Used as an allowlist when converting PascalCase tokens to camelCase during normalization.
 */
export const GREMLIN_STEP_NAMES_PASCAL: ReadonlySet<string> = new Set([
${pascalCaseEntries}
]);
`;

writeFileSync(outputPath, content, 'utf-8');
console.log(`Generated ${outputPath} with ${stepNames.size} step names.`);
