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

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

	influxframework.route_history_queue = [];

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

influxframework.router.on("change", () => {
	const route = influxframework.get_route();
	if (is_route_useful(route)) {
		influxframework.route_history_queue.push({
			creation: influxframework.datetime.now_datetime(),
			route: influxframework.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;
	}
}
