// embed(iframe)模式:父页面经 postMessage 推送 token → 进入应用;401 后重签。
// 顶层无副作用,boot 决定是否调 embedInit。导出 embedInit(boot 调)+
// embedPostToParent / embedShowWaiting(auth 的 logout 在 embed 下通知父页面/显示等待态)。
import { state, LS_TOKEN, EMBED_PARENT_ORIGIN, EMBED_INITIAL_TASK_ID, setIdentity } from "./state.js";
import { $ } from "./dom.js";
import { enterApp } from "./main.js";
import { loadTaskList, selectTask } from "./chat.js";

// embed 首个 task 自动定位的一次性标志(仅 embed 段使用)
let _embedInitialTaskHandled = false;

// ───── embed mode ─────
export function embedPostToParent(msg) {
  if (!EMBED_PARENT_ORIGIN || window.parent === window) return;
  try { window.parent.postMessage(msg, EMBED_PARENT_ORIGIN); } catch (e) {}
}
export function embedShowWaiting(text, isErr) {
  const w = $("embed-waiting");
  if (!w) return;
  if (isErr) {
    w.querySelector(".text").textContent = "";
    w.querySelector(".err").textContent = text || "";
    w.querySelector(".spinner").style.display = "none";
  } else {
    w.querySelector(".text").textContent = text || "等待登录…";
    w.querySelector(".err").textContent = "";
    w.querySelector(".spinner").style.display = "";
  }
}
function embedHandleMessage(e) {
  if (e.origin !== EMBED_PARENT_ORIGIN) return;
  const d = e.data || {};
  if (d.type === "zcbot-token" && d.token && d.user_id) {
    state.token = d.token;
    localStorage.setItem(LS_TOKEN, state.token);
    // 父页面可带 name/user_name/email;缺的由 enterApp → loadRole 拉 /v1/me 补齐
    setIdentity({ user_id: d.user_id, name: d.name, user_name: d.user_name, email: d.email });
    document.body.classList.remove("embed-waiting");
    if ($("app").classList.contains("ready")) {
      // 401 后重签:重载列表,不重复 enterApp / 不重复定位 task(尊重用户中间切过的选择)
      loadTaskList();
    } else {
      enterApp();
      // 首次签发:若 URL 带 task_id,定位到该 task(loadMessages 由 selectTask 触发)
      if (EMBED_INITIAL_TASK_ID && !_embedInitialTaskHandled) {
        _embedInitialTaskHandled = true;
        selectTask(EMBED_INITIAL_TASK_ID);
      }
    }
  }
}
export function embedInit() {
  if (!EMBED_PARENT_ORIGIN) {
    document.body.classList.add("embed-mode", "embed-waiting");
    embedShowWaiting("embed 模式缺少 parent_origin 参数 (URL 必须形如 ?embed=1&parent_origin=https://your-portal.com)", true);
    return;
  }
  document.body.classList.add("embed-mode");
  window.addEventListener("message", embedHandleMessage);
  if (state.token) {
    enterApp();
    if (EMBED_INITIAL_TASK_ID && !_embedInitialTaskHandled) {
      _embedInitialTaskHandled = true;
      selectTask(EMBED_INITIAL_TASK_ID);
    }
  } else {
    document.body.classList.add("embed-waiting");
    embedShowWaiting("等待登录…", false);
  }
  embedPostToParent({ type: "zcbot-ready" });
}

