package click.kamil.springstatemachineexporter.exporter;

import lombok.Builder;
import lombok.Value;

@Value
@Builder
public class ExportOptions {
    @Builder.Default
    boolean useLambdaGuards = true;
    @Builder.Default
    boolean renderChoicesAsDiamonds = true;
    @Builder.Default
    boolean embedIdentifiers = false;
    @Builder.Default
    boolean includeMetadataPane = true;

    @Builder.Default
    EnumFormat eventFormat = EnumFormat.fn;
    @Builder.Default
    EnumFormat stateFormat = EnumFormat.fn;

    public String formatState(click.kamil.springstatemachineexporter.model.State state) {
        if (state == null) return null;
        return format(state.rawName(), state.fullIdentifier(), stateFormat);
    }

    public String formatEvent(click.kamil.springstatemachineexporter.model.Event event) {
        if (event == null) return null;
        return format(event.rawName(), event.fullIdentifier(), eventFormat);
    }

    /** Formats a bare state label (e.g. start/end state string) using the same rules as {@link #formatState}. */
    public String formatStateLabel(String label) {
        if (label == null) return null;
        return format(label, label, stateFormat);
    }

    private String format(String raw, String fqn, EnumFormat format) {
        if (fqn == null) return raw;
        switch (format) {
            case fqn: return fqn;
            case sn: return fqn.substring(fqn.lastIndexOf('.') + 1);
            case fn: 
                int lastDot = fqn.lastIndexOf('.');
                if (lastDot > 0) {
                    int prevDot = fqn.lastIndexOf('.', lastDot - 1);
                    return prevDot > 0 ? fqn.substring(prevDot + 1) : fqn;
                }
                return fqn;
            default: return raw;
        }
    }
}
