function toMethod(words) {
  const cleaned = words.trim().replace(/[^A-Za-z0-9\s]+/g, " ");
  const camel = cleaned.replace(/\s+(.)/g, (_, c) => c.toUpperCase()).replace(/^\w/, (c) => c.toLowerCase());
  const safe = camel.replace(/[^A-Za-z0-9_$]/g, "");
  return safe || "unnamedStep";
}
function toVar(name) {
  return name.charAt(0).toLowerCase() + name.slice(1);
}
const NL_LEADING_ADJECTIVES = [
  "new",
  "existing",
  "current",
  "the",
  "a",
  "an"
];
const NL_ADJECTIVE_PREFIX = `(?:(?:${NL_LEADING_ADJECTIVES.join("|")})\\s+)?`;
let _stepRecorder = null;
function setStepMatchRecorder(fn) {
  _stepRecorder = fn;
}
function matchAgainstConventions(step, ctx, conventions, aiArgsExpr) {
  let result = null;
  for (const convention of conventions) {
    const m = step.match(convention.pattern);
    if (m) {
      const call = convention.generateCall(m, ctx);
      if (call) {
        result = {
          matched: true,
          call,
          helperMethod: convention.generateMethod?.(m, ctx)
        };
        break;
      }
    }
  }
  if (!result) {
    const functionName = toMethod(step);
    const declared = Array.from(ctx.declaredVars || []);
    const paramNames = ctx.parameterNames || [];
    const inputs = [...paramNames, ...declared];
    const resultVar = ctx.resultName || `step${ctx.stepNum}Result`;
    if (ctx.declaredVars) ctx.declaredVars.add(resultVar);
    const inputObj = aiArgsExpr(inputs, paramNames);
    result = {
      matched: false,
      call: `    // Step ${ctx.stepNum}: ${step} [AI-generated \u2014 pure transform]
    const ${resultVar} = await aiBehaviors.${functionName}(${inputObj});`,
      functionName,
      inputs,
      resultVar
    };
  }
  if (_stepRecorder) {
    try {
      _stepRecorder({
        ownerName: ctx.serviceName,
        actionName: ctx.operationName,
        stepOrdinal: ctx.stepNum,
        stepText: step,
        matched: result.matched,
        helperMethod: result.helperMethod,
        aiFunctionName: result.functionName
      });
    } catch {
    }
  }
  return result;
}
export {
  NL_ADJECTIVE_PREFIX,
  NL_LEADING_ADJECTIVES,
  matchAgainstConventions,
  setStepMatchRecorder,
  toMethod,
  toVar
};
