jingrow.route_history_queue = [];
const routes_to_skip = ["Form", "social", "setup-wizard", "recorder"];

const save_routes = jingrow.utils.debounce(() => {
	if (jingrow.session.user === "Guest") return;
	const routes = jingrow.route_history_queue;
	if (!routes.length) return;

	jingrow.route_history_queue = [];

	jingrow
		.xcall("jingrow.desk.pagetype.route_history.route_history.deferred_insert", {
			routes: routes,
		})
		.catch(() => {
			jingrow.route_history_queue.concat(routes);
		});
}, 10000);

jingrow.router.on("change", () => {
	const route = jingrow.get_route();
	if (is_route_useful(route)) {
		jingrow.route_history_queue.push({
			creation: jingrow.datetime.now_datetime(),
			route: jingrow.get_route_str(),
		});

		save_routes();
	}
});

function is_route_useful(route) {
	if (!route[1]) {
		return false;
	} else if ((route[0] === "List" && !route[2]) || routes_to_skip.includes(route[0])) {
		return false;
	} else {
		return true;
	}
}
