import"./modulepreload-polyfill-B5Qt9EMX.js";import{r as s,j as o,X as $,S as k,d as F}from"./globals-DnvsENLs.js";import"./logger-CvfM-6aa.js";import"./_commonjsHelpers-BosuxZz1.js";import"./remote-config-BEX-YVxx.js";const P="selectedPlatforms";function R(a){chrome.storage.local.set({[P]:a}).catch(()=>{})}function J(){const[a,I]=s.useState(null),[A,T]=s.useState([]),[f,i]=s.useState([]),[g,c]=s.useState("loading"),[u,p]=s.useState([]),[D,y]=s.useState(null),[M,S]=s.useState(new Map),[C,x]=s.useState(null),w=s.useRef(null);s.useEffect(()=>{w.current=C},[C]),s.useEffect(()=>{const t=l=>{var n;try{const e=typeof l.data=="string"?JSON.parse(l.data):l.data;if(e.syncId&&w.current&&e.syncId!==w.current)return;if(e.type==="INIT_DATA")I(e.article||null),T(e.platforms||[]),(n=e.selectedPlatformIds)!=null&&n.length?i(e.selectedPlatformIds):chrome.storage.local.get(P).then(r=>{const d=r[P],h=(e.platforms||[]).filter(m=>m.isAuthenticated).map(m=>m.id),v=new Set(h),G=(d==null?void 0:d.filter(m=>v.has(m)))||[];i(G)}).catch(()=>{i([])}),c("idle");else if(e.type==="SYNC_PROGRESS")e.result&&p(r=>[...r,e.result]);else if(e.type==="SYNC_DETAIL_PROGRESS"){const r=e.progress;r!=null&&r.platform&&S(d=>{const h=new Map(d);return h.set(r.platform,r),h})}else e.type==="SYNC_COMPLETE"?c("completed"):e.type==="SYNC_ERROR"&&(y(e.error),c("idle"))}catch{}};return window.addEventListener("message",t),window.parent.postMessage(JSON.stringify({type:"SYNC_DIALOG_READY"}),"*"),()=>window.removeEventListener("message",t)},[]),s.useEffect(()=>{g==="syncing"&&u.length>0&&u.length>=f.length&&c("completed")},[u.length,f.length,g]);const E=s.useCallback(t=>{window.parent.postMessage(JSON.stringify(t),"*")},[]),j=t=>{i(l=>{const n=new Set(l);n.has(t)?n.delete(t):n.add(t);const e=Array.from(n);return R(e),e})},O=()=>{const t=A.filter(l=>l.isAuthenticated).map(l=>l.id);i(t),R(t)},b=()=>{i([]),R([])},Y=()=>{if(!a||f.length===0)return;const t=`sync_${Date.now()}_${Math.random().toString(36).substr(2,9)}`;x(t),c("syncing"),p([]),y(null),S(new Map),E({type:"START_SYNC",article:a,platforms:f,syncId:t})},L=()=>{const t=u.filter(n=>!n.success).map(n=>n.platform);if(t.length===0||!a)return;const l=`sync_${Date.now()}_${Math.random().toString(36).substr(2,9)}`;x(l),c("syncing"),p(n=>n.filter(e=>e.success)),S(new Map),E({type:"START_SYNC",article:a,platforms:t,syncId:l})},N=()=>{c("idle"),p([]),y(null),S(new Map),x(null)},_=()=>{E({type:"CLOSE_SYNC_DIALOG"})};return o.jsxs("div",{className:"h-full flex flex-col bg-white rounded-xl overflow-hidden shadow-2xl",children:[o.jsxs("div",{className:"flex items-center justify-between px-4 py-3 border-b flex-shrink-0",children:[o.jsx("span",{className:"font-semibold text-gray-900",children:"文章同步"}),o.jsx("button",{onClick:_,className:"p-1 rounded hover:bg-gray-100 transition-colors",children:o.jsx($,{className:"w-4 h-4 text-gray-500"})})]}),o.jsx(k,{article:a,platforms:A,status:g,selectedPlatforms:f,results:u,platformProgress:M,error:D,onTogglePlatform:j,onSelectAll:O,onDeselectAll:b,onStartSync:Y,onRetryFailed:L,onReset:N,onCancel:N,onClose:_,className:"flex-1 min-h-0"})]})}F(document.getElementById("root")).render(o.jsx(s.StrictMode,{children:o.jsx(J,{})}));
