(function () {
  var STORAGE_KEY = 'ttk_quiz_eligible';

  function getBase() {
    return typeof window.__TTK_BASE === 'string' ? window.__TTK_BASE : '';
  }

  function querySuffix() {
    if (window.location.search) return window.location.search;
    if (window.__TTK_PRESERVED_QUERY) return '?' + window.__TTK_PRESERVED_QUERY;
    if (typeof window.__TTK_QUERY_SUFFIX === 'function') return window.__TTK_QUERY_SUFFIX();
    if (typeof window.getAllUtms === 'function') {
      var utms = window.getAllUtms(), p = new URLSearchParams(), k, has = false;
      for (k in utms) {
        if (utms[k]) { p.set(k, utms[k]); has = true; }
      }
      if (has) return '?' + p.toString();
    }
    return '';
  }

  function withQuery(path) {
    return path + querySuffix() + (window.location.hash || '');
  }

  function isEligible() {
    try {
      return sessionStorage.getItem(STORAGE_KEY) === '1';
    } catch (e) {
      return false;
    }
  }

  function setEligible() {
    try {
      sessionStorage.setItem(STORAGE_KEY, '1');
    } catch (e) {}
  }

  function normalizePath(path) {
    return (path || '').replace(/\/index\.html$/i, '').replace(/\/+$/, '') || '/';
  }

  function isResgatarPath(path) {
    var p = normalizePath(path);
    var base = getBase();
    var target = normalizePath(base + '/resgatar');
    return p === target;
  }

  function quizPath() {
    return getBase() + '/quiz/';
  }

  function isQuizPath(path) {
    var p = normalizePath(path);
    var base = getBase();
    var target = normalizePath(base + '/quiz');
    return p === target;
  }

  function resgatarUrl() {
    return (window.location.origin || '') + withQuery(getBase() + '/resgatar');
  }

  function redirectToQuiz() {
    window.location.replace((window.location.origin || '') + withQuery(quizPath()));
  }

  function guardResgatar() {
    if (isResgatarPath(window.location.pathname) && !isEligible()) {
      redirectToQuiz();
      return true;
    }
    return false;
  }

  function patchUrl(url) {
    if (!url || typeof url !== 'string') return url;
    try {
      var resolved = new URL(url, window.location.href);
      if (isResgatarPath(resolved.pathname) && !isEligible()) {
        return withQuery(quizPath());
      }
    } catch (e) {}
    return url;
  }

  function wrapHistory() {
    var origPush = history.pushState;
    var origReplace = history.replaceState;
    history.pushState = function (s, t, u) {
      return origPush.call(history, s, t, patchUrl(u));
    };
    history.replaceState = function (s, t, u) {
      return origReplace.call(history, s, t, patchUrl(u));
    };
    window.addEventListener('popstate', function () {
      if (guardResgatar()) return;
    });
  }

  window.TTKQuizGate = {
    STORAGE_KEY: STORAGE_KEY,
    isEligible: isEligible,
    setEligible: setEligible,
    isResgatarPath: isResgatarPath,
    isQuizPath: isQuizPath,
    quizUrl: function () {
      return (window.location.origin || '') + withQuery(quizPath());
    },
    quizPath: quizPath,
    resgatarUrl: resgatarUrl,
    guardResgatar: guardResgatar
  };

  if (isQuizPath(window.location.pathname) && isEligible()) {
    return;
  }

  if (guardResgatar()) return;

  wrapHistory();
})();
