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

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

	xhiveframework.route_history_queue = [];

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

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