package org.zwobble.mammoth.internal.util;

import com.google.common.collect.ImmutableMap;

import java.util.*;
import java.util.function.Function;

public class MammothMaps {
    public static <K, V> Map<K, V> map() {
        return Collections.emptyMap();
    }

    public static <K, V> Map<K, V> map(K key1, V value1) {
        return Collections.singletonMap(key1, value1);
    }

    public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2) {
        return ImmutableMap.of(key1, value1, key2, value2);
    }

    public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2, K key3, V value3) {
        return ImmutableMap.of(key1, value1, key2, value2, key3, value3);
    }

    public static <K, V> Map.Entry<K, V> entry(K key, V value) {
        return new AbstractMap.SimpleImmutableEntry<K, V>(key, value);
    }

    public static <K, V> Optional<V> lookup(Map<K, V> map, K key) {
        return Optional.ofNullable(map.get(key));
    }

    public static <T, K> Map<K, T> toMapWithKey(Iterable<T> iterable, Function<T, K> function) {
        return toMap(iterable, element -> entry(function.apply(element), element));
    }

    public static <T, K, V> Map<K, V> toMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) {
        ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
        for (T element : iterable) {
            builder.put(function.apply(element));
        }
        return builder.build();
    }

    public static <T, K> Map<K, List<T>> toMultiMapWithKey(Iterable<T> iterable, Function<T, K> function) {
        return toMultiMap(iterable, element -> entry(function.apply(element), element));
    }

    public static <T, K, V> Map<K, List<V>> toMultiMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) {
        Map<K, List<V>> map = new HashMap<>();
        for (T element : iterable) {
            Map.Entry<K, V> pair = function.apply(element);
            map.computeIfAbsent(pair.getKey(), key -> new ArrayList<V>())
                .add(pair.getValue());
        }
        return map;
    }

    public static <K1, K2, V> Map<K2, V> eagerMapKeys(Map<K1, V> map, Function<K1, K2> function) {
        Map<K2, V> result = new HashMap<>();
        for (Map.Entry<K1, V> element : map.entrySet()) {
            result.put(function.apply(element.getKey()), element.getValue());
        }
        return result;
    }

    public static <K, V1, V2> Map<K, V2> eagerMapValues(Map<K, V1> map, Function<V1, V2> function) {
        Map<K, V2> result = new HashMap<>();
        for (Map.Entry<K, V1> element : map.entrySet()) {
            result.put(element.getKey(), function.apply(element.getValue()));
        }
        return result;
    }
}

@@ -41,56 +41,8 @@
 l;%0A%0A
-import com.google.common.collect.ImmutableMap;%0A%0A
 impo
@@ -420,234 +420,377 @@
     
-return ImmutableMap.of(key1, value1, key2, value2);%0A    %7D%0A%0A    public static %3CK, V%3E Map%3CK, V%3E map(K key1, V value1, K key2, V value2, K key3, V value3) %7B%0A        return ImmutableMap.of(key1, value1, key2, value2, key3, value3)
+Map%3CK, V%3E map = new HashMap%3C%3E();%0A        map.put(key1, value1);%0A        map.put(key2, value2);%0A        return map;%0A    %7D%0A%0A    public static %3CK, V%3E Map%3CK, V%3E map(K key1, V value1, K key2, V value2, K key3, V value3) %7B%0A        Map%3CK, V%3E map = new HashMap%3C%3E();%0A        map.put(key1, value1);%0A        map.put(key2, value2);%0A        map.put(key3, value3);%0A        return map
 ;%0A  
@@ -1369,152 +1369,190 @@
     
-ImmutableMap.Builder%3CK, V%3E builder = ImmutableMap.builder();%0A        for (T element : iterable) %7B%0A            builder.put(function.apply(element
+Map%3CK, V%3E map = new HashMap%3C%3E();%0A        for (T element : iterable) %7B%0A            Map.Entry%3CK, V%3E entry = function.apply(element);%0A            map.put(entry.getKey(), entry.getValue(
 ));%0A
@@ -1580,23 +1580,11 @@
 urn 
-builder.build()
+map
 ;%0A  

Remove ImmutableMap usage from MammothMaps