package click.kamil.springstatemachineexporter.analysis.resolver;

import click.kamil.springstatemachineexporter.analysis.service.EventCarrierPolicy;

/**
 * Shared detection of AST-derived trigger event expressions that must not be rewritten
 * as package-canonical enum constants.
 */
public final class DynamicTriggerExpressions {

    private DynamicTriggerExpressions() {
    }

    /**
     * True for unresolved tokens, {@code valueOf}, ternaries, method calls, placeholders,
     * and bare lowercase identifiers — not for concrete enum constants like {@code OrderEvent.PAY}.
     */
    public static boolean isDynamic(String eventStr) {
        if (eventStr == null || eventStr.isBlank()) {
            return false;
        }
        if (eventStr.startsWith("<SYMBOLIC: ")) {
            return false;
        }
        if (eventStr.contains("${")) {
            return true;
        }
        if (EventCarrierPolicy.isDynamicEventToken(eventStr)) {
            return true;
        }
        if (eventStr.contains(".valueOf(") || eventStr.contains("valueOf (")) {
            return true;
        }
        if (eventStr.contains(" ? ") && eventStr.contains(" : ")) {
            return true;
        }
        if (eventStr.contains("(") || eventStr.contains(")")) {
            return true;
        }
        if (Character.isLowerCase(eventStr.charAt(0)) && !eventStr.contains(".")) {
            return true;
        }
        return false;
    }
}
