var D=Object.defineProperty;var M=(o,a,t)=>a in o?D(o,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[a]=t;var T=(o,a,t)=>(M(o,typeof a!="symbol"?a+"":a,t),t);import{j as h}from"./router.10a36819.js";import{r as k,c as _,ek as R,ae as O,w as J}from"./jwt-decode.esm.c1356de1.js";import"./linters.e9f2fa79.js";import{F as z,H as N,J as $,S as F}from"./scripts.fbf94024.js";import{u as L}from"./polling.b00cc5ce.js";import{c as q}from"./string.dbe1a2f4.js";(function(){try{var o=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},a=new Error().stack;a&&(o._sentryDebugIds=o._sentryDebugIds||{},o._sentryDebugIds[a]="9e05004e-7b65-4088-b9a4-6850fcfaa20e",o._sentryDebugIdIdentifier="sentry-dbid-9e05004e-7b65-4088-b9a4-6850fcfaa20e")}catch{}})();class Y{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(n=>n.json())}async getAllTasks(a,t){const r={offset:a.currentIndex*a.pageSize,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(n=>n.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())}async clearAllTasks(){return fetch("/_editor/api/tasks/clear",{method:"DELETE"}).then(a=>a.json())}}class Z{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*a.pageSize).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(i=>r[i]===void 0&&delete r[i]),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.")}async clearAllTasks(){throw new Error("Method not implemented.")}}function H({api:o,fetchTasks:a}){const t=k({state:"idle"}),r=()=>{t.value={state:"idle"}},n=()=>{t.value={state:"creating",name:"My task",payloadCode:`{
  "key": "value"
}`}},i=async d=>{const c=t.value;if(c.state!=="creating")throw new Error("Payload is required to create a task");if(!y.value)return;const g=JSON.parse(c.payloadCode);o.createTask(c.name,d,g).then(a).then(r)},y=_(()=>{const d=t.value;if(d.state!=="creating")return!1;try{const c=JSON.parse(d.payloadCode);return R.exports.isObject(c)}catch{return!1}});return{createTask:i,creatingTaskState:t,cancelCreatingTask:r,startCreatingTask:n,isPayloadValid:y}}const f=["pink","orange","green","blue","purple","red","cyan","magenta","volcano","gold"],ee=({api:o,poll:a,stageId:t,stages:r})=>{const n=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:q(s),value:s}))}}),i=[{name:"setToPending",label:"Set to pending",handler:()=>{n.tasks.filter(e=>e.checked).map(e=>e.id).forEach(e=>S(e))}},{name:"setToCompleted",label:"Set to completed",handler:()=>{n.tasks.filter(e=>e.checked).map(e=>e.id).forEach(e=>m(e))}},{name:"duplicate",label:"Duplicate",handler:()=>{n.tasks.filter(e=>e.checked).forEach(async e=>{await o.createTask(e.type,e.targetStageId,e.payload),d()})}}],y=k(["pending"]),d=async()=>{const s=t?await o.getStageTasks(t):await o.getAllTasks(n.pagination,n.filters);n.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})),n.pagination.totalCount=s.totalCount},c=async()=>{if(!t)return[];const s=await o.getSentStageTasks(t);n.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}))},g=()=>{var p;let s=0;n.tasks.concat((p=n.sentTasks)!=null?p:[]).forEach(u=>{n.colorMap.has(u.type)||(n.colorMap.set(u.type,f[s]),s=(s+1)%f.length)})};J(n.filters,async()=>{await Promise.all([d(),c()]),g()});const I=async()=>{await Promise.all([d(),c()]),g()},{startPolling:b,endPolling:w}=L({task:I,interval:15e3}),E=async()=>{if(await Promise.all([d(),c()]),g(),r===null){const s=Promise.all([z.list(),N.list(),$.list(),F.list()]).then(([e,p,u,l])=>[...e,...p,...u,...l]);n.filterOptions.stages=(await s).map(e=>({label:e.title,value:e.id}))}else n.filterOptions.stages=r.map(s=>({label:s.stageTitle,value:s.id}));a&&b()},C=()=>{a&&w()},S=async s=>{await o.updateTaskStatus(s,"pending"),d()},m=async s=>{await o.updateTaskStatus(s,"completed"),d()},x=s=>{n.tasks=n.tasks.map(e=>(e.id===s&&(e.checked=!e.checked),e))},v=()=>{const s=[],e=n.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"),i.filter(l=>s.includes(l.name)))},j=async()=>{await o.clearAllTasks(),d(),c()},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"},{key:"actions",dataIndex:"actions",align:"center"}],A=H({api:o,fetchTasks:d});return{setup:E,tearDown:C,state:n,getColumns:P,collapseValue:y,setToPending:S,setToCompleted:m,fetchTasks:d,fetchSentTasks:c,checkTask:x,getPossibleActions:v,clearAllTasks:j,...A}};export{Y as L,Z as R,ee as T};
//# sourceMappingURL=tasksController.46137c36.js.map
