import{l as a,g as T}from"./chunk-3Z950eWt.js";import{m as g,r as w}from"./contentScript-D4pn_1g3.js";import{d as y}from"./chunk-DZZ9VYVm.js";import"./chunk-D70STq7i.js";import"./chunk-DWfWuiPw.js";import"./chunk-bl9LjOFK.js";import"./chunk-wdxSLViu.js";import"./chunk-MUVM177a.js";let h="unknown",C=!1;function v(){return C?{canStart:!1,reason:"Already executing a cross-tab job"}:w.getState().isRunning?{canStart:!1,reason:"Executor is already running"}:{canStart:!0}}function b(){C=!1}function $(t){y("coral:cross-tab-execution-start",{prompts:t,promptCount:t.length,executionMode:"auto",isRemoteJob:!0})}function P(t,r){y("coral:execution:progress",{current:t,total:r})}function R(t){y("coral:execution:complete",{results:t})}function m(t,r="info"){const o={info:"#4285f4",success:"#0f9d58",error:"#db4437"},l=document.getElementById("coral-cross-tab-notification");l&&l.remove();const n=document.createElement("div");n.id="coral-cross-tab-notification",n.style.cssText=`
    position: fixed;
    top: 20px;
    right: 20px;
    background: ${o[r]};
    color: white;
    padding: 16px 24px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.25);
    z-index: 2147483647;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 14px;
    font-weight: 500;
    animation: coralSlideIn 0.3s ease-out;
    display: flex;
    align-items: center;
    gap: 12px;
  `;const c=document.createElement("span");c.style.fontSize="20px",c.textContent=r==="success"?"✅":r==="error"?"❌":"🚀";const d=document.createElement("span");if(d.textContent=t,n.append(c,d),!document.getElementById("coral-notification-styles")){const i=document.createElement("style");i.id="coral-notification-styles",i.textContent=`
      @keyframes coralSlideIn {
        from {
          transform: translateX(400px);
          opacity: 0;
        }
        to {
          transform: translateX(0);
          opacity: 1;
        }
      }
      @keyframes coralSlideOut {
        from {
          transform: translateX(0);
          opacity: 1;
        }
        to {
          transform: translateX(400px);
          opacity: 0;
        }
      }
    `,document.head.appendChild(i)}document.body.appendChild(n),setTimeout(()=>{n.style.animation="coralSlideOut 0.3s ease-in",setTimeout(()=>n.remove(),300)},5e3)}function k(t,r){let o=document.getElementById("coral-progress-bar");o||(o=document.createElement("div"),o.id="coral-progress-bar",o.style.cssText=`
      position: fixed;
      bottom: 20px;
      right: 20px;
      background: rgba(0, 0, 0, 0.85);
      color: white;
      padding: 16px 24px;
      border-radius: 12px;
      box-shadow: 0 4px 20px rgba(0,0,0,0.3);
      z-index: 2147483647;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
      font-size: 14px;
      min-width: 200px;
    `,document.body.appendChild(o));const l=Math.round(t/r*100);o.replaceChildren();const n=document.createElement("div");n.style.cssText="display: flex; justify-content: space-between; margin-bottom: 8px;";const c=document.createElement("span");c.textContent="🚀 Agent Wielder Running";const d=document.createElement("span");d.textContent=`${t}/${r}`,n.append(c,d);const i=document.createElement("div");i.style.cssText="background: rgba(255,255,255,0.2); border-radius: 4px; height: 6px; overflow: hidden;";const p=document.createElement("div");p.style.cssText=`background: #4285f4; height: 100%; width: ${l}%; transition: width 0.3s ease;`,i.appendChild(p),o.append(n,i)}function x(){const t=document.getElementById("coral-progress-bar");t&&t.remove()}async function z(t,r,o,l,n){a.info(`[CrossTabHandler] 🎯 Received execution request for job ${t}`),a.info(`[CrossTabHandler] Prompts: ${r.length}, Mode: ${o}`);const c=v();if(!c.canStart){a.warn(`[CrossTabHandler] ⚠️ Cannot start execution: ${c.reason}`),m(`Cannot start: ${c.reason}`,"error"),chrome.runtime.sendMessage({type:"EXECUTION_ERROR",jobId:t,error:c.reason}).catch(()=>{});return}if(C=!0,h=T(window.location.hostname),h!=="unknown")try{await g.registerReceivedJob(t,r,h,o,l,n),a.info("[CrossTabHandler] Registered job with multiJobManager")}catch(e){a.warn("[CrossTabHandler] Could not register with multiJobManager:",e)}if($(r),m(`Starting ${r.length} prompts...`,"info"),await new Promise(e=>setTimeout(e,1500)),n)try{const e=document.querySelector('#prompt-textarea, textarea[placeholder*="Ask"], textarea[placeholder*="Message"], [contenteditable="true"]');e&&("value"in e?e.value=n:e.isContentEditable&&(e.textContent=n),e.dispatchEvent(new Event("input",{bubbles:!0})),a.info("[CrossTabHandler] Injected original text"))}catch(e){a.warn("[CrossTabHandler] Could not inject text:",e)}await new Promise(e=>setTimeout(e,1500));const d=o==="slow"?"timer":o,i=o==="slow"?15e3:o==="timer"?l.delay*1e3:void 0,p=async(e,s)=>{chrome.runtime.sendMessage({type:"EXECUTION_PROGRESS",data:{jobId:t,currentIndex:e,totalPrompts:s}}).catch(()=>{}),k(e,s),P(e,s);try{await g.updateJobProgress(t,e,e,0)}catch{}},S=async e=>{chrome.runtime.sendMessage({type:"EXECUTION_COMPLETE",data:{jobId:t,results:e}}).catch(()=>{}),R(e),x();const s=e.filter(f=>f.status==="success").length,u=e.filter(f=>f.status==="error").length;try{await g.updateJobStatus(t,"completed",{currentIndex:e.length,completedPrompts:s,failedPrompts:u})}catch{}},E=async e=>{chrome.runtime.sendMessage({type:"EXECUTION_ERROR",jobId:t,error:e.message}).catch(()=>{});try{await g.updateJobStatus(t,"error",void 0,e.message)}catch{}};try{a.info(`[CrossTabHandler] ▶️ Starting reliableExecutor with ${r.length} prompts`),await w.start(r,{executionMode:d,delayBetweenPrompts:i,onProgress:(e,s)=>{a.debug(`[CrossTabHandler] Progress: ${e}/${s}`),p(e,s)},onComplete:e=>{const s=e.filter(u=>u.status==="success").length;a.info(`[CrossTabHandler] ✅ Completed: ${s}/${e.length} succeeded`),x(),m(`Completed ${s}/${e.length} prompts!`,"success"),S(e),b()},onError:e=>{a.error("[CrossTabHandler] ❌ Execution error:",e),x(),m(`Error: ${e.message}`,"error"),E(e),b()}}),a.info("[CrossTabHandler] Execution started successfully")}catch(e){throw a.error("[CrossTabHandler] Failed to start execution:",e),x(),m(`Failed to start: ${e instanceof Error?e.message:"Unknown error"}`,"error"),E(e instanceof Error?e:new Error(String(e))),b(),e}}export{z as executeCrossTabJob};
