var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
import { removeSessionStorage, getSessionStorage, isValidHttpUrl, setSessionStorage } from "../lib/unl-utility.js?v=6.1.1";
class UNLNoticeBanner {
  static {
    __name(this, "UNLNoticeBanner");
  }
  bannerContainer = null;
  bannerElement = null;
  messageSource = null;
  messageKey = null;
  skipNav = document.getElementById("dcf-skip-nav");
  clearMessageIntervalSecs = 15 * 60 * 1e3;
  constructor(options = {}) {
    this.messageSource = "https://its-unl-cms-prd-s3.s3.amazonaws.com/wdn-message.html", this.messageKey = "UNLNoticeMessage", this.#getMessage().then((messageToDisplay) => {
      this.#buildBanner(messageToDisplay);
    }), "clearMessageIntervalSecs" in options && typeof options.clearMessageIntervalSecs == "number" && (this.clearMessageIntervalSecs = options.clearMessageIntervalSecs), setInterval(() => {
      removeSessionStorage(this.messageKey);
    }, this.clearMessageIntervalSecs), document.dispatchEvent(new CustomEvent(UNLNoticeBanner.events("UNLBannerReady"), {
      detail: {
        classInstance: this
      }
    }));
  }
  // The names of the events to be used easily
  static events(name) {
    const events = {
      UNLBannerReady: "UNLBannerReady"
    };
    return Object.freeze(events), name in events ? events[name] : void 0;
  }
  /**
   * Builds the banner and places it on the page
   * @param { string|null } messageToDisplay 
   */
  #buildBanner(messageToDisplay) {
    messageToDisplay === null && messageToDisplay === "" || (this.bannerElement = document.createElement("div"), this.bannerElement.setAttribute("id", "unl-notice-banner"), this.bannerElement.classList.add("dcf-d-none@print"), this.bannerElement.innerHTML = messageToDisplay, this.skipNav !== null ? this.skipNav.parentNode.insertBefore(this.bannerElement, this.skipNav.nextSibling) : document.body.prepend(this.bannerElement));
  }
  /**
   * Gets the data from session, or fetches from url, or the message source
   * It will also store the message in session data
   * @returns { Promise<String> } Message string
   */
  async #getMessage() {
    const sessionData = getSessionStorage(this.messageKey);
    if (sessionData !== null)
      return sessionData;
    let returnText = "";
    return isValidHttpUrl(this.messageSource) ? returnText = await this.#fetchMessageFromUrl() : returnText = this.messageSource, setSessionStorage(this.messageKey, returnText), returnText;
  }
  /**
   * Fetches message from Url
   * @returns { Promise<String> } Fetched message
   */
  async #fetchMessageFromUrl() {
    const response = await fetch(this.messageSource);
    if (!response.ok)
      throw new Error(`Failed to fetch banner data: ${response.status}`);
    return await response.text();
  }
}
export {
  UNLNoticeBanner as default
};
//# sourceMappingURL=component.banner.js.map
