import"./goofishItemUrl-DrzhgGQ-.js";import"./modelConfig-CSZMXwN9.js";import{a as e}from"./humanInteraction-8U58N0CI.js";import{c as t,l as n,s as r}from"./shopCollectTypes-D5sWEXiQ.js";var i=`a[class*="feeds-item-wrap--"][href*="item?id="]`,a=`[class*="feeds-list-container--"]`,o=`J_SubAppContainer`,s=[`#content`,`[class*="content-container--"]`,`#ice-container`];function c(e=window.location.href){try{let t=new URL(e,window.location.href);return t.origin===`https://www.goofish.com`?t.pathname===`/personal`||t.pathname.startsWith(`/personal/`)||document.body?.getAttribute(`data-spm`)===`personal`?!0:!!document.querySelector(a):!1}catch{return!1}}function l(e){try{let t=new URL(e,window.location.href),n=t.searchParams.get(`id`)??t.searchParams.get(`itemId`)??t.searchParams.get(`item_id`);if(n?.trim())return n.trim();let r=t.pathname.match(/\/item\/([^/?#]+)/i);if(r?.[1]){let e=r[1].replace(/\.html$/i,``).trim();if(e&&!/^item$/i.test(e))return e}}catch{}return null}function u(){let e=new Set,t=[],n=document.querySelectorAll(i);for(let r of n){if(!(r instanceof HTMLAnchorElement))continue;let n=l(r.href);!n||e.has(n)||(e.add(n),t.push(n))}return t}function d(){let e=[],t=t=>{t instanceof HTMLElement&&!e.includes(t)&&e.push(t)};t(document.getElementById(o));for(let e of s)t(document.querySelector(e));let n=document.querySelector(a),r=n instanceof HTMLElement?n:null;for(;r;)t(r),r=r.parentElement;return t(document.documentElement),t(document.body),e}function f(e){if(e===document.documentElement||e===document.body)return document.documentElement.scrollHeight>window.innerHeight+8;let t=window.getComputedStyle(e).overflowY;return(t===`auto`||t===`scroll`||t===`overlay`)&&e.scrollHeight>e.clientHeight+8}function p(){let e=d().filter(f);return e.length>0?e:[document.documentElement]}function m(){let e=document.querySelector(a);if(e instanceof HTMLElement)return e;let t=document.querySelectorAll(i),n=t[t.length-1];return n instanceof HTMLElement?n:document.body}function h(e){let t=e.getBoundingClientRect();return{clientX:Math.min(Math.max(t.left+t.width*.5,8),window.innerWidth-8),clientY:Math.min(Math.max(t.top+t.height*.65,8),window.innerHeight-8)}}function g(e,t){let{clientX:n,clientY:r}=h(e),i={bubbles:!0,cancelable:!0,composed:!0,deltaY:t,deltaMode:WheelEvent.DOM_DELTA_PIXEL,clientX:n,clientY:r,view:window},a=[e];for(let e of p())a.push(e);a.push(document,window);for(let e of a)e.dispatchEvent(new WheelEvent(`wheel`,i));window.scrollBy({top:t,left:0,behavior:`auto`});for(let e of p())e===document.documentElement?(document.documentElement.scrollTop+=t,document.body.scrollTop+=t):e.scrollTop+=t}function _(){let e=document.querySelectorAll(i),t=e[e.length-1];t instanceof HTMLElement&&t.scrollIntoView({block:`end`,inline:`nearest`,behavior:`auto`})}function v(){_();let e=m(),{clientX:t,clientY:n}=h(e);e.dispatchEvent(new MouseEvent(`mousemove`,{bubbles:!0,cancelable:!0,clientX:t,clientY:n,view:window}));let r=4+Math.floor(Math.random()*3);for(let t=0;t<r;t++)g(e,260+Math.random()*180);for(let e of p())if(e===document.documentElement){let e=document.documentElement.scrollHeight-window.innerHeight;document.documentElement.scrollTop=Math.min(document.documentElement.scrollTop+Math.max(360,window.innerHeight*.75),e),document.body.scrollTop=document.documentElement.scrollTop}else e.scrollTop=Math.min(e.scrollTop+Math.max(360,e.clientHeight*.75),e.scrollHeight)}async function y(e,t,n){let r=e.maxIdleRounds??3,i=0,a=0;for(;i<r&&!e.shouldAbort();){for(;e.shouldPause()&&!e.shouldAbort();)e.onProgress?.(t(),`已暂停`),await e.delay(400,600);if(e.shouldAbort())break;let r=t();if(e.onProgress?.(r,n(r)),r<=a?i+=1:(i=0,a=r),v(),!await e.delay(e.minDelayMs,e.maxDelayMs))break}}async function b(e,t){let n=new Set(e);return await y(t,()=>u().filter(e=>!n.has(e)).length,e=>`滚动加载更多… 新发现 ${e} 件（共 ${n.size+e} 件）`),u().filter(e=>!n.has(e))}var x=`xyxp-shop-collect-hud`,S=`xyxp-shop-collect-hud-style`;function C(){if(document.getElementById(S))return;let e=document.createElement(`style`);e.id=S,e.textContent=`
#${x} {
  position: fixed;
  right: 14px;
  bottom: 52px;
  z-index: 2147483000;
  width: 280px;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid rgba(0,0,0,0.08);
  background: #fffef5;
  box-shadow: 0 8px 28px rgba(0,0,0,0.12);
  font-size: 12px;
  color: #303133;
  line-height: 1.5;
}
#${x} .xyxp-shop-hud-title {
  font-weight: 700;
  margin-bottom: 6px;
}
#${x} .xyxp-shop-hud-risk {
  color: #e6a23c;
  font-size: 11px;
  margin-bottom: 8px;
}
#${x} .xyxp-shop-hud-stats {
  margin-bottom: 8px;
  color: #606266;
}
#${x} .xyxp-shop-hud-msg {
  min-height: 18px;
  margin-bottom: 10px;
  color: #909399;
  word-break: break-all;
}
#${x} .xyxp-shop-hud-countdown {
  color: #e6a23c;
  font-weight: 600;
  margin-top: 4px;
}
#${x} .xyxp-shop-hud-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
#${x} button {
  border: none;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
}
#${x} .xyxp-shop-hud-primary {
  background: #ffda00;
  color: #303133;
  font-weight: 600;
}
#${x} .xyxp-shop-hud-secondary {
  background: #f4f4f5;
  color: #303133;
}
#${x} .xyxp-shop-hud-danger {
  background: #fef0f0;
  color: #f56c6c;
}
`,document.documentElement.appendChild(e)}function w(e){if(!e||e.status===`idle`)return`尚未开始`;let t=e.itemIds.length;return`进度 ${t>0?`${Math.min(e.currentIndex,t)}/${t}`:`—`} · 新增 ${e.collected} · 跳过 ${e.skipped} · 失败 ${e.failed}`}function T(e){return e?.message?e.message:`先采集首屏宝贝，采完一批后再滚动加载更多（跳过已存在）。`}function E(e,t=Date.now()){if(!e?.waitUntil||e.waitUntil<=t)return``;let n=Math.ceil((e.waitUntil-t)/1e3);return e.detailTabId==null?e.message.includes(`休息`)?`休息倒计时 ${n}s`:`下一件倒计时 ${n}s`:`详情等待倒计时 ${n}s`}function D(e){let t=e?.status??`idle`;return t===`idle`||t===`done`||t===`cancelled`||t===`error`?`<button type="button" class="xyxp-shop-hud-primary" data-action="start">开始店铺采集</button>`:t===`discovering`||t===`running`?`
      <button type="button" class="xyxp-shop-hud-secondary" data-action="pause">暂停</button>
      <button type="button" class="xyxp-shop-hud-danger" data-action="stop">停止</button>
    `:t===`paused`?`
      <button type="button" class="xyxp-shop-hud-primary" data-action="resume">继续</button>
      <button type="button" class="xyxp-shop-hud-danger" data-action="stop">停止</button>
    `:``}function O(e,t=null){C();let r=document.getElementById(x);r||(r=document.createElement(`div`),r.id=x,document.documentElement.appendChild(r));let i=t,a=null;function o(t){if(!r)return;i=t;let n=Date.now(),s=E(t,n);r.innerHTML=`
      <div class="xyxp-shop-hud-title">店铺批量采集</div>
      <div class="xyxp-shop-hud-risk">自动化存在账号风险，请低频使用。每件间隔 5 秒，每采集 5 件休息 20~30 秒。</div>
      <div class="xyxp-shop-hud-stats">${w(t)}</div>
      <div class="xyxp-shop-hud-msg">${T(t)}${s?`<div class="xyxp-shop-hud-countdown">${s}</div>`:``}</div>
      <div class="xyxp-shop-hud-actions">${D(t)}</div>
    `,r.querySelector(`[data-action="start"]`)?.addEventListener(`click`,()=>void e.onStart()),r.querySelector(`[data-action="pause"]`)?.addEventListener(`click`,()=>void e.onPause()),r.querySelector(`[data-action="resume"]`)?.addEventListener(`click`,()=>void e.onResume()),r.querySelector(`[data-action="stop"]`)?.addEventListener(`click`,()=>void e.onStop()),t?.waitUntil&&t.waitUntil>n?a??=window.setInterval(()=>o(i),1e3):a!=null&&(window.clearInterval(a),a=null)}o(t);let s=(e,t)=>{if(t!==`local`)return;let r=e[n];r&&o(r.newValue??null)};return chrome.storage.onChanged.addListener(s),()=>{a!=null&&(window.clearInterval(a),a=null),chrome.storage.onChanged.removeListener(s),r?.remove(),document.getElementById(S)?.remove()}}async function k(){let e=await chrome.runtime.sendMessage({type:`GET_SHOP_COLLECT_STATUS`});return e?.ok?e.state??null:null}var A=!1,j=!1,M=!1,N=!1,P=null,F=null,I=!1,L=!1;async function R(n){return b(n,{shouldAbort:()=>A,shouldPause:()=>j,onProgress:(e,t)=>{H({type:`SHOP_COLLECT_SET_DISCOVERING`,payload:{message:t}})},delay:(t,n)=>e(t,n,()=>A,120),minDelayMs:t,maxDelayMs:r})}function z(){L||(L=!0,chrome.runtime.onMessage.addListener((e,t,n)=>e?.type===`SHOP_COLLECT_SCROLL_DISCOVER_MORE`?(R(e.payload?.knownIds??[]).then(e=>{n({ok:!0,newIds:e})}),!0):!1))}function B(){P?.(),P=null,M=!1}function V(){F||M||!document.body||(F=new MutationObserver(()=>{c()&&!M&&!N&&X()}),F.observe(document.body,{childList:!0,subtree:!0}))}async function H(e){return await chrome.runtime.sendMessage(e)}async function U(){try{let e=await H({type:`CHECK_SHOP_COLLECTION_ENABLED`});return e?.ok===!0&&e.enabled===!0}catch{return!1}}async function W(){A=!1,j=!1,await H({type:`SHOP_COLLECT_SET_DISCOVERING`,payload:{message:`正在识别首屏商品…`}});let e=u();if(A)return;if(!e.length){await H({type:`SHOP_COLLECT_FAIL`,payload:{message:`首屏未发现可采集商品，请稍候重试`}});return}let t=await H({type:`START_SHOP_COLLECT`,payload:{itemIds:e}});t?.ok||await H({type:`SHOP_COLLECT_FAIL`,payload:{message:t?.error??`启动采集失败`}})}async function G(){j=!0,await H({type:`PAUSE_SHOP_COLLECT`})}async function K(){j=!1,await H({type:`RESUME_SHOP_COLLECT`})}async function q(){A=!0,j=!1,await H({type:`STOP_SHOP_COLLECT`})}function J(){J.installed||(J.installed=!0,chrome.storage.onChanged.addListener((e,t)=>{if(t!==`local`||!e.xyxp_shop_collect)return;let r=e[n];if(r.newValue===void 0){A=!0,j=!1;return}let i=r.newValue;j=i?.status===`paused`,i?.status===`cancelled`&&(A=!0)}))}function Y(){I||(I=!0,chrome.storage.onChanged.addListener((e,t)=>{t!==`local`||!e.xyxp_model_config||c()&&(B(),Z())}))}async function X(){if(window.self===window.top){if(!c()){B(),F?.disconnect(),F=null;return}if(!(M||N)){N=!0;try{if(!await U())return;J(),Y(),z();let e=null;try{e=await k()}catch{}j=e?.status===`paused`,P=O({onStart:W,onPause:G,onResume:K,onStop:q},e),M=!0,F?.disconnect(),F=null}catch{}finally{N=!1}}}}function Z(){if(!c()){B(),F?.disconnect(),F=null;return}V(),Y(),X(),window.setTimeout(()=>void X(),800),window.setTimeout(()=>void X(),2500),window.setTimeout(()=>void X(),5e3)}export{Z as t};