package click.kamil.springstatemachineexporter.analysis.index;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Scan-time constants for indexed getters backed by fields with literal or enum initializers.
 */
public final class IndexedFieldConstantsIndex {

    private static final IndexedFieldConstantsIndex EMPTY = new IndexedFieldConstantsIndex(Map.of(), 0);

    private final Map<String, Map<String, List<String>>> byOwnerAndMethod;
    private final int entryCount;

    public IndexedFieldConstantsIndex(Map<String, Map<String, List<String>>> byOwnerAndMethod, int entryCount) {
        this.byOwnerAndMethod = Collections.unmodifiableMap(deepCopy(byOwnerAndMethod));
        this.entryCount = entryCount;
    }

    public static IndexedFieldConstantsIndex empty() {
        return EMPTY;
    }

    public Optional<List<String>> lookup(String ownerFqn, String methodName) {
        if (ownerFqn == null || methodName == null) {
            return Optional.empty();
        }
        Map<String, List<String>> methods = byOwnerAndMethod.get(ownerFqn);
        if (methods == null) {
            return Optional.empty();
        }
        List<String> constants = methods.get(methodName);
        if (constants == null || constants.isEmpty()) {
            return Optional.empty();
        }
        return Optional.of(constants);
    }

    public int entryCount() {
        return entryCount;
    }

    private static Map<String, Map<String, List<String>>> deepCopy(Map<String, Map<String, List<String>>> source) {
        Map<String, Map<String, List<String>>> copy = new HashMap<>();
        for (Map.Entry<String, Map<String, List<String>>> entry : source.entrySet()) {
            copy.put(entry.getKey(), Map.copyOf(entry.getValue()));
        }
        return copy;
    }
}
