import{r as e}from"./rolldown-runtime-hePW80VL.js";import{o as t}from"./vendor-forms-yQg41KHz.js";import{l as n,t as r,u as i}from"./vendor-react-CAGJwf-T.js";import{c as a}from"./api-sqDSbqTC.js";import{t as o}from"./arrow-left-C4s0_GjY.js";import{t as s}from"./circle-x-BmjkkTIE.js";import{t as c}from"./refresh-cw-9IjPWvzY.js";import{t as l}from"./rotate-ccw-DxwMb1G3.js";import{D as u,St as d,vt as f,x as p}from"./index-Z_bdWDpJ.js";import{t as m}from"./PageHeader-UI75nV23.js";import{D as h,E as g,I as _,P as v,R as y,s as b}from"./workflowRtkApi-CinPa_NH.js";import{t as x}from"./Card-CBjYg7NQ.js";import{n as S,t as C}from"./Tabs-D8UstNBC.js";import{n as w}from"./workflowDefinition-VIUSUzpJ.js";import{n as T,r as E,t as D}from"./TransitionDialog-D9o-qeUZ.js";import{t as O}from"./InstanceHistory-Dw1b_jQg.js";var k=e(t(),1),A=r();function j(){let{id:e=``}=i(),t=n(),[r,j]=(0,k.useState)(!1),{data:M,isLoading:N}=g(e,{skip:!e}),{data:P}=h(M?.workflow??``,{skip:!M?.workflow}),[F,{isLoading:I}]=b(),[L,{isLoading:R}]=y(),[z,{isLoading:B}]=v(),[V,{isLoading:H}]=_(),{steps:U,transitions:W}=(0,k.useMemo)(()=>P?w(P):{steps:[],transitions:[]},[P]),G=(0,k.useMemo)(()=>U.filter(e=>e.id!==M?.current_step),[U,M?.current_step]);if(N)return(0,A.jsx)(`div`,{className:`flex h-64 items-center justify-center`,children:(0,A.jsx)(f,{className:`h-8 w-8 animate-spin text-[var(--color-text-muted)]`})});if(!M)return(0,A.jsxs)(`div`,{className:`flex flex-col items-center justify-center py-24 text-center`,children:[(0,A.jsx)(`p`,{className:`font-medium text-[var(--color-text-primary)]`,children:`Workflow instance not found`}),(0,A.jsx)(u,{variant:`ghost`,size:`sm`,className:`mt-4`,onClick:()=>t(`/admin/workflow`),children:`Back to Workflows`})]});let K=[`in_progress`,`pending`].includes(M.status),q=![`completed`,`cancelled`].includes(M.status),J=[`in_progress`,`pending`].includes(M.status),Y=M.status===`rejected`,X=async(e,t)=>{try{await e(),a.success(t)}catch(e){a.error(p(e))}};return(0,A.jsxs)(`div`,{children:[(0,A.jsxs)(`button`,{onClick:()=>t(-1),className:`mb-3 flex items-center gap-1.5 text-[12px] text-[var(--color-text-muted)] transition-colors hover:text-[var(--color-text-secondary)]`,children:[(0,A.jsx)(o,{className:`h-3.5 w-3.5`}),` Back`]}),(0,A.jsx)(m,{title:`Workflow Instance`,description:`${M.workflow_name||`Workflow`} — Version ${M.workflow_version}`,actions:(0,A.jsxs)(`div`,{className:`flex items-center gap-2`,children:[K&&G.length>0&&(0,A.jsxs)(u,{variant:`outline`,onClick:()=>j(!0),isLoading:H,children:[(0,A.jsx)(d,{className:`h-4 w-4`}),` Transition`]}),J&&(0,A.jsxs)(u,{variant:`outline`,onClick:()=>X(()=>L({id:e,reason:`Withdrawn by user`}).unwrap(),`Instance withdrawn`),isLoading:R,children:[(0,A.jsx)(l,{className:`h-4 w-4`}),` Withdraw`]}),Y&&(0,A.jsxs)(u,{variant:`outline`,onClick:()=>X(()=>z(e).unwrap(),`Document reset to draft`),isLoading:B,children:[(0,A.jsx)(c,{className:`h-4 w-4`}),` Resubmit`]}),q&&(0,A.jsxs)(u,{variant:`danger`,onClick:()=>X(()=>F({id:e,reason:`Cancelled by user`}).unwrap(),`Instance cancelled`),isLoading:I,children:[(0,A.jsx)(s,{className:`h-4 w-4`}),` Cancel`]})]})}),(0,A.jsxs)(`div`,{className:`grid grid-cols-1 gap-6 lg:grid-cols-3`,children:[(0,A.jsxs)(`div`,{className:`space-y-6 lg:col-span-2`,children:[(0,A.jsxs)(x,{children:[(0,A.jsx)(`h2`,{className:`mb-4 text-base font-semibold text-[var(--color-text-primary)]`,children:`Workflow Diagram`}),(0,A.jsx)(E,{instance:M,steps:U,transitions:W})]}),(0,A.jsx)(x,{children:(0,A.jsxs)(S,{tabs:[{id:`history`,label:`History`},{id:`data`,label:`Instance Data`}],defaultTab:`history`,children:[(0,A.jsx)(C,{id:`history`,children:(0,A.jsx)(O,{instanceId:e})}),(0,A.jsx)(C,{id:`data`,children:M.instance_data&&Object.keys(M.instance_data).length>0?(0,A.jsx)(`pre`,{className:`overflow-x-auto rounded-lg bg-slate-50 dark:bg-slate-800/40 p-4 text-xs text-[var(--color-text-secondary)]`,children:JSON.stringify(M.instance_data,null,2)}):(0,A.jsx)(`p`,{className:`py-8 text-center text-sm text-[var(--color-text-muted)]`,children:`No instance data available`})})]})})]}),(0,A.jsx)(`div`,{children:(0,A.jsx)(T,{instance:M})})]}),(0,A.jsx)(D,{open:r,onClose:()=>j(!1),instance:M,availableSteps:G,onTransition:async(t,n)=>{await V({id:e,body:{to_step:t,comments:n}}).unwrap(),a.success(`Workflow transitioned`)},isLoading:H})]})}export{j as default};