import{b as a}from"./vendor-lit-B2r-dKc0.js";import{B as m,a as b,I as z}from"./vendor-mini-lit-CpUQub-j.js";import{_ as M,$ as O,c as Z,a0 as D,P as H,B as Q,Q as B,a1 as G,a2 as Y,a3 as K,a4 as X,r as ee}from"./app-session-runtime-CeFJDILJ.js";import{r as l,s as L}from"./app-review-C3CmcSYC.js";import{setHashRoute as j}from"./app-routing-C_r2pdnC.js";import{s as R,am as te,an as re,Z as se,E as oe,T as ae,ao as ne}from"./vendor-lucide-B9Yl1Gez.js";import"./vendor-marked-_iteVaMm.js";import"./app-message-reducer-wATqnZGf.js";import"./vendor-sortable-B1o3v0Qs.js";import"./app-inbox-CaZD85aD.js";import"./app-panel-workspace-C3XDRCgE.js";import"./app-pr-walkthrough-WipZACSB.js";let I="list",f=[],n=null,_=!0,g=!1,h=!1,T="",N="",w="",$=!1,A="",p=[],E="",v="compact",ie="Manual wake",C=null,k=-1,S="none";async function je(){I="list",n=null,_=!0,g=!1,h=!1,l(),f=await M(),_=!1,l()}function W(){I="list",n=null,j("staff")}function le(e){I="edit",n=e,T=e.name,N=e.description,w=e.systemPrompt,$=!1,A=e.cwd,p=J(JSON.stringify(e.triggers)),E=e.memory||"",v=e.contextPolicy==="preserve"?"preserve":"compact",C=null,g=!1,h=!1,q(e),j("staff-edit",e.id)}function ze(e){const t=f.find(i=>i.id===e);if(t)I="edit",n=t,T=t.name,N=t.description,w=t.systemPrompt,$=!1,A=t.cwd,p=J(JSON.stringify(t.triggers)),E=t.memory||"",v=t.contextPolicy==="preserve"?"preserve":"compact",C=null,g=!1,h=!1,q(t);else try{window.dispatchEvent(new CustomEvent("search-result-stale",{detail:{kind:"staff",id:e}}))}catch{}l()}async function q(e){const t=e.currentSessionId?L.gatewaySessions.find(i=>i.id===e.currentSessionId):void 0;k=t?O.get(t.id)??-1:-1,S=e.accessory||"none"}function F(e){return e.some(t=>(t.type==="goal_created"||t.type==="goal_archived")&&(t.prompt||"").trim().length===0)}async function de(){if(!n||g||F(p))return;g=!0,l();const e=await G(n.id,{name:T,description:N,systemPrompt:w,cwd:A,triggers:p,memory:E,contextPolicy:v,accessory:S});if(n.currentSessionId){const t=n.currentSessionId,i=O.get(t)??-1;k!==i&&k>=0&&X(t,k)}if(e){const[t]=await Promise.all([M(),ee()]);f=t;const i=f.find(s=>s.id===n.id);i&&(n=i,S=i.accessory||"none")}g=!1,l()}async function ce(){if(!n||h||!confirm(`Delete staff agent "${n.name}"?`))return;h=!0,l(),await K(n.id)&&(f=await M(),W()),h=!1,l()}async function pe(){if(!n)return;const e=n.state==="paused"?"active":"paused";if(await G(n.id,{state:e})){f=await M();const i=f.find(s=>s.id===n.id);i&&(n=i)}l()}async function xe(){if(!n)return;const e=ie.trim()||"Manual wake";(await Y(n.id,{title:"Manual wake",prompt:e,source:{type:"manual_ui"}}))?.entry?C="Enqueued. The agent will process when idle.":C="Failed to enqueue. Check connection.",l()}function J(e){try{const t=JSON.parse(e);return Array.isArray(t)?t:[]}catch{return[]}}function y(e,t){p[e]&&(t(p[e]),l())}function ue(e){p.splice(e,1),l()}function fe(){p.push({type:"schedule",config:{cron:"0 9 * * *"},enabled:!0,prompt:""}),l()}function me(){return p.length===0?a`<div class="text-xs text-muted-foreground italic p-3 border border-dashed border-border rounded-md">No triggers configured. Add one above.</div>`:a`<div class="flex flex-col gap-2">${p.map((e,t)=>ge(e,t))}</div>`}function ge(e,t){const i={schedule:"⏰ Schedule",git:"🔀 Git",manual:"👆 Manual",goal_created:"🎯 Goal created",goal_archived:"🗄 Goal archived"},s=["schedule","git","manual","goal_created","goal_archived"],d="w-full h-8 px-2 text-xs rounded-md border border-border bg-background text-foreground focus:outline-none focus:ring-1 focus:ring-ring",x=e.type==="goal_created"||e.type==="goal_archived",c=x&&(e.prompt||"").trim().length===0,P=o=>{const r=o.target.value;y(t,u=>{u.type=r,r==="schedule"?u.config={cron:"0 9 * * *"}:r==="git"?u.config={event:"push",branch:"master"}:u.config={}})};return a`
		<div class="rounded-md border border-border bg-secondary/20 p-3">
			<div style="display:flex; align-items:center; gap:8px; margin-bottom:8px">
				<select
					class="text-xs px-2 py-1 rounded border border-border bg-background text-foreground focus:outline-none focus:ring-1 focus:ring-ring"
					.value=${e.type}
					@change=${P}
				>
					${s.map(o=>a`<option value=${o} ?selected=${e.type===o}>${i[o]||o}</option>`)}
				</select>
				<label style="display:flex; align-items:center; gap:4px; margin-left:auto; font-size:11px" class="text-muted-foreground cursor-pointer select-none">
					<input
						type="checkbox"
						class="accent-primary"
						.checked=${e.enabled!==!1}
						@change=${o=>y(t,r=>{r.enabled=o.target.checked})}
					/> Enabled
				</label>
				<button
					class="text-[10px] px-1.5 py-0.5 rounded text-muted-foreground hover:text-destructive hover:bg-destructive/10 transition-colors"
					title="Remove trigger"
					@click=${()=>ue(t)}
				>${"✕"}</button>
			</div>

			${e.type==="schedule"?a`
				<div style="margin-bottom:4px">
					<label class="text-[10px] text-muted-foreground" style="display:block; margin-bottom:2px">Cron expression (UTC)</label>
					<input
						type="text"
						class=${d}
						placeholder="0 9 * * *"
						.value=${e.config?.cron||""}
						@input=${o=>y(t,r=>{r.config.cron=o.target.value})}
					/>
				</div>
				<div class="text-[10px] text-muted-foreground" style="margin-bottom:8px">${be(e.config?.cron||"")}</div>
			`:""}

			${e.type==="git"?a`
				<div style="display:grid; grid-template-columns:100px 1fr; gap:8px; margin-bottom:8px">
					<div>
						<label class="text-[10px] text-muted-foreground" style="display:block; margin-bottom:2px">Event</label>
						<select
							class=${d}
							.value=${e.config?.event||"push"}
							@change=${o=>y(t,r=>{r.config.event=o.target.value})}
						>
							<option value="push" ?selected=${e.config?.event==="push"}>push</option>
						</select>
					</div>
					<div>
						<label class="text-[10px] text-muted-foreground" style="display:block; margin-bottom:2px">Branch</label>
						<input
							type="text"
							class=${d}
							placeholder="master"
							.value=${e.config?.branch||""}
							@input=${o=>y(t,r=>{r.config.branch=o.target.value})}
						/>
					</div>
				</div>
			`:""}

			<div>
				<label class="text-[10px] ${c?"text-destructive":"text-muted-foreground"}" style="display:block; margin-bottom:2px">${x?"Wake prompt (required)":"Wake prompt (optional)"}</label>
				<textarea
					class="w-full p-2 text-xs rounded-md border ${c?"border-destructive":"border-border"} bg-background text-foreground resize-y focus:outline-none focus:ring-1 focus:ring-ring"
					rows="2"
					data-testid="trigger-prompt-${t}"
					placeholder="Message sent to the agent when this trigger fires"
					.value=${e.prompt||""}
					@input=${o=>y(t,r=>{r.prompt=o.target.value})}
				></textarea>
				${c?a`<div class="text-[10px] text-destructive" style="margin-top:2px" data-testid="trigger-prompt-error-${t}">Goal triggers require a non-empty wake prompt.</div>`:""}
			</div>
		</div>
	`}function be(e){const t=e.trim().split(/\s+/);if(t.length!==5)return e?`Custom: ${e}`:"";const[i,s,d,x,c]=t,P=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];let o="";if(i!=="*"&&s!=="*"){const r=parseInt(s,10),u=parseInt(i,10);if(!isNaN(r)&&!isNaN(u)){const U=r>=12?"PM":"AM";o=`${r===0?12:r>12?r-12:r}:${u.toString().padStart(2,"0")} ${U}`}}if(s.startsWith("*/")){const r=s.slice(2),u=i==="0"?"on the hour":`at :${i.padStart(2,"0")}`;return`Every ${r} hour${r==="1"?"":"s"}, ${u}`}if(i.startsWith("*/")){const r=i.slice(2);return`Every ${r} minute${r==="1"?"":"s"}`}if(d==="*"&&x==="*"&&c==="*"&&o)return`Daily at ${o}`;if(d==="*"&&x==="*"&&c==="1-5"&&o)return`Weekdays at ${o}`;if(d==="*"&&x==="*"&&c!=="*"&&o){const r=parseInt(c,10);return`Every ${!isNaN(r)&&r>=0&&r<=6?P[r]:c} at ${o}`}return d!=="*"&&x==="*"&&c==="*"&&o?`${d}${d==="1"?"st":d==="2"?"nd":d==="3"?"rd":"th"} of each month at ${o}`:e?`Custom: ${e}`:""}function ve(e){if(!e)return"Never";const t=Date.now()-e;return t<6e4?"Just now":t<36e5?`${Math.floor(t/6e4)}m ago`:t<864e5?`${Math.floor(t/36e5)}h ago`:`${Math.floor(t/864e5)}d ago`}function V(e){const t={active:"bg-green-500/15 text-green-700 dark:text-green-400",paused:"bg-yellow-500/15 text-yellow-700 dark:text-yellow-400",retired:"bg-muted text-muted-foreground"};return a`<span class="px-2 py-0.5 text-xs rounded-full ${t[e]||t.retired}">${e}</span>`}function he(e){return!e||e.length===0?"No triggers":e.map(t=>t.type==="schedule"?`Cron: ${t.config?.cron||"?"}`:t.type==="git"?`Git: ${t.config?.event||"push"}`:t.type).join(", ")}function ye(){return _?a`<div class="text-center py-12 text-muted-foreground text-sm">Loading...</div>`:a`
		<div class="flex-1 flex flex-col overflow-hidden">
			<div class="flex items-center justify-between p-4 border-b border-border shrink-0">
				<div class="flex items-center gap-2">
					${m({variant:"ghost",size:"sm",onClick:()=>j("landing"),children:a`${b(R,"sm")}`})}
					<h1 class="text-lg font-semibold">Staff Agents</h1>
				</div>
			</div>
			<div class="flex-1 overflow-y-auto">
				${f.length===0?a`
						<div class="text-center py-12">
							<div class="text-muted-foreground mb-3 flex justify-center empty-state-icon">${b(ne,"lg")}</div>
							<p class="text-sm text-muted-foreground mb-4">No staff agents yet</p>
							<p class="text-xs text-muted-foreground max-w-sm mx-auto">
								Create a staff agent from the sidebar using the + button next to "Staff",
								or use the Staff Assistant.
							</p>
						</div>
					`:a`
						<table class="w-full text-sm">
							<thead>
								<tr class="border-b border-border text-xs text-muted-foreground uppercase tracking-wider">
									<th class="text-left px-4 py-2 font-medium">Name</th>
									<th class="text-left px-4 py-2 font-medium">State</th>
									<th class="text-left px-4 py-2 font-medium">Triggers</th>
									<th class="text-left px-4 py-2 font-medium">Last Active</th>
								</tr>
							</thead>
							<tbody>
								${f.map(e=>a`
									<tr class="border-b border-border/50 hover:bg-secondary/30 cursor-pointer transition-colors"
										@click=${()=>le(e)}>
										<td class="px-4 py-3">
											<div class="font-medium">${e.name}</div>
											<div class="text-xs text-muted-foreground truncate max-w-[300px]">${e.description}</div>
										</td>
										<td class="px-4 py-3">${V(e.state)}</td>
										<td class="px-4 py-3 text-muted-foreground text-xs">${he(e.triggers)}</td>
										<td class="px-4 py-3 text-muted-foreground text-xs">${ve(e.lastWakeAt)}</td>
									</tr>
								`)}
							</tbody>
						</table>
					`}
			</div>
		</div>
	`}function $e(){if(!n)return a`<div class="p-4">Staff agent not found</div>`;const e=B(S),t=e.id!=="none"&&e.shadow!=="",i=Math.ceil(D.length/2);return a`
		<div class="flex-1 flex flex-col overflow-hidden">
			<div class="flex items-center justify-between p-4 border-b border-border shrink-0">
				<div class="flex items-center gap-2">
					${m({variant:"ghost",size:"sm",onClick:W,children:a`${b(R,"sm")}`})}
					<h1 class="text-lg font-semibold">${n.name}</h1>
					${V(n.state)}
				</div>
				<div class="flex items-center gap-2">
					${m({variant:"ghost",size:"sm",onClick:pe,children:a`<span class="inline-flex items-center gap-1">${b(n.state==="paused"?te:re,"sm")} ${n.state==="paused"?"Resume":"Pause"}</span>`})}
					${m({variant:"default",size:"sm",onClick:xe,children:a`<span class="inline-flex items-center gap-1">${b(se,"sm")} Wake Now</span>`})}
					${n.currentSessionId?m({variant:"outline",size:"sm",onClick:()=>Z(n.currentSessionId,!0),children:a`<span class="inline-flex items-center gap-1">${b(oe,"sm")} View Session</span>`}):""}
					${m({variant:"ghost",size:"sm",onClick:ce,disabled:h,children:a`<span class="inline-flex items-center gap-1 text-destructive">${b(ae,"sm")} Delete</span>`})}
				</div>
			</div>
			<div class="flex-1 overflow-y-auto p-5 flex flex-col gap-4">
				<div>
					<label class="text-xs text-muted-foreground mb-1.5 block font-medium">Name</label>
					${z({type:"text",value:T,placeholder:"Staff agent name",onInput:s=>{T=s.target.value,l()}})}
				</div>
				<div>
					<label class="text-xs text-muted-foreground mb-1.5 block font-medium">Description</label>
					<textarea
						class="w-full p-2 text-sm rounded-md border border-border bg-background text-foreground resize-y focus:outline-none focus:ring-1 focus:ring-ring"
						rows="2"
						placeholder="What does this staff agent do?"
						.value=${N}
						@input=${s=>{N=s.target.value,l()}}
					></textarea>
				</div>
				<div>
					<label class="text-xs text-muted-foreground mb-1.5 block font-medium">Working Directory</label>
					${z({type:"text",value:A,placeholder:L.defaultCwd||"(server default)",onInput:s=>{A=s.target.value,l()}})}
				</div>
				<!-- Sandbox indicator (read-only; chosen at creation, immutable) -->
				<div>
					<label class="text-xs text-muted-foreground mb-1.5 block font-medium">Sandbox</label>
					<div class="text-sm text-foreground">
						${n.sandboxed?"Enabled":"Disabled"}
					</div>
				</div>
				<!-- Colour picker -->
				<div>
					<div class="text-xs text-muted-foreground mb-2 font-medium">Colour</div>
					<div class="flex flex-col gap-2">
						${[0,i].map(s=>a`
							<div class="flex gap-2 justify-center">
								${D.slice(s,s+i).map((d,x)=>{const c=s+x,P=k===c,o=t?e.shadow:"",r=e.id!=="flask"?`filter:hue-rotate(${-d}deg);`:"";return a`
										<button
											class="relative transition-all rounded-lg flex items-center justify-center
												${P?"ring-2 ring-primary ring-offset-1 ring-offset-background":"hover:bg-secondary/50"}"
											style="width:${t?34:28}px;height:24px;"
											title="Colour ${c+1}"
											@click=${()=>{k=c,l()}}
										>
											<span style="position:absolute;left:${t?3:4}px;top:3px;filter:hue-rotate(${d}deg);">
												<span style="position:absolute;left:0;top:0;display:block;width:1px;height:1px;image-rendering:pixelated;transform:scale(2);transform-origin:0 0;box-shadow:3px 0px 0 #000,4px 0px 0 #000,5px 0px 0 #000,6px 0px 0 #000,7px 0px 0 #000,2px 1px 0 #000,3px 1px 0 #8ec63f,4px 1px 0 #8ec63f,5px 1px 0 #8ec63f,6px 1px 0 #b5d98a,7px 1px 0 #b5d98a,8px 1px 0 #000,1px 2px 0 #000,2px 2px 0 #8ec63f,3px 2px 0 #8ec63f,4px 2px 0 #8ec63f,5px 2px 0 #8ec63f,6px 2px 0 #8ec63f,7px 2px 0 #b5d98a,8px 2px 0 #8ec63f,9px 2px 0 #000,0px 3px 0 #000,1px 3px 0 #8ec63f,2px 3px 0 #8ec63f,3px 3px 0 #8ec63f,4px 3px 0 #8ec63f,5px 3px 0 #8ec63f,6px 3px 0 #8ec63f,7px 3px 0 #8ec63f,8px 3px 0 #8ec63f,9px 3px 0 #000,0px 4px 0 #000,1px 4px 0 #8ec63f,2px 4px 0 #8ec63f,3px 4px 0 #1a3010,4px 4px 0 #8ec63f,5px 4px 0 #8ec63f,6px 4px 0 #1a3010,7px 4px 0 #8ec63f,8px 4px 0 #8ec63f,9px 4px 0 #000,0px 5px 0 #000,1px 5px 0 #8ec63f,2px 5px 0 #8ec63f,3px 5px 0 #1a3010,4px 5px 0 #8ec63f,5px 5px 0 #8ec63f,6px 5px 0 #1a3010,7px 5px 0 #8ec63f,8px 5px 0 #8ec63f,9px 5px 0 #000,0px 6px 0 #000,1px 6px 0 #6b9930,2px 6px 0 #8ec63f,3px 6px 0 #8ec63f,4px 6px 0 #8ec63f,5px 6px 0 #8ec63f,6px 6px 0 #8ec63f,7px 6px 0 #8ec63f,8px 6px 0 #8ec63f,9px 6px 0 #000,1px 7px 0 #000,2px 7px 0 #6b9930,3px 7px 0 #8ec63f,4px 7px 0 #8ec63f,5px 7px 0 #8ec63f,6px 7px 0 #8ec63f,7px 7px 0 #8ec63f,8px 7px 0 #000,2px 8px 0 #000,3px 8px 0 #000,4px 8px 0 #000,5px 8px 0 #000,6px 8px 0 #000,7px 8px 0 #000;"></span>
												${t?a`<span style="position:absolute;left:0;top:0;display:block;width:1px;height:1px;image-rendering:pixelated;transform:scale(2);transform-origin:0 0;box-shadow:${o};${r}"></span>`:""}
											</span>
										</button>
									`})}
							</div>
						`)}
					</div>
				</div>
				<!-- Accessory picker -->
				<div>
					<div class="text-xs text-muted-foreground mb-2 font-medium">Accessory</div>
					<div class="flex flex-wrap gap-2 justify-center">
						${H.map(s=>{const d=B(s);return a`
								<button
									class="relative transition-all rounded-lg flex flex-col items-center gap-0.5 px-1 py-1
										${S===s?"ring-2 ring-primary ring-offset-1 ring-offset-background":"hover:bg-secondary/50"}"
									style="width:40px;"
									title="${d.label}"
									@click=${()=>{S=s,l()}}
								>
									<span class="block" style="width:20px;height:18px;position:relative;">
										${Q("idle",!1,void 0,!1,!1,!1,!1,s,!0)}
									</span>
									<span class="text-[9px] text-muted-foreground truncate w-full text-center">${d.label}</span>
								</button>
							`})}
					</div>
				</div>
				<div>
					<div class="flex items-center justify-between mb-1.5">
						<label class="text-xs text-muted-foreground font-medium">Triggers</label>
						<button
							class="text-[10px] px-2 py-0.5 rounded border border-border text-muted-foreground hover:text-foreground hover:bg-secondary transition-colors"
							title="Add trigger"
							@click=${fe}
						>+ Add trigger</button>
					</div>
					${me()}
				</div>
				<div>
					<div class="flex items-center justify-between mb-1.5">
						<label class="text-xs text-muted-foreground font-medium">System Prompt</label>
						<button
							class="text-[10px] px-2 py-0.5 rounded border border-border text-muted-foreground hover:text-foreground hover:bg-secondary transition-colors"
							title="Toggle prompt edit mode"
							@click=${()=>{$=!$,l()}}
						>
							${$?"Preview":"Edit"}
						</button>
					</div>
					${$?a`<textarea
								class="p-3 text-sm font-mono rounded-md border border-border bg-background text-foreground resize-y focus:outline-none focus:ring-1 focus:ring-ring"
								style="min-height:150px; max-height:400px; width:100%"
								.value=${w}
								@input=${s=>{w=s.target.value}}
							></textarea>`:a`<div class="p-3 rounded-md border border-border bg-secondary/30 overflow-y-auto text-sm" style="min-height:150px; max-height:400px">
								<markdown-block .content=${w||"_No prompt content yet_"}></markdown-block>
							</div>`}
				</div>
				<div>
					<label class="text-xs text-muted-foreground mb-1.5 block font-medium">Pinned Context (optional)</label>
					<p class="text-[10px] text-muted-foreground mb-1">Injected into the system prompt. Survives conversation compaction.</p>
					<textarea
						class="w-full p-2 text-sm font-mono rounded-md border border-border bg-background text-foreground resize-y focus:outline-none focus:ring-1 focus:ring-ring"
						rows="4"
						.value=${E}
						@input=${s=>{E=s.target.value}}
					></textarea>
				</div>

				<div class="context-policy-group" data-testid="context-policy">
					<label class="text-xs text-muted-foreground mb-1.5 block font-medium">Context Policy</label>
					<p class="text-[10px] text-muted-foreground mb-1">What happens before a wake digest is sent when the inbox has pending entries.</p>
					<div class="flex gap-3">
						<label class="flex items-center gap-1.5 text-sm cursor-pointer">
							<input
								type="radio"
								name="contextPolicy"
								value="compact"
								.checked=${v==="compact"}
								@change=${()=>{v="compact",l()}}
							/>
							<span>Compact <span class="text-[10px] text-muted-foreground">(default)</span></span>
						</label>
						<label class="flex items-center gap-1.5 text-sm cursor-pointer">
							<input
								type="radio"
								name="contextPolicy"
								value="preserve"
								.checked=${v==="preserve"}
								@change=${()=>{v="preserve",l()}}
							/>
							<span>Preserve</span>
						</label>
					</div>
				</div>

				${C?a`<div class="text-xs text-muted-foreground border border-border rounded p-2 bg-secondary/30" data-testid="wake-feedback">${C}</div>`:""}

				<div class="flex items-center justify-end gap-2 pt-2 border-t border-border">
					${m({variant:"ghost",onClick:W,children:"Cancel"})}
					${m({variant:"default",onClick:de,disabled:g||F(p),children:g?"Saving...":"Save Changes"})}
				</div>
			</div>
		</div>
	`}function De(){return I==="edit"?$e():ye()}export{je as loadStaffPageData,ze as navigateToStaffEdit,De as renderStaffPage};
