(function () {
  "use strict";

  var defaults = {
    enabled: true,
    customerName: "OOO",
    eventUrl: "/school/02-membership-v2.html",
    storageKey: "sm_vvip_invite_v2_hide_until"
  };
  var config = Object.assign({}, defaults, window.SM_VVIP_INVITE_V2 || window.SM_VVIP_INVITE || {});

  if (window.SM_IS_VVIP === false || config.enabled === false) return;

  function todayKey() {
    var d = new Date();
    return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0");
  }

  try { if (localStorage.getItem(config.storageKey) === todayKey()) return; } catch (e) {}

  function closePopup(lockToday) {
    var overlay = document.querySelector(".sm-vvip2-overlay");
    if (lockToday) { try { localStorage.setItem(config.storageKey, todayKey()); } catch (e) {} }
    if (overlay) overlay.remove();
    document.documentElement.classList.remove("sm-vvip2-lock");
  }

  function esc(s) {
    return String(s || "OOO").replace(/[<>&"']/g, function (ch) {
      return ({ "<": "&lt;", ">": "&gt;", "&": "&amp;", "\"": "&quot;", "'": "&#39;" })[ch];
    });
  }

  function render() {
    if (document.querySelector(".sm-vvip2-overlay")) return;
    var name = esc(config.customerName);
    var overlay = document.createElement("div");
    overlay.className = "sm-vvip2-overlay";
    overlay.setAttribute("role", "dialog");
    overlay.setAttribute("aria-modal", "true");
    overlay.setAttribute("aria-label", "스쿨뮤직 VVIP 멤버십 초청");
    overlay.innerHTML =
      '<div class="sm-vvip2-modal">' +
        '<button class="sm-vvip2-close" type="button" aria-label="닫기">' +
          '<svg width="12" height="12" viewBox="0 0 12 12" aria-hidden="true">' +
            '<path d="M2 2 L10 10 M10 2 L2 10" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/>' +
          '</svg>' +
        '</button>' +
        '<div class="sm-vvip2-inner">' +
          '<span class="sm-vvip2-kicker">스쿨뮤직 VVIP 단독 초청</span>' +
          '<h2 class="sm-vvip2-title">스쿨뮤직 멤버십 가입 할인</h2>' +
          '<p class="sm-vvip2-copy">스쿨뮤직이 특별히 선정한 <span class="sm-vvip2-name">' + name + '</span> 고객님<br>VVIP 고객에게만 제안합니다.</p>' +
          '<div class="sm-vvip2-actions">' +
            '<a class="sm-vvip2-primary" href="' + config.eventUrl + '">이벤트 페이지 이동</a>' +
            '<button class="sm-vvip2-muted-btn" type="button">오늘 하루 보지 않기</button>' +
          '</div>' +
        '</div>' +
      '</div>';
    document.body.appendChild(overlay);
    document.documentElement.classList.add("sm-vvip2-lock");
    overlay.querySelector(".sm-vvip2-close").addEventListener("click", function () { closePopup(false); });
    overlay.querySelector(".sm-vvip2-muted-btn").addEventListener("click", function () { closePopup(true); });
    overlay.addEventListener("click", function (event) { if (event.target === overlay) closePopup(false); });
    document.addEventListener("keydown", function onKey(event) {
      if (event.key === "Escape") { document.removeEventListener("keydown", onKey); closePopup(false); }
    });
  }

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