import{b as c}from"./vendor-lit-B2r-dKc0.js";import{s as l,r as m}from"./app-review-C3CmcSYC.js";import"./app-panel-workspace-C3XDRCgE.js";import"./app-routing-C_r2pdnC.js";function g(e){try{const r=JSON.parse(e);return Array.isArray(r)?r:[]}catch{return[]}}function p(e,r){const s=g(l.staffPreviewTriggers);s[e]&&(r(s[e]),l.staffPreviewTriggers=JSON.stringify(s),l.staffPreviewTriggersEdited=!0,m())}function b(e){const r=g(l.staffPreviewTriggers);r.splice(e,1),l.staffPreviewTriggers=JSON.stringify(r),l.staffPreviewTriggersEdited=!0,m()}function N(){const e=g(l.staffPreviewTriggers);return e.length===0?c`<div class="text-xs text-muted-foreground italic p-3 border border-dashed border-border rounded-md">No triggers configured. Add one above.</div>`:c`<div class="flex flex-col gap-2">${e.map((r,s)=>x(r,s))}</div>`}function P(){return g(l.staffPreviewTriggers).some(r=>(r.type==="goal_created"||r.type==="goal_archived")&&(r.prompt||"").trim().length===0)}function x(e,r){const s={schedule:"⏰ Schedule",git:"🔀 Git",manual:"👆 Manual",goal_created:"🎯 Goal created",goal_archived:"🗄 Goal archived"},u=["schedule","git","manual","goal_created","goal_archived"],a="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",d=e.type==="goal_created"||e.type==="goal_archived",n=d&&(e.prompt||"").trim().length===0,f=o=>{const t=o.target.value;p(r,i=>{i.type=t,t==="schedule"?i.config={cron:"0 9 * * *"}:t==="git"?i.config={event:"push",branch:"master"}:i.config={}})};return c`
		<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=${f}
				>
					${u.map(o=>c`<option value=${o} ?selected=${e.type===o}>${s[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=>p(r,t=>{t.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=${()=>b(r)}
				>✕</button>
			</div>

			${e.type==="schedule"?c`
				<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=${a}
						placeholder="0 9 * * *"
						.value=${e.config?.cron||""}
						@input=${o=>p(r,t=>{t.config.cron=o.target.value})}
					/>
				</div>
				<div class="text-[10px] text-muted-foreground" style="margin-bottom:8px">${h(e.config?.cron||"")}</div>
			`:""}

			${e.type==="git"?c`
				<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=${a}
							.value=${e.config?.event||"push"}
							@change=${o=>p(r,t=>{t.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=${a}
							placeholder="master"
							.value=${e.config?.branch||""}
							@input=${o=>p(r,t=>{t.config.branch=o.target.value})}
						/>
					</div>
				</div>
			`:""}

			<div style="margin-top:${e.type==="manual","0"}">
				<label class="text-[10px] ${n?"text-destructive":"text-muted-foreground"}" style="display:block; margin-bottom:2px">${d?"Wake prompt (required)":"Wake prompt (optional)"}</label>
				<textarea
					class="w-full p-2 text-xs rounded-md border ${n?"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-${r}"
					placeholder="Message sent to the agent when this trigger fires"
					.value=${e.prompt||""}
					@input=${o=>p(r,t=>{t.prompt=o.target.value})}
				></textarea>
				${n?c`<div class="text-[10px] text-destructive" style="margin-top:2px" data-testid="trigger-prompt-error-${r}">Goal triggers require a non-empty wake prompt.</div>`:""}
			</div>
		</div>
	`}function h(e){const r=e.trim().split(/\s+/);if(r.length!==5)return e?`Custom: ${e}`:"";const[s,u,a,d,n]=r,f=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];let o="";if(s!=="*"&&u!=="*"){const t=parseInt(u,10),i=parseInt(s,10);if(!isNaN(t)&&!isNaN(i)){const v=t>=12?"PM":"AM";o=`${t===0?12:t>12?t-12:t}:${i.toString().padStart(2,"0")} ${v}`}}if(u.startsWith("*/")){const t=u.slice(2),i=s==="0"?"on the hour":`at :${s.padStart(2,"0")}`;return`Every ${t} hour${t==="1"?"":"s"}, ${i}`}if(s.startsWith("*/")){const t=s.slice(2);return`Every ${t} minute${t==="1"?"":"s"}`}if(a==="*"&&d==="*"&&n==="*"&&o)return`Daily at ${o}`;if(a==="*"&&d==="*"&&n==="1-5"&&o)return`Weekdays at ${o}`;if(a==="*"&&d==="*"&&n!=="*"&&o){const t=parseInt(n,10);return`Every ${!isNaN(t)&&t>=0&&t<=6?f[t]:n} at ${o}`}return a!=="*"&&d==="*"&&n==="*"&&o?`${a}${a==="1"?"st":a==="2"?"nd":a==="3"?"rd":"th"} of each month at ${o}`:e?`Custom: ${e}`:""}export{h as describeCron,P as hasInvalidGoalTriggersForPreview,g as parseTriggers,b as removeTrigger,x as renderTriggerCard,N as renderTriggersEditor,p as updateTrigger};
