(()=>{"use strict";
  // Pulse operator info sniffer (email/login). Runs in MAIN world.
  if(window.__PULSE_OPERATOR_SNIFFER__) return;
  window.__PULSE_OPERATOR_SNIFFER__=true;

  const ORIGIN = location.origin;

  function post(info){
    try{
      window.postMessage({src:"PULSE_PAGE",type:"PULSE_OPERATOR_INFO",app:"PULSE",...info}, ORIGIN);
    }catch{}
  }

  function getToken(){
    try{
      const raw = localStorage.getItem("token");
      if(!raw) return null;
      const t = String(raw).trim();
      if(!t) return null;
      return t.startsWith("Bearer ") ? t.slice(7).trim() : t;
    }catch{ return null; }
  }

  function parseJwtPayload(jwt){
    const parts = String(jwt||"").split(".");
    if(parts.length<2) return null;
    const p = parts[1].replace(/-/g,"+").replace(/_/g,"/");
    const b64 = p.padEnd(Math.ceil(p.length/4)*4,"=");
    try{ return JSON.parse(atob(b64)); }catch{ return null; }
  }

  function pickFromPayload(payload){
    if(!payload || typeof payload!=="object") return {};
    const email = payload.email ?? payload.operator_email ?? payload.user_email ?? null;
    const login = payload.login ?? payload.username ?? payload.user_login ?? payload.operator_login ?? null;
    const out = {};
    if(email) out.email = email;
    if(login) out.login = login;
    return out;
  }

  function deepFind(obj){
    const hits = [];
    const seen = new WeakSet();
    const keys = ["login","username","user_login","operator_login","email","operator_email","user_email","operatorEmail","userEmail"];
    function walk(x, path){
      if(!x || typeof x!=="object") return;
      if(seen.has(x)) return;
      seen.add(x);
      for(const k of keys){
        if(x[k]!=null && String(x[k]).trim()!==""){
          hits.push({key:k, value:x[k], path: path?`${path}.${k}`:k});
        }
      }
      for(const [k,v] of Object.entries(x)){
        if(v && typeof v==="object") walk(v, path?`${path}.${k}`:k);
      }
    }
    walk(obj,"");
    return hits;
  }

  function emitFromHits(hits){
    if(!hits || !hits.length) return;
    const emailHit = hits.find(h=>String(h.key).toLowerCase().includes("email"));
    const loginHit = hits.find(h=>String(h.key).toLowerCase().includes("login") || String(h.key).toLowerCase().includes("username"));
    const info = {};
    if(emailHit) info.email = emailHit.value;
    if(loginHit) info.login = loginHit.value;
    if(info.email || info.login) post(info);
  }

  // 1) Fast: try from JWT payload
  try{
    const token = getToken();
    const payload = token ? parseJwtPayload(token) : null;
    const info = pickFromPayload(payload);
    if(info.email || info.login) post(info);
  }catch{}

  // 2) Fetch sniffer (MAIN world)
  try{
    const origFetch = window.fetch;
    window.fetch = async function(...args){
      const res = await origFetch.apply(this,args);
      try{
        const ct = res && res.headers ? (res.headers.get("content-type")||"") : "";
        if(ct.includes("application/json")){
          const clone = res.clone();
          const data = await clone.json().catch(()=>null);
          if(data && typeof data==="object"){
            const hits = deepFind(data);
            emitFromHits(hits);
          }
        }
      }catch{}
      return res;
    };
  }catch{}
})();