// @ts-nocheck
export function download(file, fileName) {
  if (!fileName) {
    fileName = "PORTAL_FILE_".concat(new Date().toLocaleString());
  }
  if (typeof window.navigator.msSaveBlob !== 'undefined') {
    // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created.
    // These URLs will no longer resolve as the data backing the URL has been freed."
    window.navigator.msSaveBlob(file, fileName);
  } else {
    var URL = window.URL || window.webkitURL;
    var downloadUrl = file instanceof Blob ? URL.createObjectURL(file) : file;
    if (fileName) {
      // use HTML5 a[download] attribute to specify filename
      var a = document.createElement('a');

      // safari doesn't support this yet
      if (typeof a.download === 'undefined') {
        window.location.href = downloadUrl;
      } else {
        a.href = downloadUrl;
        a.download = fileName;
        document.body.appendChild(a);
        a.click();
      }
    } else {
      window.location.href = downloadUrl;
    }
    setTimeout(function () {
      URL.revokeObjectURL(downloadUrl);
    }, 100); // cleanup
  }
}

export var Ajax = {
  get: function get(url, fn, error) {
    // XMLHttpRequest对象用于在后台与服务器交换数据
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.onreadystatechange = function () {
      // readyState == 4说明请求已完成
      if (xhr.readyState == 4) {
        // 从服务器获得数据
        if (xhr.status == 200 || xhr.status < 300) {
          fn.call(this, JSON.parse(xhr.responseText));
        } else {
          error.call(this, xhr.status);
        }
      }
    };
    xhr.send();
  }
};