var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
class UNLQa {
  static {
    __name(this, "UNLQa");
  }
  /**
   * @type { HTMLElement|null }
   */
  qaLink = null;
  webauditQueryUrl = "https://webaudit.unl.edu/registry/closest/?format=json&query=";
  constructor() {
    this.qaLink = document.getElementById("qa-test"), this.qaLink !== null && (this.#appendLinkWithLocation(), this.#appendLinkWithStar()), this.qaLink.dispatchEvent(new CustomEvent(UNLQa.events("qaReady"), {
      detail: {
        classInstance: this
      }
    })), window.UNL = window.UNL || {}, window.UNL.classes = window.UNL.classes || {}, window.UNL.classes["qa-test"] = this, this.qaLink.dispatchEvent(new Event("UNLClassReady"));
  }
  // The names of the events to be used easily
  static events(name) {
    const events = {
      qaReady: "qaReady"
    };
    return Object.freeze(events), name in events ? events[name] : void 0;
  }
  #appendLinkWithLocation() {
    let pathname = document.location.pathname;
    !pathname.match(/\..*$/) && !pathname.match(/\/$/) && (pathname += "/");
    const currentHref = this.qaLink.getAttribute("href");
    this.qaLink.setAttribute("href", `${currentHref}?url=${encodeURI(document.location.origin + pathname)}`);
  }
  async #appendLinkWithStar() {
    const gpa = await this.#fetchScore();
    if (gpa !== null) {
      if (gpa === 100) {
        const starIcon = document.createElement("span");
        starIcon.setAttribute("aria-hidden", "true"), starIcon.innerHTML = "&nbsp;&starf;";
        const starText = document.createElement("span");
        starText.classList.add("dcf-sr-only"), starText.innerText = "100% (gold star)", this.qaLink.after(starIcon, starText);
      } else if (gpa >= 90) {
        const starIcon = document.createElement("span");
        starIcon.setAttribute("aria-hidden", "true"), starIcon.innerHTML = "&nbsp;&star;";
        const starText = document.createElement("span");
        starText.classList.add("dcf-sr-only"), starText.innerText = `${gpa}% (silver star)`, this.qaLink.after(starIcon, starText);
      }
    }
  }
  #getBaseUrl() {
    const baseTag = document.querySelector("base");
    let baseUrl = window.location;
    if (baseTag !== null) {
      const baseTagHref = baseTag.getAttribute("href");
      if (baseTagHref.length > 1 && baseTagHref !== null)
        try {
          baseUrl = new URL(baseTagHref).toString();
        } catch {
        }
    }
    return baseUrl;
  }
  /**
   * 
   * @returns { Promise<Number|null> }
   */
  async #fetchScore() {
    try {
      const baseURIEncoded = encodeURIComponent(this.#getBaseUrl()), response = await fetch(`${this.webauditQueryUrl}${baseURIEncoded}`);
      if (!response.ok)
        return null;
      const jsonData = await response.json();
      return "gpa" in jsonData ? parseFloat(jsonData.gpa) : null;
    } catch (err) {
      return console.error(err), null;
    }
  }
}
export {
  UNLQa as default
};
//# sourceMappingURL=component.qa.js.map
