/**
 * 作者体验活动 · 五节连载
 * ①目录 → ②公农历 → ③干支 → ④活页 → ⑤排盘
 */
(function (w) {
  "use strict";
  var STEPS = [
    { id: "hub", n: "①", title: "目录", hint: "活动总目" },
    { id: "cal", n: "②", title: "公农历", hint: "日期时辰 → 八字" },
    { id: "gz", n: "③", title: "干支", hint: "四柱 · 五虎五鼠" },
    { id: "huoye", n: "④", title: "活页", hint: "定体 · 三阶段" },
    { id: "zeisen", n: "⑤", title: "排盘", hint: "泽森全盘" }
  ];
  var HREF = {
    preview: {
      hub: "./index.html?unlock=master",
      cal: "./calendar_bazi_activity.html?unlock=master",
      gz: "./author_experience_check.html?unlock=master",
      huoye: "../author_xin_pan_huoye.html?unlock=master",
      zeisen: "../app/zeisen.html?unlock=master&theme=light&go=1"
    },
    root: {
      hub: "./preview/index.html?unlock=master",
      cal: "./preview/calendar_bazi_activity.html?unlock=master",
      gz: "./preview/author_experience_check.html?unlock=master",
      huoye: "./author_xin_pan_huoye.html?unlock=master",
      zeisen: "./app/zeisen.html?unlock=master&theme=light&go=1"
    },
    app: {
      hub: "../preview/index.html?unlock=master",
      cal: "../preview/calendar_bazi_activity.html?unlock=master",
      gz: "../preview/author_experience_check.html?unlock=master",
      huoye: "../author_xin_pan_huoye.html?unlock=master",
      zeisen: "./zeisen.html?unlock=master&theme=light&go=1"
    }
  };
  function stashGet() {
    try { return JSON.parse(sessionStorage.getItem("kr_serial") || "{}"); } catch (e) { return {}; }
  }
  function stashSet(part) {
    var o = stashGet();
    Object.keys(part || {}).forEach(function (k) { if (part[k] != null) o[k] = part[k]; });
    try { sessionStorage.setItem("kr_serial", JSON.stringify(o)); } catch (e) { }
    return o;
  }
  function withCarry(href) {
    var o = stashGet();
    var has = function (k) { return new RegExp("[?&]" + k + "=").test(href); };
    var extra = [];
    if (o.name && !has("name")) extra.push("name=" + encodeURIComponent(o.name));
    if (o.date && !has("date")) extra.push("date=" + encodeURIComponent(o.date));
    if (o.time && !has("time")) extra.push("time=" + encodeURIComponent(o.time));
    if (o.pillars && !has("pillars")) extra.push("pillars=" + encodeURIComponent(String(o.pillars).replace(/\s+/g, "")));
    if (o.gender === "F" && !has("gender")) extra.push("gender=F");
    if (o.gender === "F" && !has("sex")) extra.push("sex=female");
    if (o.city && !has("city")) extra.push("city=" + encodeURIComponent(o.city));
    if (o.city && !has("birth")) extra.push("birth=" + encodeURIComponent(o.city));
    if (o.stand && !has("stand")) extra.push("stand=" + encodeURIComponent(o.stand));
    if (!extra.length) return href;
    return href + (href.indexOf("?") >= 0 ? "&" : "?") + extra.join("&");
  }
  function hrefOf(base, id) {
    var map = HREF[base] || HREF.preview;
    return withCarry(map[id] || map.hub);
  }
  function render(opts) {
    opts = opts || {};
    var base = opts.base || "preview";
    var cur = opts.current || "hub";
    var idx = 0;
    STEPS.forEach(function (s, i) { if (s.id === cur) idx = i; });
    var prev = idx > 0 ? STEPS[idx - 1] : null;
    var next = idx < STEPS.length - 1 ? STEPS[idx + 1] : null;
    var chips = STEPS.map(function (s, i) {
      var on = s.id === cur ? " on" : "";
      var done = i < idx ? " done" : "";
      return '<a class="serial-chip' + on + done + '" href="' + hrefOf(base, s.id) + '">' + s.n + s.title + "</a>";
    }).join("");
    var nav = "";
    if (prev) nav += '<a class="serial-btn ghost" href="' + hrefOf(base, prev.id) + '">← 上一节 · ' + prev.title + "</a>";
    if (next) nav += '<a class="serial-btn" href="' + hrefOf(base, next.id) + '">下一节 · ' + next.title + " →</a>";
    var html = '<div class="serial" data-serial="' + cur + '">' +
      '<div class="serial-kicker">作者体验活动 · 连载</div>' +
      '<div class="serial-chips">' + chips + "</div>" +
      (nav ? '<div class="serial-nav">' + nav + "</div>" : "") +
      "</div>";
    var host = opts.mountId ? document.getElementById(opts.mountId) : null;
    if (host) host.innerHTML = html;
    else {
      var box = document.createElement("div");
      box.innerHTML = html;
      var first = document.querySelector("main") || document.body;
      first.insertBefore(box.firstChild, first.firstChild);
    }
    return { prev: prev, next: next, href: hrefOf };
  }
  var css = ".serial{margin:0 0 14px;padding:10px 12px;border:1px solid rgba(111,155,122,.35);border-radius:12px;background:rgba(0,0,0,.2)}" +
    ".serial-kicker{color:#c4a574;font-size:12.5px;margin:0 0 8px}" +
    ".serial-chips{display:flex;flex-wrap:wrap;gap:6px}" +
    ".serial-chip{display:inline-block;padding:4px 8px;border-radius:8px;border:1px solid rgba(111,155,122,.35);color:#9aa89c;text-decoration:none;font-size:12.5px}" +
    ".serial-chip.on{background:rgba(111,155,122,.22);color:#c4a574;border-color:#6f9b7a}" +
    ".serial-chip.done{color:#7eb889}" +
    ".serial-nav{display:flex;flex-wrap:wrap;gap:8px;margin-top:10px}" +
    ".serial-btn{display:inline-block;padding:7px 12px;border-radius:8px;background:#6f9b7a;color:#0e120f;font-weight:700;text-decoration:none;font-size:13.5px}" +
    ".serial-btn.ghost{background:transparent;color:#c4a574;border:1px solid #c4a574}" +
    "body[data-theme=light] .serial{background:#fffdf8;border-color:rgba(63,107,79,.32)}" +
    "body[data-theme=light] .serial-kicker,body[data-theme=light] .serial-chip.on{color:#8a6230}" +
    "body[data-theme=light] .serial-chip{color:#4a5a50;border-color:rgba(63,107,79,.32)}";
  if (!document.getElementById("kr-serial-css")) {
    var st = document.createElement("style");
    st.id = "kr-serial-css";
    st.textContent = css;
    document.head.appendChild(st);
  }
  w.KRSerial = { steps: STEPS, render: render, stashGet: stashGet, stashSet: stashSet, hrefOf: hrefOf };
})(window);
