"use strict";

if (typeof window.loader !== "function") {
  // Not initialized

  // Initialize of the class in global namespace
  window.loader = class pages {
    /**
     * Storage for history
     */
    static storage = {};

    /**
     * Element: menu
     *
     * @return {void}
     */
    static async menu() {
      return await fetch("/elements/menu", {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      })
        .then((response) => response.text())
        .then((data) => {
          // Deleting outdated elements
          for (
            const element of document.querySelectorAll(
              `section[id="menu"]`,
            )
          ) element.remove();

          const section = document.createElement("section");
          document.body.prepend(section);
          section.outerHTML = data;
        });
    }

    /**
     * Page: tasks
     *
     * @return {void}
     */
    static async index() {
      // Запрос к серверу
      return await fetch("/", {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      })
        .then((response) => response.text())
        .then((data) => {
          // Write path in history
          history.pushState(this.storage, "/", "/");

					// Write path to the current directory buffer
					core.page = 'tasks';

          // Write content in document
          document.body.getElementsByTagName("main")[0].innerHTML = data;
        });
    }

    /**
     * Page: administrators
     *
     * @return {void}
     */
    static async administrators() {
      return await fetch("/administrators", {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      })
        .then((response) => response.text())
        .then((data) => {
					// Write path in history
          history.pushState(this.storage, "/administrators", "/administrators");

					// Write path to the current directory buffer
					core.page = 'administrators';

          // Write content in document
          document.body.getElementsByTagName("main")[0].innerHTML = data;
        });
    }

    /**
     * Page: operators
     *
     * @return {void}
     */
    static async operators() {
      return await fetch("/operators", {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      })
        .then((response) => response.text())
        .then((data) => {
					// Write path in history
          history.pushState(this.storage, "/operators", "/operators");

					// Write path to the current directory buffer
					core.page = 'operators';

          // Write content in document
          document.body.getElementsByTagName("main")[0].innerHTML = data;
        });
    }
    /**
     * Page: markets
     *
     * @return {void}
     */
    static async markets() {
      return await fetch("/markets", {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      })
        .then((response) => response.text())
        .then((data) => {
					// Write path in history
          history.pushState(this.storage, "/markets", "/markets");

					// Write path to the current directory buffer
					core.page = 'markets';

          // Write content in document
          document.body.getElementsByTagName("main")[0].innerHTML = data;
        });
    }

    /**
     * Page: workers
     *
     * @return {void}
     */
    static async workers() {
      return await fetch("/workers", {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      })
        .then((response) => response.text())
        .then((data) => {
          // Write path in history
          history.pushState(this.storage, "/workers", "/workers");

					// Write path to the current directory buffer
					core.page = 'workers';

          // Write content in document
          document.body.getElementsByTagName("main")[0].innerHTML = data;
        });
    }

    /**
     * Page: settings
     *
     * @return {void}
     */
    static async settings() {
      return await fetch("/settings", {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      })
        .then((response) => response.text())
        .then((data) => {
          document.body.getElementsByTagName("main")[0].innerHTML = data;
        });
    }

    /**
     * Page: account
     *
     * @return {void}
     */
    static async account() {
      // Initialization of the account identifier
      account = Cookies.get(`account_id`) ?? "account";

      return await fetch(`/${account}`, {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      })
        .then((response) => response.text())
        .then((data) => {
					// Write path in history
          history.pushState(this.storage, "/account", "/account");

					// Write path to the current directory buffer
					core.page = 'account';

          // Write content in document
          document.body.getElementsByTagName("main")[0].innerHTML = data;
        });
    }
  };
}
