import"./main2.js";function r(t){return t==null?"":t.toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#039;")}function i(t,e="info"){const n=document.getElementById("info-panel"),a=document.getElementById("info-message");!n||!a||(a.innerHTML="",a.appendChild(document.createTextNode(t)),n.className=`ant-alert ant-alert-${e} ant-alert-with-description ant-alert-closable`,n.style.display="flex")}function p(){const t=document.getElementById("info-panel");t&&(t.style.display="none")}function y(t,e){window.location.href=`/playbook/${encodeURIComponent(t)}/${encodeURIComponent(e)}`}async function d(t){i(`Executing playbook "${t.path}"...`,"info");try{const e=await fetch("/agent/execute",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});if(!e.ok){const s=await e.json().catch(()=>({detail:"Unknown server error."}));throw new Error(s.detail||`HTTP error! Status: ${e.status}`)}const n=await e.json();i(`Execution started. ID: ${n.execution_id}`,"success");const a=document.getElementById("info-message"),o=document.createElement("a");o.href=`/execution/${n.execution_id}`,o.textContent=" View execution details",o.style.color="#1890ff",o.style.textDecoration="underline",a.appendChild(o)}catch(e){console.error("Error executing playbook:",e),i(`Error: ${e.message}`,"error")}}async function m(){const t=document.getElementById("playbook-list");t.innerHTML=`
        <tr>
            <td colspan="4" style="text-align: center; padding: 24px;">
                <div class="ant-spin ant-spin-spinning"></div>
                <p style="margin-top: 8px;">Loading playbooks...</p>
            </td>
        </tr>`;try{const e=await fetch("/catalog/list");if(!e.ok)throw new Error("Network response was not ok");const n=await e.json();h(n.entries||[])}catch(e){console.error("Error loading playbooks:",e),t.innerHTML=`
            <tr class="ant-table-row">
                <td colspan="4" style="text-align: center; color: #f5222d; padding: 24px;">
                    Error loading playbooks. Please check the console and try again later.
                </td>
            </tr>`}}function h(t){const e=document.getElementById("playbook-list");if(e.innerHTML="",t.length===0){const n=e.insertRow();n.className="ant-table-row";const a=n.insertCell();a.colSpan=4,a.style.textAlign="center",a.style.padding="24px",a.innerHTML='No playbooks found. <a href="/editor/new">Create a new playbook</a>';return}t.forEach(n=>{const a=e.insertRow();a.className="ant-table-row",a.dataset.path=n.resource_path,a.dataset.version=n.resource_version,a.innerHTML=`
            <td class="ant-table-cell">${r(n.resource_name||"Unnamed")}</td>
            <td class="ant-table-cell">${r(n.resource_path)}</td>
            <td class="ant-table-cell">${r(n.resource_version)}</td>
            <td class="ant-table-cell action-buttons">
                <button class="ant-btn ant-btn-sm ant-btn-primary" data-action="view">View</button>
                <a href="/editor/${encodeURIComponent(n.resource_path)}/${encodeURIComponent(n.resource_version)}"
                   class="ant-btn ant-btn-sm ant-btn-primary" data-action="edit">Edit</a>
                <button class="ant-btn ant-btn-sm ant-btn-success" data-action="execute">Execute</button>
                <button class="ant-btn ant-btn-sm ant-btn-warning" data-action="payload">Payload</button>
            </td>
        `})}const c={element:null,path:null,version:null,init(){if(this.element=document.getElementById("payload-modal"),!this.element){console.error("Payload modal element not found in the DOM.");return}this.element.querySelector("#modal-cancel").addEventListener("click",()=>this.hide()),this.element.querySelector("#modal-close").addEventListener("click",()=>this.hide()),this.element.querySelector("#execute-with-payload").addEventListener("click",()=>this.execute());const t=this.element.querySelector("#payload-file"),e=this.element.querySelector("#file-name-display");t.addEventListener("change",()=>{e.textContent=t.files.length>0?`Selected: ${t.files[0].name}`:""}),this.setupTabs()},setupTabs(){const t=this.element.querySelector("#payloadTabs");t.addEventListener("click",e=>{const n=e.target.closest(".ant-tabs-tab-btn");if(!n)return;const a=n.dataset.tabId;t.querySelectorAll(".ant-tabs-tab").forEach(o=>o.classList.remove("ant-tabs-tab-active")),t.querySelectorAll(".ant-tabs-tabpane").forEach(o=>o.style.display="none"),n.closest(".ant-tabs-tab").classList.add("ant-tabs-tab-active"),document.getElementById(a).style.display="block"})},show(t,e){this.element&&(this.path=t,this.version=e,this.element.querySelector("#payload-json").value="",this.element.querySelector("#payload-file").value="",this.element.querySelector("#file-name-display").textContent="",this.element.querySelector("#merge-payload").checked=!1,this.element.style.display="block")},hide(){this.element&&(this.element.style.display="none")},async execute(){const t=this.element.querySelector("#merge-payload").checked;let e=null;const n=this.element.querySelector(".ant-tabs-tab-active .ant-tabs-tab-btn"),a=n.dataset.tabId==="json-content",o=n.dataset.tabId==="file-upload";try{if(a){const l=this.element.querySelector("#payload-json").value.trim();l&&(e=JSON.parse(l))}else if(o){const l=this.element.querySelector("#payload-file");if(l.files.length>0){const u=await l.files[0].text();e=JSON.parse(u)}}}catch{i("Invalid JSON format. Please check your input.","error");return}const s={path:this.path,version:this.version,sync_to_postgres:!0,merge:t};e&&(s.input_payload=e),this.hide(),d(s)}};document.addEventListener("DOMContentLoaded",function(){m(),c.init(),document.querySelector("#info-panel .ant-alert-close-icon")?.addEventListener("click",p),document.getElementById("playbook-list").addEventListener("click",t=>{const e=t.target.closest("button[data-action]");if(!e)return;const n=e.closest("tr"),a=n.dataset.path,o=n.dataset.version,s=e.dataset.action;s==="view"?y(a,o):s==="execute"?d({path:a,version:o,sync_to_postgres:!0}):s==="payload"&&c.show(a,o)})});
