var M=Object.defineProperty;var A=(n,t,a)=>t in n?M(n,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):n[t]=a;var T=(n,t,a)=>(A(n,typeof t!="symbol"?t+"":t,a),a);import{C as f}from"./gateway.278d63bb.js";import{e as k,f as _,ej as D,E as O,g as L}from"./index.9f6b07f8.js";import{u as J}from"./polling.b2dd3650.js";import{c as N}from"./string.5d4ff2a8.js";import{F as R,H as $,J as q,S as z}from"./scripts.0edcd729.js";import"./linters.2304cd61.js";(function(){try{var n=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},t=new Error().stack;t&&(n._sentryDebugIds=n._sentryDebugIds||{},n._sentryDebugIds[t]="bec6f662-c757-4c2b-9555-9c526e4a58c5",n._sentryDebugIdIdentifier="sentry-dbid-bec6f662-c757-4c2b-9555-9c526e4a58c5")}catch{}})();class X{async createTask(t,a,r){return fetch("/_editor/api/tasks",{method:"POST",body:JSON.stringify({name:t,payload:r,stage_id:a}),headers:{"Content-Type":"application/json"}}).then(o=>o.json())}async getAllTasks(t,a){const r={offset:t.currentIndex,limit:t.pageSize,filter:{stage:a.stage,status:a.status}};return fetch("/_editor/api/tasks/list",{method:"POST",body:JSON.stringify(r),headers:{"Content-Type":"application/json"}}).then(o=>o.json())}async getStageTasks(t){return fetch(`/_editor/api/tasks/${t}`).then(a=>a.json())}async getSentStageTasks(t){return fetch(`/_editor/api/tasks/${t}/sent`).then(a=>a.json())}async updateTaskStatus(t,a){return fetch(`/_editor/api/tasks/${t}/status`,{method:"PUT",body:JSON.stringify({status:a}),headers:{"Content-Type":"application/json"}}).then(r=>r.json())}}class Y{constructor(t){T(this,"projectId");this.projectId=t}async createTask(t,a,r){return await f.post(`projects/${this.projectId}/tasks`,{name:t,stageId:a,payload:r})}async getAllTasks(t,a){const r={offset:t.currentIndex.toString(),limit:t.pageSize.toString(),stageId:a.stage,status:a.status};return Object.keys(r).forEach(g=>r[g]===void 0&&delete r[g]),await f.get(`projects/${this.projectId}/tasks`,r)}async getStageTasks(t){throw new Error("Method not implemented.")}async getSentStageTasks(t){throw new Error("Method not implemented.")}async updateTaskStatus(t,a){throw new Error("Method not implemented.")}async fetchStages(){throw new Error("Method not implemented.")}async fetchStatuses(){throw new Error("Method not implemented.")}}function F({api:n,stageId:t,fetchTasks:a}){const r=k({state:"idle"}),o=()=>{r.value={state:"idle"}},g=()=>{r.value={state:"creating",name:"My task",payloadCode:`{
  "key": "value"
}`}},y=async()=>{if(!t)throw new Error("StageId is required to create a task");const l=r.value;if(l.state!=="creating")throw new Error("Payload is required to create a task");if(!c.value)return;const i=JSON.parse(l.payloadCode);n.createTask(l.name,t,i).then(a).then(o)},c=_(()=>{const l=r.value;if(l.state!=="creating")return!1;try{const i=JSON.parse(l.payloadCode);return D.exports.isObject(i)}catch{return!1}});return{createTask:y,creatingTaskState:r,cancelCreatingTask:o,startCreatingTask:g,isPayloadValid:c}}const h=["pink","orange","green","blue","purple","red","cyan","magenta","volcano","gold"],Z=({api:n,poll:t,stageId:a,stages:r})=>{const o=O({tasks:[],sentTasks:[],colorMap:new Map,pagination:{currentIndex:0,pageSize:10,totalCount:0},filters:{stage:void 0,status:void 0,dateRange:void 0},filterOptions:{stages:[],statuses:["pending","started","completed"].map(s=>({label:N(s),value:s}))}}),g=[{name:"setToPending",label:"Set to pending",handler:()=>{o.tasks.filter(e=>e.checked).map(e=>e.id).forEach(e=>S(e))}},{name:"setToCompleted",label:"Set to completed",handler:()=>{o.tasks.filter(e=>e.checked).map(e=>e.id).forEach(e=>m(e))}},{name:"duplicate",label:"Duplicate",handler:()=>{o.tasks.filter(e=>e.checked).forEach(async e=>{await n.createTask(e.type,e.targetStageId,e.payload),c()})}}],y=k("pending"),c=async()=>{const s=a?await n.getStageTasks(a):await n.getAllTasks(o.pagination,o.filters);o.tasks=s.tasks.map(e=>({key:e.id,id:e.id,type:e.type,status:e.status,targetStageId:e.targetStageId,targetStageTitle:e.targetStageTitle,targetStageType:e.targetStageType,sourceStageTitle:e.sourceStageTitle,sourceStageType:e.sourceStageType,created:{at:new Date(e.created.at).toLocaleString(),byExecutionId:e.created.byExecutionId,byStageId:e.created.byStageId},locked:e.locked?{at:new Date(e.locked.at).toLocaleString(),byExecutionId:e.locked.byExecutionId,byStageId:e.locked.byStageId}:null,completed:e.completed?{at:new Date(e.completed.at).toLocaleString(),byExecutionId:e.completed.byExecutionId,byStageId:e.completed.byStageId}:null,payload:e.payload,checked:!1})),o.pagination.totalCount=s.totalCount},l=async()=>{if(!a)return[];const s=await n.getSentStageTasks(a);o.sentTasks=s.tasks.map(e=>({key:e.id,id:e.id,type:e.type,status:e.status,targetStageId:e.targetStageId,targetStageTitle:e.targetStageTitle,targetStageType:e.targetStageType,sourceStageTitle:e.sourceStageTitle,sourceStageType:e.sourceStageType,created:{at:new Date(e.created.at).toLocaleString(),byExecutionId:e.created.byExecutionId,byStageId:e.created.byStageId},locked:e.locked?{at:new Date(e.locked.at).toLocaleString(),byExecutionId:e.locked.byExecutionId,byStageId:e.locked.byStageId}:null,completed:e.completed?{at:new Date(e.completed.at).toLocaleString(),byExecutionId:e.completed.byExecutionId,byStageId:e.completed.byStageId}:null,payload:e.payload}))},i=()=>{var p;let s=0;o.tasks.concat((p=o.sentTasks)!=null?p:[]).forEach(u=>{o.colorMap.has(u.type)||(o.colorMap.set(u.type,h[s]),s=(s+1)%h.length)})};L(o.filters,async()=>{await Promise.all([c(),l()]),i()});const b=async()=>{await Promise.all([c(),l()]),i()},{startPolling:I,endPolling:w}=J({task:b,interval:15e3}),C=async()=>{if(await Promise.all([c(),l()]),i(),r===null){const s=Promise.all([R.list(),$.list(),q.list(),z.list()]).then(([e,p,u,d])=>[...e,...p,...u,...d]);o.filterOptions.stages=(await s).map(e=>({label:e.title,value:e.id}))}else o.filterOptions.stages=r.map(s=>({label:s.stageTitle,value:s.id}));t&&I()},E=()=>{t&&w()},S=async s=>{await n.updateTaskStatus(s,"pending"),c()},m=async s=>{await n.updateTaskStatus(s,"completed"),c()},x=s=>{o.tasks=o.tasks.map(e=>(e.id===s&&(e.checked=!e.checked),e))},v=()=>{const s=[],e=o.tasks.filter(d=>d.checked);return e.length===0?[]:(s.push("duplicate"),e.every(d=>d.status==="pending")&&s.push("setToCompleted"),e.every(d=>d.status==="completed")&&s.push("setToPending"),g.filter(d=>s.includes(d.name)))},P=()=>[{title:"Type",dataIndex:"type",key:"type",align:"center"},{title:"Payload",dataIndex:"payload",key:"payload",align:"center",width:"20%"},{title:"Status",dataIndex:"status",key:"status",align:"center",filterMode:"menu"},{title:"Stage",dataIndex:"targetStageTitle",key:"targetStageTitle",align:"center"},{title:"Created at",dataIndex:"createdAt",key:"createdAt",align:"center"},{title:"Completed at",dataIndex:"completedAt",key:"completedAt",align:"center"}],j=F({api:n,stageId:a,fetchTasks:c});return{setup:C,tearDown:E,state:o,getColumns:P,collapseValue:y,setToPending:S,setToCompleted:m,fetchTasks:c,fetchSentTasks:l,checkTask:x,getPossibleActions:v,...j}};export{X as L,Y as R,Z as T};
//# sourceMappingURL=tasksController.8cf447bf.js.map
