/* Reddit's anti-bot JS challenge, reproduced end to end.
 *
 * Browsing to https://www.reddit.com/r/<sub>/ without cookies does not return
 * the subreddit: reddit serves an 8 KB interstitial titled "Reddit - Please
 * wait for verification" whose inline script computes a token and resubmits a
 * hidden form. Solve it and the real 517 KB page arrives, along with the
 * token_v2/loid cookies that keep it working afterwards. That is why the page
 * loads in a browser that visited reddit before, even with JS switched off.
 *
 * Duktape 2.7 cannot run this at all -- the script uses arrow functions and
 * async/await, so it is a SyntaxError before a single statement executes, and
 * the browser sits on the interstitial forever.
 *
 * Everything above the marker is the minimum DOM the challenge touches. It
 * doubles as the spec for what the port still has to implement: none of
 * document.forms, elements.namedItem, form.requestSubmit or URLSearchParams
 * exist in the current bindings.
 */

var submitted = null;

function El(name)
{
	this.name = name;
	this.type = null;
	this.value = null;
	this.children = [];
}
El.prototype.appendChild = function (c) { this.children.push(c); return c; };

var solutionInput = new El("solution");

var form = {
	elements: {
		namedItem: function (n) {
			return n === "solution" ? solutionInput : null;
		}
	},
	/* the challenge's onsubmit copies the query string into hidden inputs */
	extra: [],
	appendChild: function (c) { this.extra.push(c); return c; },
	onsubmit: null,
	requestSubmit: function () {
		if (this.onsubmit)
			this.onsubmit({ target: this });
		submitted = solutionInput.value;
	}
};

var document = {
	forms: [form],
	createElement: function (n) { return new El(n); },
	_handlers: {},
	addEventListener: function (ev, fn, opts) { this._handlers[ev] = fn; },
	_fire: function (ev) { if (this._handlers[ev]) this._handlers[ev](); }
};
document.location = { search: "?foo=bar" };

/* URLSearchParams, cut down to the surface the challenge uses. Note forEach
 * yields (value, key) -- not (key, value); getting that backwards silently
 * produces a form with the names and values swapped. */
function URLSearchParams(qs)
{
	this._p = [];
	if (!qs)
		return;
	if (qs.charAt(0) === "?")
		qs = qs.slice(1);
	if (qs.length === 0)
		return;
	var parts = qs.split("&");
	for (var i = 0; i < parts.length; i++) {
		var kv = parts[i].split("=");
		this._p.push([decodeURIComponent(kv[0]),
			      decodeURIComponent(kv[1] || "")]);
	}
}
URLSearchParams.prototype.forEach = function (cb, thisArg) {
	for (var i = 0; i < this._p.length; i++)
		cb.call(thisArg, this._p[i][1], this._p[i][0], this);
};
URLSearchParams.prototype.get = function (k) {
	for (var i = 0; i < this._p.length; i++)
		if (this._p[i][0] === k)
			return this._p[i][1];
	return null;
};

/* ---- verbatim from the interstitial, do not reformat ---- */
document.addEventListener("DOMContentLoaded",async function(){var e=document.forms[0],n=(e.onsubmit=function(t){return new URLSearchParams(document.location.search).forEach((e,n)=>t.target.appendChild(Object.assign(document.createElement("input"),{name:n,type:"hidden",value:e}))),!0},await(async e=>e+e)("7b8c3762b1695308"));e.elements.namedItem("solution").value=n,e.requestSubmit()},{once:!0});
/* ---- end verbatim ---- */

document._fire("DOMContentLoaded");

/* The handler is async, so `submitted` is still null here. Assertions live in
 * reddit-challenge-check.js, which the harness runs once the job queue drains. */
