(function () {
  "use strict";

  var script = document.currentScript;
  var baseUrl = script && script.src ? new URL(".", script.src) : new URL("./", window.location.href);
  var configUrl = new URL("special.json", baseUrl);
  var cssUrl = new URL("special-popup.css?v=1.0.3", baseUrl);
  var storagePrefix = "specialPopup:";

  function loadStyles() {
    if (document.querySelector('link[data-special-popup="styles"]')) {
      return;
    }

    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.href = cssUrl.href;
    link.dataset.specialPopup = "styles";
    document.head.appendChild(link);
  }

  function text(value, fallback) {
    return typeof value === "string" && value.trim() ? value.trim() : fallback;
  }

  function isExpired(expiresAt) {
    if (!expiresAt) {
      return false;
    }

    var expiryDate = new Date(String(expiresAt) + "T23:59:59");
    return Number.isNaN(expiryDate.getTime()) ? false : new Date() > expiryDate;
  }

  function dismissed(config) {
    var mode = config.dismissMode || "session";
    var key = storagePrefix + (config.id || config.expiresAt || "default");

    if (mode === "none") {
      return false;
    }

    try {
      if (mode === "local") {
        return window.localStorage.getItem(key) === "dismissed";
      }

      return window.sessionStorage.getItem(key) === "dismissed";
    } catch (error) {
      return false;
    }
  }

  function rememberDismissal(config) {
    var mode = config.dismissMode || "session";
    var key = storagePrefix + (config.id || config.expiresAt || "default");

    if (mode === "none") {
      return;
    }

    try {
      if (mode === "local") {
        window.localStorage.setItem(key, "dismissed");
        return;
      }

      window.sessionStorage.setItem(key, "dismissed");
    } catch (error) {
      // Storage can be unavailable in private browsing or strict privacy modes.
    }
  }

  function mailtoHref(config) {
    var emailTo = text(config.emailTo, "");
    var subject = text(config.emailSubject, "I'm interested in this special");
    var body = text(config.emailBody, "");
    var params = new URLSearchParams();

    params.set("subject", subject);

    if (body) {
      params.set("body", body);
    }

    return "mailto:" + emailTo.replace(/\s+/g, "") + "?" + params.toString();
  }

  function element(tag, className, content) {
    var node = document.createElement(tag);

    if (className) {
      node.className = className;
    }

    if (content) {
      node.textContent = content;
    }

    return node;
  }

  function buildPopup(config) {
    var popup = element("div", "sp-popup");
    var backdrop = element("div", "sp-popup__backdrop");
    var dialog = element("section", "sp-popup__dialog");
    var media = element("div", "sp-popup__media");
    var content = element("div", "sp-popup__content");
    var close = element("button", "sp-popup__close", "x");
    var brandBlock = null;
    var brandKicker = null;
    var brand = null;
    var logo = null;
    var eyebrow = null;
    var headline = element("h2", "sp-popup__headline", text(config.headline, "Website Special"));
    var headlineSub = config.headlineSub ? element("span", "sp-popup__headline-sub", text(config.headlineSub, "")) : null;
    var message = element("p", "sp-popup__message", text(config.message, "Get in touch today and ask about our current offer."));
    var price = null;
    var note = config.note ? element("p", "sp-popup__note", text(config.note, "")) : null;
    var button = element("a", "sp-popup__button", text(config.buttonText, "I'm Interested"));

    popup.setAttribute("role", "presentation");
    backdrop.setAttribute("data-special-popup-close", "");
    dialog.setAttribute("role", "dialog");
    dialog.setAttribute("aria-modal", "true");
    dialog.setAttribute("aria-labelledby", "special-popup-title");
    headline.id = "special-popup-title";
  
    if (headlineSub) {
  headline.appendChild(headlineSub);
}

    close.type = "button";
    close.setAttribute("aria-label", "Close special popup");
    close.setAttribute("data-special-popup-close", "");

    button.href = text(config.buttonUrl, "") || mailtoHref(config);
  
  if (config.eyebrowLogo) {
  	eyebrow = document.createElement("img");
  	eyebrow.src = config.eyebrowLogo;
  	eyebrow.alt = text(config.eyebrowLogoAlt, text(config.eyebrow, "Kyocera"));
  	eyebrow.className = "sp-popup__eyebrow-logo";
  	eyebrow.loading = "eager";
  	eyebrow.addEventListener("error", function () {
    var fallback = element("p", "sp-popup__eyebrow", text(config.eyebrow, "Limited Time Special"));
    eyebrow.replaceWith(fallback);
    eyebrow = fallback;
  });
} else {
  	eyebrow = element("p", "sp-popup__eyebrow", text(config.eyebrow, "Limited Time Special"));
}

    if (config.brandKicker) {
  	  brandKicker = element("p", "sp-popup__brand-kicker", text(config.brandKicker, ""));
}

	if (config.logo || config.brandText) {
  	  brandBlock = element("div", "sp-popup__brand-block");
}

	if ((config.logo || config.brandText) && brandBlock) {
  	  brand = element("div", "sp-popup__brand");
  	  brandBlock.appendChild(brand);
}

	if (config.logo && brand) {
  	  logo = document.createElement("img");
  	  logo.src = config.logo;
  	  logo.alt = text(config.logoAlt, "");
  	  logo.className = "sp-popup__logo";
  	  logo.loading = "eager";
  	  logo.addEventListener("error", function () {
      logo.remove();
  });
  	  brand.appendChild(logo);
}

	if (config.brandText && brand) {
  	  brand.appendChild(element("span", "sp-popup__brand-text", text(config.brandText, "")));
}

	if (config.price) {
  price = element("div", "sp-popup__price");
  price.appendChild(element("strong", "sp-popup__price-value", text(config.price, "")));

  if (config.priceNote) {
    price.appendChild(element("span", "sp-popup__price-note", text(config.priceNote, "")));
  }
}

	if (brandKicker) {
  	  media.appendChild(brandKicker);
}

	if (config.image) {
  	  var img = document.createElement("img");
  	  img.src = config.image;
  	  img.alt = text(config.imageAlt, "");
  	  img.className = "sp-popup__product";
  	  img.loading = "eager";
  	  media.appendChild(img);

  	  img.addEventListener("error", function () {
      img.remove();
      media.classList.add("is-empty");
  });
} 	else {
  	  media.classList.add("is-empty");
}

	if (price) {
  	  media.appendChild(price);
}

    content.appendChild(close);

    if (brandBlock) {
      content.appendChild(brandBlock);
    }

    content.append(eyebrow, headline, message);

    if (note) {
      content.appendChild(note);
    }

    content.appendChild(button);
    dialog.append(media, content);
    popup.append(backdrop, dialog);

    return popup;
  }

  function showPopup(config) {
    var popup = buildPopup(config);
    var closeButtons = popup.querySelectorAll("[data-special-popup-close]");

    var onKeydown = function (event) {
      if (event.key === "Escape") {
        close();
      }
    };

    var close = function () {
      rememberDismissal(config);
      popup.remove();
      document.documentElement.classList.remove("sp-popup-lock");
      document.removeEventListener("keydown", onKeydown);
    };

    closeButtons.forEach(function (button) {
      button.addEventListener("click", close);
    });

    document.addEventListener("keydown", onKeydown);
    document.body.appendChild(popup);
    document.documentElement.classList.add("sp-popup-lock");

    var focusTarget = popup.querySelector(".sp-popup__button");

    if (focusTarget) {
      focusTarget.focus({ preventScroll: true });
    }
  }

  function init() {
    fetch(configUrl.href, { cache: "no-store" })
      .then(function (response) {
        if (!response.ok) {
          throw new Error("Special config could not be loaded.");
        }

        return response.json();
      })
      .then(function (config) {
        if (!config || config.enabled === false || isExpired(config.expiresAt) || dismissed(config)) {
          return;
        }

        loadStyles();

        window.setTimeout(function () {
          showPopup(config);
        }, Number(config.showDelayMs) || 0);
      })
      .catch(function () {
        // The popup should never break the host website if the special files are missing.
      });
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", init);
  } else {
    init();
  }
})();