import{g as b,l as x}from"./chunk-3Z950eWt.js";import{m as s,r as m}from"./contentScript-D4pn_1g3.js";import"./chunk-D70STq7i.js";import"./chunk-DWfWuiPw.js";import"./chunk-bl9LjOFK.js";import"./chunk-wdxSLViu.js";import"./chunk-DZZ9VYVm.js";import"./chunk-MUVM177a.js";let d=null,p=null,y={current:0,total:0};function E(r){u(),d=setInterval(async()=>{try{const n=m.getState();await s.sendHeartbeat(r),n&&n.isRunning&&await s.updateJobProgress(r,n.currentIndex,n.completedPrompts,n.failedPrompts),P()||x.warn("[AutoExecute] Page appears unresponsive")}catch{}},3e3)}function P(){try{const r=document.body!==null,n=document.readyState!=="loading";return r&&n}catch{return!1}}function u(){d&&(clearInterval(d),d=null),p=null}async function f(){await new Promise(o=>setTimeout(o,2e3));const r=window.location.hostname,n=b(r);if(n==="unknown")return;const e=await s.getPendingJobForPlatform(n);if(e){e.jobId,e.groupId,e.prompts.length,e.originPlatform,c(`Ready to execute ${e.prompts.length} prompts from ${e.originPlatform}...`);try{if(p=e.jobId,await s.updateJobStatus(e.jobId,"running"),E(e.jobId),e.originalText)try{const t=document.querySelector('#prompt-textarea, textarea[placeholder*="Ask"], textarea[placeholder*="Message"]');t&&("value"in t?t.value=e.originalText:t.textContent=e.originalText,t.dispatchEvent(new Event("input",{bubbles:!0})))}catch(t){console.warn("[AutoExecute] Could not inject text:",t)}await new Promise(t=>setTimeout(t,3e3)),c(`Starting execution of ${e.prompts.length} prompts...`,"info");const o=e.automationMode==="slow"?"timer":e.automationMode,l=e.automationMode==="slow"?15e3:e.timerSettings.delay*1e3;await m.start(e.prompts,{executionMode:o,delayBetweenPrompts:o==="timer"?l:void 0,onProgress:async(t,i)=>{`${t}${i}`,y={current:t,total:i},await s.updateJobProgress(e.jobId,t,t,0);const a=m.getState();a&&x.debug("[AutoExecute] Executor state:",{isRunning:a.isRunning,isPaused:a.isPaused,currentIndex:a.currentIndex,completedPrompts:a.completedPrompts,failedPrompts:a.failedPrompts})},onComplete:async t=>{t.length;const i=t.filter(g=>g.status==="success").length,a=t.filter(g=>g.status==="error").length;await s.updateJobStatus(e.jobId,"completed",{currentIndex:e.prompts.length,completedPrompts:i,failedPrompts:a}),u(),c(`Completed ${i}/${t.length} prompts!`,"success")},onError:async t=>{console.error("❌ [AutoExecute] Execution error:",t),await s.updateJobStatus(e.jobId,"error",void 0,t.message),u(),c(`Execution failed: ${t.message}`,"error")}})}catch(o){console.error("❌ [AutoExecute] Failed to execute job:",o),await s.updateJobStatus(e.jobId,"error",void 0,o instanceof Error?o.message:String(o)),u(),c(`Failed to execute: ${o}`,"error")}}}function c(r,n="info"){const e={info:"#4285f4",success:"#0f9d58",error:"#db4437"},o=document.createElement("div");o.style.cssText=`
    position: fixed;
    top: 20px;
    right: 20px;
    background: ${e[n]};
    color: white;
    padding: 16px 24px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: 999999;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 14px;
    font-weight: 500;
    animation: slideIn 0.3s ease-out;
  `,o.textContent=`Agent Wielder: ${r}`;const l=document.createElement("style");l.textContent=`
    @keyframes slideIn {
      from {
        transform: translateX(400px);
        opacity: 0;
      }
      to {
        transform: translateX(0);
        opacity: 1;
      }
    }
    @keyframes slideOut {
      from {
        transform: translateX(0);
        opacity: 1;
      }
      to {
        transform: translateX(400px);
        opacity: 0;
      }
    }
  `,document.head.appendChild(l),document.body.appendChild(o),setTimeout(()=>{o.style.animation="slideOut 0.3s ease-in",setTimeout(()=>o.remove(),300)},5e3)}function $(){document.readyState==="complete"?f():window.addEventListener("load",()=>{f()}),document.addEventListener("visibilitychange",()=>{document.visibilityState==="visible"&&(p||f())}),chrome.runtime.onMessage.addListener((r,n,e)=>(r.type==="STOP_EXECUTION"&&p&&(m.stop(),u(),e({success:!0})),!0))}export{f as checkAndExecutePendingJobs,$ as setupAutoExecute};
