var D=Object.defineProperty;var M=(n,a,t)=>a in n?D(n,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[a]=t;var T=(n,a,t)=>(M(n,typeof a!="symbol"?a+"":a,t),t);import{C as h}from"./gateway.973a0055.js";import{e as b,f as A,ej as _,E as R,g as O}from"./index.b466b20d.js";import{F as J,H as N,J as $,S as q}from"./scripts.67bab85e.js";import"./linters.a9259de0.js";import{u as z}from"./polling.f4d808f9.js";import{c as F}from"./string.42378387.js";(function(){try{var n=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},a=new Error().stack;a&&(n._sentryDebugIds=n._sentryDebugIds||{},n._sentryDebugIds[a]="b5b16e22-144c-48ac-b1e1-c3631c095931",n._sentryDebugIdIdentifier="sentry-dbid-b5b16e22-144c-48ac-b1e1-c3631c095931")}catch{}})();class X{async createTask(a,t,r){return fetch("/_editor/api/tasks",{method:"POST",body:JSON.stringify({name:a,payload:r,stage_id:t}),headers:{"Content-Type":"application/json"}}).then(o=>o.json())}async getAllTasks(a,t){const r={offset:a.currentIndex,limit:a.pageSize,filter:{stage:t.stage,status:t.status,startDate:t.dateRange?t.dateRange[0]:void 0,endDate:t.dateRange?t.dateRange[1]:void 0}};return fetch("/_editor/api/tasks/list",{method:"POST",body:JSON.stringify(r),headers:{"Content-Type":"application/json"}}).then(o=>o.json())}async getStageTasks(a){return fetch(`/_editor/api/tasks/${a}`).then(t=>t.json())}async getSentStageTasks(a){return fetch(`/_editor/api/tasks/${a}/sent`).then(t=>t.json())}async updateTaskStatus(a,t){return fetch(`/_editor/api/tasks/${a}/status`,{method:"PUT",body:JSON.stringify({status:t}),headers:{"Content-Type":"application/json"}}).then(r=>r.json())}}class Y{constructor(a){T(this,"projectId");this.projectId=a}async createTask(a,t,r){return await h.post(`projects/${this.projectId}/tasks`,{name:a,stageId:t,payload:r})}async getAllTasks(a,t){const r={offset:a.currentIndex.toString(),limit:a.pageSize.toString(),stageId:t.stage,status:t.status,startDate:t.dateRange?t.dateRange[0]:void 0,endDate:t.dateRange?t.dateRange[1]:void 0};return Object.keys(r).forEach(g=>r[g]===void 0&&delete r[g]),await h.get(`projects/${this.projectId}/tasks`,r)}async getStageTasks(a){throw new Error("Method not implemented.")}async getSentStageTasks(a){throw new Error("Method not implemented.")}async updateTaskStatus(a,t){throw new Error("Method not implemented.")}async fetchStages(){throw new Error("Method not implemented.")}async fetchStatuses(){throw new Error("Method not implemented.")}}function L({api:n,stageId:a,fetchTasks:t}){const r=b({state:"idle"}),o=()=>{r.value={state:"idle"}},g=()=>{r.value={state:"creating",name:"My task",payloadCode:`{
  "key": "value"
}`}},y=async()=>{if(!a)throw new Error("StageId is required to create a task");const c=r.value;if(c.state!=="creating")throw new Error("Payload is required to create a task");if(!d.value)return;const i=JSON.parse(c.payloadCode);n.createTask(c.name,a,i).then(t).then(o)},d=A(()=>{const c=r.value;if(c.state!=="creating")return!1;try{const i=JSON.parse(c.payloadCode);return _.exports.isObject(i)}catch{return!1}});return{createTask:y,creatingTaskState:r,cancelCreatingTask:o,startCreatingTask:g,isPayloadValid:d}}const f=["pink","orange","green","blue","purple","red","cyan","magenta","volcano","gold"],Z=({api:n,poll:a,stageId:t,stages:r})=>{const o=R({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:F(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),d()})}}],y=b(["pending"]),d=async()=>{const s=t?await n.getStageTasks(t):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).toString(),byExecutionId:e.created.byExecutionId,byStageId:e.created.byStageId},locked:e.locked?{at:new Date(e.locked.at).toString(),byExecutionId:e.locked.byExecutionId,byStageId:e.locked.byStageId}:null,completed:e.completed?{at:new Date(e.completed.at).toString(),byExecutionId:e.completed.byExecutionId,byStageId:e.completed.byStageId}:null,payload:e.payload,checked:!1})),o.pagination.totalCount=s.totalCount},c=async()=>{if(!t)return[];const s=await n.getSentStageTasks(t);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).toString(),byExecutionId:e.created.byExecutionId,byStageId:e.created.byStageId},locked:e.locked?{at:new Date(e.locked.at).toString(),byExecutionId:e.locked.byExecutionId,byStageId:e.locked.byStageId}:null,completed:e.completed?{at:new Date(e.completed.at).toString(),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,f[s]),s=(s+1)%f.length)})};O(o.filters,async()=>{await Promise.all([d(),c()]),i()});const k=async()=>{await Promise.all([d(),c()]),i()},{startPolling:I,endPolling:w}=z({task:k,interval:15e3}),C=async()=>{if(await Promise.all([d(),c()]),i(),r===null){const s=Promise.all([J.list(),N.list(),$.list(),q.list()]).then(([e,p,u,l])=>[...e,...p,...u,...l]);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}));a&&I()},E=()=>{a&&w()},S=async s=>{await n.updateTaskStatus(s,"pending"),d()},m=async s=>{await n.updateTaskStatus(s,"completed"),d()},v=s=>{o.tasks=o.tasks.map(e=>(e.id===s&&(e.checked=!e.checked),e))},x=()=>{const s=[],e=o.tasks.filter(l=>l.checked);return e.length===0?[]:(s.push("duplicate"),e.every(l=>l.status==="pending")&&s.push("setToCompleted"),e.every(l=>l.status==="completed")&&s.push("setToPending"),g.filter(l=>s.includes(l.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=L({api:n,stageId:t,fetchTasks:d});return{setup:C,tearDown:E,state:o,getColumns:P,collapseValue:y,setToPending:S,setToCompleted:m,fetchTasks:d,fetchSentTasks:c,checkTask:v,getPossibleActions:x,...j}};export{X as L,Y as R,Z as T};
//# sourceMappingURL=tasksController.a7296231.js.map
