function formatAndValidateJson(errorElement) {
  const input = document.querySelector('#jsonBlock');
  const processInfo = document.getElementById(errorElement);

  const address = window.location.protocol + "//" + window.location.hostname + ":" + 8081 + "/json/formatting"

  fetch(address, {
    method: 'POST',
    body: input.textContent
  })
  .then(async (response) => {
    const promise = response.json();
    if (!response.ok) {
      throw Error(await promise);
    }

    return promise;
  })
  .then((data) => {
    input.innerText = data.data;
    processInfo.innerText = "";
    hljs.highlightElement(input);

    processInfo.innerHTML = "<b style='color: green'>Computed in </b> <span style='color: green'>" + data.time + "ms</span>";
  })
  .catch((error) => {
    processInfo.innerHTML = "<b style='color: red'>" + error.data + "</b>";
    console.error('Error:', error);
  });
}

function minimizeJson(errorElement) {
  const input = document.querySelector('#jsonBlock');
  const processInfo = document.getElementById(errorElement);

  const address = window.location.protocol + "//" + window.location.hostname + ":" + 8081 + "/json/minimize"

  fetch(address, {
    method: 'POST',
    body: input.textContent
  })
  .then(async (response) => {
    const promise = response.json();
    if (!response.ok) {
      throw Error(await promise);
    }

    return promise;
  })
  .then((data) => {
    input.innerText = data.data;
    processInfo.innerText = "";
    hljs.highlightElement(input);

    processInfo.innerHTML = "<b style='color: green'>Computed in </b> <span style='color: green'>" + data.time + "ms</span>";
  })
  .catch((error) => {
    processInfo.innerHTML = "<b style='color: red'>" + error.data + "</b>";
    console.error('Error:', error);
  });
}

function clearJsonData() {
  const input = document.querySelector('#jsonBlock');
  input.textContent = "";
}

function insertDefaultJson() {
  const input = document.querySelector('#jsonBlock');
  input.textContent = "{\"enter\": \"your\", \"json\": \"here\"}";
  hljs.highlightElement(input);
}