/*
 * Copyright (c) 2018, Ron Young <https://github.com/raiyni>
 * All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *     list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.runelite.client.game;

import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

/**
 * Converts variation items to it's base item counterparts
 */
public class ItemVariationMapping
{
	private static final Map<Integer, Integer> MAPPINGS;

	static
	{
		final Gson gson = new Gson();
		final TypeToken<Map<String, Collection<Integer>>> typeToken = new TypeToken<Map<String, Collection<Integer>>>()
		{
		};

		final InputStream geLimitData = ItemVariationMapping.class.getResourceAsStream("/item_variations.json");
		final Map<String, Collection<Integer>> itemVariations = gson.fromJson(new InputStreamReader(geLimitData), typeToken.getType());

		ImmutableMap.Builder<Integer, Integer> builder = new ImmutableMap.Builder<>();
		for (Collection<Integer> value : itemVariations.values())
		{
			final Iterator<Integer> iterator = value.iterator();
			final int base = iterator.next();

			while (iterator.hasNext())
			{
				builder.put(iterator.next(), base);
			}
		}
		MAPPINGS = builder.build();
	}

	/**
	 * Get base item id for provided variation item id.
	 *
	 * @param itemId the item id
	 * @return the base item id
	 */
	public static int map(int itemId)
	{
		return MAPPINGS.getOrDefault(itemId, itemId);
	}
}

@@ -1474,16 +1474,111 @@
 bleMap;%0A
+import com.google.common.collect.ImmutableMultimap;%0Aimport com.google.common.collect.Multimap;%0A
 import c
@@ -1727,24 +1727,54 @@
 Collection;%0A
+import java.util.Collections;%0A
 import java.
@@ -1967,16 +1967,84 @@
 APPINGS;
+%0A%09private static final Multimap%3CInteger, Integer%3E INVERTED_MAPPINGS;
 %0A%0A%09stati
@@ -2523,16 +2523,115 @@
 er%3C%3E();%0A
+%09%09ImmutableMultimap.Builder%3CInteger, Integer%3E invertedBuilder = new ImmutableMultimap.Builder%3C%3E();%0A
 %09%09for (C
@@ -2823,35 +2823,135 @@
 %09%09%09%09
-builder.put(iterator.next()
+final int id = iterator.next();%0A%09%09%09%09builder.put(id, base);%0A%09%09%09%09invertedBuilder.put(base, id);%0A%09%09%09%7D%0A%0A%09%09%09invertedBuilder.put(base
 , ba
@@ -2961,14 +2961,57 @@
 ;%0A%09%09
-%09
 %7D
+%0A
 %0A%09%09
-%7D
+INVERTED_MAPPINGS = invertedBuilder.build();
 %0A%09%09M
@@ -3255,11 +3255,289 @@
 mId);%0A%09%7D
+%0A%0A%09/**%0A%09 * Get item ids for provided variation item id.%0A%09 *%0A%09 * @param itemId the item id%0A%09 * @return the item ids%0A%09 */%0A%09public static Collection%3CInteger%3E getVariations(int itemId)%0A%09%7B%0A%09%09return INVERTED_MAPPINGS.asMap().getOrDefault(itemId, Collections.singletonList(itemId));%0A%09%7D
 %0A%7D%0A

add reverse mapping of variations