var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
import { setCookie, getCookie, loadJS } from "../lib/unl-utility.js?v=6.1.1";
import { l as loadStyleSheet } from "../chunks/chunk.dcf-utility.js?v=6.1.1";
const unlAlertCssUrl = "/wdn/templates_6.1/css/components-js/_unl-alert.css?v=6.1.1";
class UNLAlert {
  static {
    __name(this, "UNLAlert");
  }
  alertDataUrl;
  alertDataActiveIDs = [];
  serverCallTimeoutID;
  closedAlertCookieName = "unlAlertsC";
  alertDataReceivedCookieName = "unlAlertsData";
  activeAlertCookieName = "unlAlertsA";
  serverCallExecutionDelay = 3e4;
  // Delay (in milliseconds) before executing the checkIfServerCallIsNeeded function
  serverCallExecutionDelayTwo = 31e3;
  // Secondary delay (in milliseconds) before executing the checkIfServerCallIsNeeded function
  alertDataReceivedCookieMaxAge = 30;
  // Maximum age (in seconds) for the alertDataReceived cookie
  closedAlertCookieMaxAge = 3600;
  // Maximum age (in seconds) for the alertDataReceived cookie
  constructor() {
    loadStyleSheet(unlAlertCssUrl), this.alertDataUrl = "https://alert.unl.edu/json/unlcap.js?v=6.1.1", window.unlAlerts = window.unlAlerts || {}, window.unlAlerts.data = window.unlAlerts.data || {}, this.#fetchAlertData(), document.dispatchEvent(
      new CustomEvent(UNLAlert.events("UNLAlertReady"), {
        detail: {
          classInstance: this
        }
      })
    );
  }
  // The names of the events to be used easily
  static events(name) {
    const events = {
      UNLAlertReady: "UNLAlertReady"
    };
    return Object.freeze(events), name in events ? events[name] : void 0;
  }
  // Sets a cookie to indicate that there is an active alert
  #setActiveAlertCookie = /* @__PURE__ */ __name((flag) => {
    let value = 1, time = 60;
    flag === !1 && (value = "", time = -1), setCookie(this.activeAlertCookieName, value, time);
  }, "#setActiveAlertCookie");
  // Sets a cookie to indicate that there is an acknowledged alert
  #setClosedAlertCookie(alertDataActiveID) {
    const closedAlertCookieValue = this.#getClosedAlertCookie();
    closedAlertCookieValue.indexOf(alertDataActiveID) === -1 && (closedAlertCookieValue.push(alertDataActiveID), setCookie(
      this.closedAlertCookieName,
      closedAlertCookieValue.join(","),
      this.closedAlertCookieMaxAge
    ));
  }
  #getClosedAlertCookie() {
    const cookie = getCookie(this.closedAlertCookieName);
    return cookie ? cookie.split(",") : [];
  }
  #acknowledgeAlert(alertDataActiveID) {
    this.#setClosedAlertCookie(alertDataActiveID);
  }
  #isAlertAcknowledged(alertDataID) {
    return this.#getClosedAlertCookie().indexOf(alertDataID) !== -1;
  }
  #checkIfFetchCallNeeded = /* @__PURE__ */ __name(() => {
    this.#fetchAlertData();
  }, "#checkIfFetchCallNeeded");
  #toggleAlert() {
    const alert = document.getElementById("unlalert"), alertIconClose = document.getElementById("unlalert_icon_close"), alertIconWarning = document.getElementById("unlalert_icon_warning"), alertAction = document.getElementById("unlalert_action");
    if (alert.classList.contains("show")) {
      alert.classList.remove("show");
      const body = alert.closest("body");
      body && body.classList.remove("unlalert-shown"), alertIconClose.setAttribute("hidden", "true"), alertIconWarning.removeAttribute("hidden"), alertAction.classList.remove("dcf-sr-only"), alertAction.classList.add("dcf-ml-2", "dcf-txt-sm"), alertAction.textContent = "Show emergency alert";
      for (let counter = 0; counter < this.alertDataActiveIDs.length; counter++)
        this.#acknowledgeAlert(this.alertDataActiveIDs[counter]);
    } else {
      if (alert) {
        alert.classList.add("show");
        const body = alert.closest("body");
        body && body.classList.add("unlalert-shown");
      }
      alertIconWarning && alertIconWarning.setAttribute("hidden", "true"), alertIconClose && alertIconClose.removeAttribute("hidden"), alertAction && (alertAction.classList.remove("dcf-ml-2", "dcf-txt-sm"), alertAction.classList.add("dcf-sr-only"), alertAction.textContent = "Hide emergency alert");
    }
  }
  #alertUser(alertData) {
    this.#setActiveAlertCookie(!0), this.alertDataActiveIDs = [];
    let unlAlertDivWrapper = document.getElementById("unlalert"), alertToggle = document.getElementById("unlalert_toggle"), unlAlertContent, containsExtreme = !1, info = alertData.info, effectiveDate = "", web, alertContentHTML, alertAcknowledgmentStatus = !0;
    const alertDataID = alertData.identifier || +/* @__PURE__ */ new Date(), dcfHeaderDiv = document.getElementById("dcf-header"), alertIconSvg = `<svg id="unlalert" class="dcf-h-4 dcf-w-4 dcf-fill-current" viewBox="0 0 24 24" height="16" width="16" focusable="false" aria-hidden="true">
            <g id="unlalert_icon_close">
                <path d="M13.4 12 23.7 1.7c.4-.4.4-1 0-1.4s-1-.4-1.4 0L12 10.6 1.7.3C1.3-.1.7-.1.3.3s-.4 1 0 1.4L10.6 12 .3 22.3c-.4.4-.4 1 0 1.4.2.2.4.3.7.3s.5-.1.7-.3L12 13.4l10.3 10.3c.2.2.5.3.7.3s.5-.1.7-.3c.4-.4.4-1 0-1.4L13.4 12z">
                </path>
            </g>
            <g id="unlalert_icon_warning" hidden>
                <path 
                    d="M12 17.3c-.6 0-1-.4-1-1V8.7c0-.6.4-1 1-1s1 .4 1 1v7.7c0 .5-.4.9-1 .9z">
                </path>
                <path 
                    d="M23 24H1c-.3 0-.7-.2-.9-.5s-.2-.7 0-1l11-22c.3-.7 1.5-.7 1.8 0l11 22c.2.3.1.7 0 1s-.6.5-.9.5zM2.6 22h18.8L12 3.2 2.6 22z">
                </path>
                <path 
                    d="M12 21c-.8 0-1.5-.7-1.5-1.5S11.2 18 12 18s1.5.7 1.5 1.5S12.8 21 12 21z">
                </path>
            </g>
            <path fill="none" d="M0 0h24v24H0z">
            </path>
        </svg>`;
    info instanceof Array || (info = [info]);
    for (let counter = 0; counter < info.length; counter++)
      info[counter].severity === "Extreme" && (containsExtreme = !0);
    if (containsExtreme) {
      this.alertDataActiveIDs.push(alertDataID), alertAcknowledgmentStatus = this.#isAlertAcknowledged(alertDataID), effectiveDate = new Date(alertData.sent).toLocaleString();
      for (let counter = 0; counter < info.length; counter++) {
        if (unlAlertDivWrapper)
          counter === 0 && (unlAlertContent = document.getElementById("unlalert_content"), unlAlertContent && (unlAlertContent.innerHTML = ""));
        else {
          unlAlertDivWrapper = document.createElement("div"), unlAlertDivWrapper.setAttribute("id", "unlalert"), unlAlertDivWrapper.className = "dcf-bleed dcf-z-1", unlAlertDivWrapper.setAttribute("role", "alert"), unlAlertDivWrapper.style.position = "absolute", unlAlertDivWrapper.style.top = "-1000px", dcfHeaderDiv.parentNode.insertBefore(unlAlertDivWrapper, dcfHeaderDiv), unlAlertContent = document.createElement("div"), unlAlertContent.className = "dcf-relative dcf-col-gap-vw dcf-row-gap-6", unlAlertContent.setAttribute("id", "unlalert_content");
          const unlAlertInnerDivWrapper = document.createElement("div");
          unlAlertInnerDivWrapper.className = "dcf-wrapper", unlAlertInnerDivWrapper.appendChild(unlAlertContent), unlAlertDivWrapper.appendChild(unlAlertInnerDivWrapper);
        }
        web = info[counter].web || "https://www.unl.edu/", alertContentHTML = `<div class="unlalert-info"><header><h2 class="unlalert-heading dcf-mb-2 dcf-txt-2xs dcf-regular dcf-lh-3 dcf-uppercase unl-ls-2">Emergency alert</h2><h3 class="unlalert-headline dcf-mt-0 dcf-txt-h4">
                ${info[counter].headline}
                </h3></header><p class="unlalert-desc dcf-mb-0 dcf-txt-xs">
                ${info[counter].description} 
                </p>`, info[counter].instruction && (alertContentHTML += `<p class="unlalert-desc dcf-mt-2 dcf-mb-0 dcf-txt-xs"> 
                    ${info[counter].instruction}
                    </p>`), alertContentHTML += `</div><footer class="unlalert-meta dcf-d-grid dcf-col-gap-vw dcf-row-gap-5 dcf-txt-2xs"><div class="unlalert-datetime"><span class="unlalert-heading dcf-d-block dcf-mb-1 dcf-lh-3 dcf-uppercase unl-ls-2">Issued </span>
                ${effectiveDate}
                </div><div class="unlalert-link"><span class="unlalert-heading dcf-d-block dcf-mb-1 dcf-lh-3 dcf-uppercase unl-ls-2">Additional info (if&nbsp;available)<span class="dcf-sr-only">: </span></span>
                <a href="${web}">${web}</a>
                </div></footer>`, unlAlertContent.insertAdjacentHTML("beforeend", alertContentHTML);
      }
      if (!alertToggle) {
        const alertToggleSpan = document.createElement("span");
        alertToggleSpan.setAttribute("id", "unlalert_action"), alertToggleSpan.className = "dcf-ml-2 dcf-txt-sm", alertToggleSpan.textContent = "Show emergency alert", alertToggle = document.createElement("button"), alertToggle.className = "dcf-btn dcf-btn-tertiary dcf-txt-decor-none dcf-d-flex dcf-ai-center dcf-jc-center dcf-txt-base", alertToggle.setAttribute("id", "unlalert_toggle"), alertToggle.insertAdjacentHTML("beforeend", alertIconSvg), alertToggle.appendChild(alertToggleSpan);
        const alertContentParent = unlAlertContent.parentElement;
        alertContentParent && alertContentParent.appendChild(alertToggle), alertToggle.addEventListener("click", () => this.#toggleAlert());
      }
      if (!alertAcknowledgmentStatus)
        unlAlertDivWrapper.classList.contains("show") || alertToggle.click();
      else {
        const alertIconClose = alertToggle.querySelector("#unlalert_icon_close"), alertIconWarning = alertToggle.querySelector("#unlalert_icon_warning");
        alertIconClose && alertIconClose.setAttribute("hidden", "true"), alertIconWarning && alertIconWarning.removeAttribute("hidden");
      }
    }
  }
  #noAlert() {
    this.#setActiveAlertCookie(!1);
    const unlAlertDivWrapper = document.getElementById("unlalert");
    unlAlertDivWrapper && unlAlertDivWrapper.remove();
  }
  async #fetchAlertData() {
    try {
      const dataURL = new URL(this.alertDataUrl);
      dataURL.searchParams.set("cb", Date.now()), await loadJS(dataURL.toString());
      const unlAlertsData = window.unlAlerts.data;
      unlAlertsData && unlAlertsData.alert && window.top === window && unlAlertsData.alert && unlAlertsData.alert.info ? this.#alertUser(unlAlertsData.alert) : this.#noAlert(), clearTimeout(this.serverCallTimeoutID), this.serverCallTimeoutID = setTimeout(
        () => this.#checkIfFetchCallNeeded(),
        this.serverCallExecutionDelayTwo
      );
    } catch {
      console.error("An unexpected error occurred while fetching the alert data.");
    }
  }
}
export {
  UNLAlert as default
};
//# sourceMappingURL=component.alert.js.map
