/** 全站设置：解锁、语言、默认盘、参数版本 — © 2026 Jason Li */
(function (global) {
  var KEYS = {
    unlock: 'kr_author_unlock',
    lang: 'kr_lang',
    defaultChart: 'kr_default_chart',
    paramsAck: 'kr_params_ack_2026_08_11'
  };

  function get(k) {
    try { return localStorage.getItem(k) || sessionStorage.getItem(k); } catch (e) { return null; }
  }
  function set(k, v) {
    try { localStorage.setItem(k, v); sessionStorage.setItem(k, v); } catch (e) {}
  }
  function del(k) {
    try { localStorage.removeItem(k); sessionStorage.removeItem(k); } catch (e) {}
  }

  var params = new URLSearchParams(location.search);
  var unlock = (params.get('unlock') || params.get('beta') || params.get('tier') || '').toLowerCase();
  if (['master', 'author', 'all', 'full', 'resonance', 'resonancemaster', '1', 'true'].indexOf(unlock) >= 0) {
    set(KEYS.unlock, 'master');
  }
  if (params.get('lock') === '1') del(KEYS.unlock);

  var KRSettings = {
    keys: KEYS,
    isAuthor: function () {
      return get(KEYS.unlock) === 'master';
    },
    unlockAuthor: function () {
      set(KEYS.unlock, 'master');
      return true;
    },
    lock: function () { del(KEYS.unlock); },
    lang: function () { return get(KEYS.lang) || 'zh'; },
    setLang: function (l) { set(KEYS.lang, l); },
    paramsVersion: function () {
      return (global.KRParams && global.KRParams.version) || '2026-08-11';
    },
    ackParams: function () { set(KEYS.paramsAck, '1'); },
    hasAckParams: function () { return get(KEYS.paramsAck) === '1'; },
    saveDefaultChart: function (obj) {
      set(KEYS.defaultChart, JSON.stringify(obj || {}));
    },
    loadDefaultChart: function () {
      try { return JSON.parse(get(KEYS.defaultChart) || 'null'); } catch (e) { return null; }
    },
    badgeHtml: function () {
      return this.isAuthor()
        ? '<span class="kr-badge ok">作者权限 · 已解锁 · 参数 ' + this.paramsVersion() + '</span>'
        : '<span class="kr-badge">访客 · <a href="?unlock=master">解锁作者</a></span>';
    },
    ensureStyles: function () {
      if (document.getElementById('kr-settings-style')) return;
      var s = document.createElement('style');
      s.id = 'kr-settings-style';
      s.textContent = '.kr-badge{display:inline-block;font-size:12px;padding:4px 10px;border-radius:8px;border:1px solid rgba(111,155,122,.35);color:#9aa89c;margin:0 0 10px}.kr-badge.ok{color:#7eb889;border-color:rgba(126,184,137,.55);background:rgba(126,184,137,.1)}.kr-badge a{color:#c4a574}';
      document.head.appendChild(s);
    }
  };

  KRSettings.ensureStyles();
  global.KRSettings = KRSettings;
  // back-compat with author_unlock.js
  global.KRAuthor = {
    unlocked: KRSettings.isAuthor(),
    level: KRSettings.isAuthor() ? 'master' : 'guest',
    ensure: function () {
      this.unlocked = KRSettings.isAuthor();
      this.level = this.unlocked ? 'master' : 'guest';
      return this.unlocked;
    },
    badgeHtml: function () { return KRSettings.badgeHtml(); }
  };
})(typeof window !== 'undefined' ? window : this);
