"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.account === "undefined") {
					// Not initialized

					// Write to the core
					core.account = class account {
						// Wrap of indicator of the account
						static wrap = document.getElementById("account");

						// Indicator of the account
						static indicator = this.wrap.getElementsByTagName("i")[0];

						// Description of the account
						static description = this.wrap.getElementsByTagName("small")[0];

						// Statuc of the account
						static connected = false;

						// Duration of the disconnected status
						static timeout = 0;

						// Instance of the time counter in disconnected status
						static counter;

						// Socket address (xn--e1ajlli это сокет)
						static socket = "wss://arming.dev.mirzaev.sexy:9502";

						// Instance of account to the socket
						static session;

						// Iterval for reconnect
						static interval;

						// Attempts to connect (when core.account.readyState === 0)
						static attempts = 0;

						// Interval for block
						static block;

						/**
						 * Authentication
						 *
						 * @return {void}
						 */
						static authentication() {
							core.status_loading.removeAttribute("disabled");

							const timer_for_response = setTimeout(() => {
								core.status_loading.setAttribute("disabled", true);
							}, 3000);

							if (core.telegram.api.initData.length > 0) {
								core
									.request(
										"/session/connect/telegram",
										core.telegram.api.initData
									)
									.then((json) => {
										if (
											json.errors !== null &&
											typeof json.errors === "object" &&
											json.errors.length > 0
										) {
											// Errors received
										} else {
											// Errors not received

											if (json.connected === true) {
												core.status_loading.setAttribute("disabled", true);
												clearTimeout(timer_for_response);

												const a =
													core.status_account.getElementsByTagName("a")[0];
												a.setAttribute("onclick", "core.account.profile()");
												a.innerText =
													json.domain.length > 0 ? "@" + json.domain : "ERROR";
											}

											if (
												json.language !== null &&
												typeof json.language === "string" &&
												json.langiage.length === 2
											) {
												core.language = json.language;
											}
										}
									});
							}
						}

						/**
						 * Buffer
						 */
						static buffer = class buffer {
							/**
							 * Write to the account 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(
										"/account/write",
										`${name}=${value}`,
										"POST",
										{},
										null
									);
								}
							}
						};
					};
				}
			}
		})
	)
);
