"use strict";

// Import dependencies
import("/js/core.js").then(() =>
	import("/js/damper.js").then(() =>
		import("/js/telegram.js").then(() => {
			const dependencies = setInterval(() => {
				if (
					typeof core === "function" &&
					typeof core.damper === "function" &&
					typeof core.telegram === "function"
				) {
					clearInterval(dependencies);
					clearTimeout(timeout);
					initialization();
				}
			}, 10);
			const timeout = setTimeout(() => {
				clearInterval(dependencies);
				initialization();
			}, 5000);

			function initialization() {
				if (typeof core.session === "undefined") {
					// Not initialized

					// Write to the core
					core.session = class session {
						/**
						 * Buffer
						 */
						static buffer = class buffer {
							/**
							 * Write to the session buffer
							 *
							 * @param {string} name Name of the parameter
							 * @param {string} value Value of the parameter (it can be JSON)
							 *
							 * @return {void}
							 */
							static write(name, value) {
								if (typeof name === "string" && typeof value === "string") {
									//

									// Send request to the server
									core.request(
										"/session/write",
										`${name}=${value}`,
										"POST",
										{},
										null
									);
								}
							}
						};
					};
				}
			}
		})
	)
);
