import{t as e}from"./batchPublishTypes-xtt5pYfv.js";import"./goofishItemUrl-DrzhgGQ-.js";import{n as t,t as n}from"./publishPrefillTypes-CYcLYgnV.js";import{a as r,c as i,i as a,n as o,o as s,r as c,s as l,t as u}from"./renderPublishPanel-wNRz3kuG.js";import{i as d}from"./goofishSearchPage-DL-11xpo.js";function f(e=window.location.href){try{let t=new URL(e);if(t.origin===`https://www.goofish.com`&&t.pathname.startsWith(`/publish`))return!0}catch{}return typeof location.hash==`string`&&location.hash.includes(`seller-item/publish`)}function p(e){return new Promise(t=>setTimeout(t,e))}async function m(){let e=await chrome.storage.local.get([n,t]),r=e[n],i=e[t],a=r?.selectedId??i?.collectionId,o=r?.batchId,s=r?.autoFill===!0,c={selectedId:a?.trim()||void 0,batchId:o?.trim()||void 0,autoFill:s,goofishPublishSurface:r?.goofishPublishSurface};return(c.selectedId||c.batchId)&&await chrome.storage.local.remove([n,t]),c}async function h(){let t=Date.now()+e;for(;Date.now()<t;){if(!f()){await p(200);continue}if(l())return await p(600),!0;await p(250)}return f()&&await i(3e3)}async function g(e,t,n,r){await chrome.runtime.sendMessage({type:`BATCH_PUBLISH_STEP_DONE`,payload:{batchId:e,itemId:t,ok:n,error:r}})}function _(e){return!!(e?.includes(`表单未就绪`)||e?.includes(`发闲置表单未就绪`))}var v=!1,y=!1;async function b(){if(!f()){c(),v=!1;return}if(!(v||y)){y=!0;try{v=!0;let{selectedId:e,batchId:t,autoFill:n,goofishPublishSurface:i}=await m(),c=!!(n&&e);if(c&&!await h()){t&&e&&await g(t,e,!1,`发布页加载超时，表单未就绪`);return}try{await o(e,{adapter:s,deferAutofill:c,goofishPublishSurface:i})}catch(n){t&&e&&await g(t,e,!1,n instanceof Error?n.message:`挂载发布面板失败`);return}if(!c)return;if(!u()){t&&e&&await g(t,e,!1,`指定的素材记录不存在或数据过大`);return}t&&r(t);try{let n=await a();!n.ok&&_(n.error)&&(await p(2e3),await h()&&(n=await a())),t&&e&&await g(t,e,n.ok,n.error)}finally{t&&r(null)}}finally{y=!1}}}function x(){if(!f()){v=!1,c(),b();return}}window.self===window.top&&(b(),d(x));