import{t as e}from"./userFacingCopy-BFx0qruN.js";import{t}from"./buildDefaultCollectionItem-CGDC8oOH.js";import{r as n,t as r}from"./leavePageAfterCollectSaved-lCS2NNnz.js";function i(){chrome.runtime.sendMessage({type:`OPEN_SIDE_PANEL`})}async function a(e,t){if(!t.trim())return!1;try{let n=await chrome.runtime.sendMessage({type:`ITEM_COLLECTION_HAS`,payload:{platform:e,externalId:t}});return!!(n?.ok&&n.exists)}catch{return!1}}async function o(t){let n=await chrome.runtime.sendMessage({type:`ITEM_COLLECTION_PUT`,payload:t});if(!n?.ok)throw Error(n?.error??e.pageOrganizeFailed)}async function s(e,t){await chrome.storage.local.set({[e]:{item:t,ts:Date.now()}})}async function c(e,t,n){throw t&&await s(e,t),i(),Error(n)}async function l(n){let r;try{r=await n.buildItem()}catch{return c(n.storageKey,void 0,e.saveToLibraryFailedOpenSidebar)}if(!r.ok||!r.item)return c(n.storageKey,r.item,e.saveToLibraryFailedOpenSidebar);let i=r.item,a;try{a=t(i)}catch(t){let r=t instanceof Error&&t.message===`NO_IMAGES_FOR_AUTO_SAVE`;return c(n.storageKey,i,r?e.noImagesForAutoSave:e.saveToLibraryFailedOpenSidebar)}try{return await o(a),{collected:!0,message:e.materialSaved}}catch{return c(n.storageKey,i,e.saveToLibraryFailedOpenSidebar)}}var u={goofish:{primaryBorder:`#e6c200`,primaryBg:`linear-gradient(180deg,#ffe566,#ffda00)`,primaryColor:`#422006`},pdd:{primaryBorder:`#e02e24`,primaryBg:`linear-gradient(180deg,#ff6b6b,#e02e24)`,primaryColor:`#fff`}};function d(e){e.style.padding=`8px 12px`,e.style.borderRadius=`8px`,e.style.fontSize=`12px`,e.style.fontWeight=`600`,e.style.cursor=`pointer`,e.style.boxShadow=`0 1px 4px rgba(0,0,0,.18)`,e.style.width=`100%`,e.style.whiteSpace=`nowrap`}function f(e,t){let n=u[t];e.style.border=`1px solid ${n.primaryBorder}`,e.style.background=n.primaryBg,e.style.color=n.primaryColor}function p(e){e.style.border=`1px solid #d1d5db`,e.style.background=`#fff`,e.style.color=`#374151`}async function m(t){i(),t.btn.disabled=!0;let n=t.btn.textContent;t.btn.textContent=e.organizing;try{let r=await t.buildCollectedItem();if(!r.ok||!r.item)throw Error(r.errorMessage??e.pageOrganizeFailed);await chrome.storage.local.set({[t.storageKey]:{item:r.item,ts:Date.now()}}),t.btn.textContent=e.organized,setTimeout(()=>{t.btn.textContent=n},1800)}catch{t.btn.textContent=e.organizeFailed,setTimeout(()=>{t.btn.textContent=n},1800)}finally{t.btn.disabled=!1}}async function h(t,n){let r=n.resolveExternalId?.()??``;if(!r){t.textContent=e.addToLibrary;return}let i=await a(n.resolvePlatform?.()??n.platform,r);t.textContent=i?e.inLibrary:e.addToLibrary,i?t.style.opacity=`0.92`:t.style.opacity=`1`}function g(t){if(document.getElementById(t.hostId))return;let i=t.theme??`goofish`,a=document.createElement(`div`);a.id=t.hostId,a.style.cssText=[`position:fixed`,`right:16px`,`bottom:96px`,`z-index:2147483646`,`display:flex`,`flex-direction:column`,`gap:8px`,`align-items:stretch`,`min-width:108px`].join(`;`);let o=document.createElement(`button`);o.type=`button`,o.textContent=e.addToLibrary,d(o),f(o,i);let s=document.createElement(`button`);s.type=`button`,s.textContent=e.manualOrganize,d(s),p(s);let c=document.createElement(`button`);c.type=`button`,c.textContent=e.viewMaterialLibrary,d(c),p(c),c.addEventListener(`click`,()=>n()),o.addEventListener(`click`,()=>{if(o.disabled)return;o.disabled=!0;let n=o.textContent;o.textContent=e.addingToLibrary,l({buildItem:t.buildCollectedItem,storageKey:t.storageKey}).then(i=>{if(i.collected){o.textContent=e.inLibrary,o.style.opacity=`0.92`,r();return}o.textContent=n,setTimeout(()=>{h(o,t)},3200)}).catch(()=>{o.textContent=e.organizeFailed,setTimeout(()=>{h(o,t)},2200)}).finally(()=>{o.disabled=!1})}),s.addEventListener(`click`,()=>{m({btn:s,storageKey:t.storageKey,buildCollectedItem:t.buildCollectedItem})}),a.appendChild(o),a.appendChild(c),a.appendChild(s),document.documentElement.appendChild(a),h(o,t)}export{g as t};