(() =>
{
  if(!/^https:\/\/steamcommunity\.com\/(id\/[\w-_]{1,64}|profiles\/\d{17})\/inventory/.test(location.href))
	{
    console.error("You need to run this script on your inventory page");
    return false;
  }

  var [appid, context, webapikey, sleep, invDesc, invAssets, classidsToCombine] = [location.hash.substr(1).replace(/\D/g, ''), '', '', '', '', '', {}];
  appid = appid != '' ? appid : '1464540';
  ShowPromptDialog("Please enter the appid", "", "Continue", "Abort", '', appid).done((a) => {
  appid = a;
  let text = 'Here are the available context number for this app (usually only the number 2):<br><br>';

  for(var contextnr in g_rgAppContextData[appid].rgContexts)
	{
    if(g_rgAppContextData[appid].rgContexts.hasOwnProperty(contextnr))
		{
      text += contextnr + ': ' + g_rgAppContextData[appid].rgContexts[contextnr].name + ' (' + g_rgAppContextData[appid].rgContexts[contextnr].asset_count + ')<br>';
    }
  }

  text += '<br>';
  ShowPromptDialog("Please enter the correct context number", text, "Continue", "Abort", '', '2').done((b) => {
  context = b;
  ShowPromptDialog("Please enter your Steam WebAPIkey", 'Enter the key listed here: <a href="https://steamcommunity.com/dev/apikey">https://steamcommunity.com/dev/apikey</a> <br><br>', "Continue", "Abort", '', '').done((c) => {
  webapikey = c;
  ShowPromptDialog("Time to sleep between api calls (in ms)", "", "Run", "Abort", '', '1000').done((d) => {
    sleep = d;
    invDesc = g_rgAppContextData[appid].rgContexts[context].inventory.m_rgDescriptions;
    invAssets = g_rgAppContextData[appid].rgContexts[context].inventory.m_rgAssets;
    //console.log([appid, context, webapikey, sleep, invDesc, invAssets, classidsToCombine]);
    stackItems();
  });
});
});
});

document.location.href = document.location.href.split('#')[0] + "#" + appid;

async function stackItems()
{
  ShowAlertDialog("Stacking items", '<div id="itemstacking"></div>');
  var key;
	var count = 0;
  var itemToPush = {};
	var toUnstack = [];

  for(key in invAssets)
  {
    if(invAssets[key].amount > 1 )
    {
      itemToUnstack =
  	  {
        assetid: invAssets[key].assetid,
        amount: invAssets[key].amount,
      };

      count += parseInt(invAssets[key].amount);
      toUnstack.push(itemToUnstack);
    }
  }

  //console.log(toUnstack);
	//console.log(count);
  
  if(Object.keys(toUnstack).length == 0)
  {
    $J('#itemstacking').text('No items to stack');
  }

  for(key in toUnstack)
	{
	  for(i = 1; i < toUnstack[key].amount; i++)
		{
      //console.log(toUnstack[key]);
      $J('#itemstacking').text('Untacking items: ' + i + '/' + count);
      var url = "https://api.steampowered.com/IInventoryService/SplitItemStack/v1/?key=" + webapikey + "&appid=" + appid + "&itemid=" + toUnstack[key].assetid + "&quantity=1";
      var othePram = { headers: { "content-type": "application/json; charset=UTF-8" }, method: "POST", mode: "no-cors" };
      //console.log("url: " + url);
      fetch(url, othePram).then(data => { console.log(data); return data.json; }).catch(error => console.log(error));
      await new Promise(r => setTimeout(r, sleep));
      $J('#itemstacking').text('Stacking items complete');
		}
  }
}
})();