@xauyxau/hnch 1.2.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +4 -4
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import Or from'yargs';import {hideBin}from'yargs/helpers';import at,{useState,useEffect,useCallback,useMemo,useRef}from'react';import {render,useApp,useInput,Box,Text}from'ink';import*as we from'yjs';import {Awareness}from'y-protocols/awareness';import {mkdirSync,chmodSync,writeFileSync,unlinkSync,existsSync,readFileSync}from'fs';import {homedir}from'os';import {join}from'path';import {randomUUID}from'crypto';import {createInterface}from'readline';import {WebsocketProvider}from'y-websocket';import Hn from'ws';import Rr from'clipboardy';import {jsxs,jsx,Fragment}from'react/jsx-runtime';var fn="https://hnch.dev";function Ie(){let t=process.env.XDG_CONFIG_HOME||join(homedir(),".config");return join(t,"hnch")}function ht(){return join(Ie(),"config.json")}function Vt(){let e=Ie();existsSync(e)||mkdirSync(e,{recursive:true});}function de(){let e=ht();if(!existsSync(e))return {};try{return JSON.parse(readFileSync(e,"utf-8"))}catch{return {}}}function $(e){Vt();let o={...de(),...e};writeFileSync(ht(),JSON.stringify(o,null,2)+`
3
- `,"utf-8");}async function gn(e){let t=createInterface({input:process.stdin,output:process.stderr});return new Promise(o=>{t.question(e,n=>{t.close(),o(n.trim());});})}function hn(e){return e.startsWith("wss://")?"https://"+e.slice(6):e.startsWith("ws://")?"http://"+e.slice(5):e}async function Te(){let e=de(),t=false;if(e.participantId||(e.participantId=randomUUID(),t=true),!e.displayName){let n=await gn("Enter your display name: ");e.displayName=n||`user-${e.participantId.slice(0,6)}`,t=true;}e.relayUrl||(e.relayUrl=fn,t=true),t&&$(e);let o=process.env.HNCH_RELAY_URL||e.relayUrl;return o=hn(o),{participantId:e.participantId,displayName:e.displayName,relayUrl:o,defaultScale:e.defaultScale,defaultSessionName:e.defaultSessionName,defaultPassword:e.defaultPassword,presetOverrides:e.presetOverrides,customScales:e.customScales}}function Ee(e){Vt();let o={...de()};for(let[n,r]of Object.entries(e))r===""?delete o[n]:r!==void 0&&(o[n]=r);writeFileSync(ht(),JSON.stringify(o,null,2)+`
4
- `,"utf-8");}var Sn={"unknown-room":4001,"wrong-password":4002,"invalid-invite":4003,"expired-invite":4004,"invite-already-redeemed":4005,"room-full":4006,"malformed-credentials":4010},Jt=new Map(Object.entries(Sn).map(([e,t])=>[t,e]));function ue(e){return e.role??"voter"}var Le={id:"fibonacci",cards:["0","1","2","3","5","8","13","21"]},Wt={id:"t-shirt",cards:["XS","S","M","L","XL"]},qt={id:"powers-of-2",cards:["1","2","4","8","16","32","64"]},Gt={id:"linear",cards:["1","2","3","4","5","6","7","8","9","10"]},Qt={id:"risk",cards:["Low","Medium","High","Critical"]},X={[Le.id]:Le,[Wt.id]:Wt,[qt.id]:qt,[Gt.id]:Gt,[Qt.id]:Qt},zt={fibonacci:"Fibonacci","t-shirt":"T-shirt","powers-of-2":"Powers of 2",linear:"Linear (1\u201310)",risk:"Risk"};function pe(e){return e in zt?zt[e]:e.length===0?e:(e.startsWith("custom-")?e.slice(7):e).split("-").map(o=>o.length===0?o:o.charAt(0).toUpperCase()+o.slice(1)).join(" ")}var xn="meta",Ae="settings",yn="participants",wn="rounds",Cn=["roomUuid","createdAt","createdBy","sessionEndedAt"],vn=["name","defaultScale","hostParticipantId"],bn=["displayName","joinedAt","role","leftAt","removedBy"],Rn=["id","title","description","ticketId","parentRoundId","scale","timerStartedAt","revealedAt","createdBy","createdAt"];function ke(e){return e.getMap(xn)}function _e(e){return e.getMap(Ae)}function $e(e){return e.getMap(yn)}function St(e){return e.getArray(wn)}function Fe(e,t){let o={};for(let n of t){let r=e.get(n);r!==void 0&&(o[n]=r);}return o}function In(e){return Fe(ke(e),Cn)}function Tn(e){return Fe(_e(e),vn)}function En(e){let t=$e(e),o=[];return t.forEach((n,r)=>{let s=Fe(n,bn);o.push({id:r,...s});}),o.sort((n,r)=>n.joinedAt-r.joinedAt||n.id.localeCompare(r.id)),o}function An(e){let t={};return e.forEach((o,n)=>{t[n]=o;}),t}function Pn(e){let t=Fe(e,Rn),o=e.get("votes"),n=o?An(o):{};return {...t,votes:n}}function Nn(e){let t=St(e),o=[];return t.forEach(n=>{o.push(Pn(n));}),o}function z(e){return {meta:In(e),settings:Tn(e),participants:En(e),rounds:Nn(e)}}function Kt(e,t){if(ke(e).has("roomUuid"))throw new Error("initDoc: doc already seeded");e.transact(()=>{let o=ke(e);o.set("roomUuid",t.roomUuid),o.set("createdAt",t.createdAt),o.set("createdBy",t.participantId);let n=_e(e);t.sessionName!==void 0&&n.set("name",t.sessionName),n.set("defaultScale",t.defaultScale),n.set("hostParticipantId",t.participantId);let r=new we.Map;r.set("displayName",t.displayName),r.set("joinedAt",t.createdAt),$e(e).set(t.participantId,r);});}function Ye(e,t){e.transact(()=>{let o=$e(e),n=o.get(t.participantId),r=n??new we.Map;r.set("displayName",t.displayName),n===void 0&&r.set("joinedAt",Date.now()),r.delete("leftAt"),r.delete("removedBy"),n===void 0&&o.set(t.participantId,r);});}function Ce(e,t){e.transact(()=>{let o=_e(e);for(let[n,r]of Object.entries(t))r===void 0?o.delete(n):o.set(n,r);});}function xt(e,t,o){e.transact(()=>{let n=$e(e),r=n.get(t),s=r??new we.Map;s.set("displayName",o.displayName),s.set("joinedAt",o.joinedAt),o.role!==void 0&&s.set("role",o.role),r===void 0&&n.set(t,s);});}function yt(e,t){let o=t.scale??_e(e).get("defaultScale");if(!o)throw new Error("appendRound: no scale (settings.defaultScale missing)");return e.transact(()=>{let n=new we.Map;n.set("id",t.id),n.set("createdAt",t.createdAt),n.set("createdBy",t.createdBy),n.set("scale",o),t.title!==void 0&&n.set("title",t.title),t.description!==void 0&&n.set("description",t.description),t.ticketId!==void 0&&n.set("ticketId",t.ticketId),t.parentRoundId!==void 0&&n.set("parentRoundId",t.parentRoundId),n.set("timerStartedAt",t.createdAt),n.set("votes",new we.Map),St(e).push([n]);}),t.id}function wt(e,t){let o=St(e);for(let n=0;n<o.length;n+=1){let r=o.get(n);if(r.get("id")===t)return r}}function Xt(e,t,o,n){let r=wt(e,t);if(!r)throw new Error(`castVote: round ${t} not found`);r.get("revealedAt")===void 0&&e.transact(()=>{r.get("votes").set(o,n);});}function Zt(e,t,o){let n=wt(e,t);if(!n)throw new Error(`clearVote: round ${t} not found`);n.get("revealedAt")===void 0&&e.transact(()=>{n.get("votes").delete(o);});}function Ct(e,t,o){let n=wt(e,t);if(!n)throw new Error(`revealRound: round ${t} not found`);e.transact(()=>{n.set("revealedAt",o);});}function eo(e){e.transact(()=>{ke(e).set("sessionEndedAt",Date.now());});}function Z(e){let{rounds:t}=e;if(t.length===0)return null;for(let o=t.length-1;o>=0;o-=1){let n=t[o];if(n.revealedAt===void 0)return n}return t[t.length-1]}function to(e){let t=Object.values(e.votes);return t.length<2?false:t.every(o=>o===t[0])}var oo="PBKDF2",On="SHA-256",no=new TextEncoder;function Dn(e){return Array.from(new Uint8Array(e)).map(t=>t.toString(16).padStart(2,"0")).join("")}async function me(e,t){let o=await crypto.subtle.importKey("raw",no.encode(e),oo,false,["deriveBits"]),n=await crypto.subtle.deriveBits({name:oo,salt:no.encode(t),iterations:1e5,hash:On},o,256);return Dn(n)}var ro="ABCDEFGHJKMNPQRSTUVWXYZ23456789";if(ro.length!==31)throw new Error(`Room code alphabet must be 31 characters, got ${ro.length}`);function vt(e){return e.trim().replace(/-/g,"").toUpperCase()}function so(e,t=true){let o=vt(e);return !t||o.length!==6?o:`${o.slice(0,3)}-${o.slice(3)}`}function Ve(e,t,o){if(e.startsWith("custom-")){let r=(o??[]).find(s=>s.id===e);return r?{id:r.id,cards:r.cards}:void 0}let n=X[e];if(n){let r=(t??{})[e]??[];if(r.length===0)return n;let s=n.cards.filter((u,i)=>!r.includes(i));return s.length===0?void 0:{id:e,cards:s}}}function Je(e,t){let o=Object.keys(X).map(r=>Ve(r,e,t)).filter(r=>r!==void 0),n=(t??[]).map(r=>({id:r.id,cards:r.cards}));return [...o,...n]}async function io(e,t){let o=await fetch(`${e}/rooms`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({})});if(!o.ok)throw new Error(`POST /rooms failed: ${o.status}`);return await o.json()}async function Pe(e,t){let o=await fetch(`${e}/rooms/${encodeURIComponent(t)}`);if(o.status===404){let n=await o.json().catch(()=>null);throw new Error(`Room not found: ${n?.reason??"unknown-room"}`)}if(!o.ok)throw new Error(`GET /rooms/:code failed: ${o.status}`);return await o.json()}async function bt(e,t,o){let n=await fetch(`${e}/rooms/${t}/access`,{method:"PATCH",headers:{"content-type":"application/json"},body:JSON.stringify({passwordHash:o})});if(!n.ok)throw new Error(`PATCH /rooms/:uuid/access failed: ${n.status}`)}function ao(e){let o=`${e.wsBaseUrl.replace(/^http/,"ws")}/ws`,n={did:e.deviceId};e.passwordHash!==void 0&&(n.pw=e.passwordHash);let r=new WebsocketProvider(o,e.roomUuid,e.doc,{params:n,connect:true,...e.awareness!==void 0&&{awareness:e.awareness},WebSocketPolyfill:Hn}),s=c=>{e.onStatusChange?.(c.status);},u=c=>{if(c===null)return;let a=Jt.get(c.code);a!==void 0&&(e.onRejection?.(a),r.shouldConnect=false,r.disconnect());};r.on("status",s),r.on("connection-close",u);let i=false,p=e.passwordHash;return {doc:e.doc,awareness:r.awareness,updateCredentials(c){if(c.passwordHash===p)return;p=c.passwordHash??void 0,r.shouldConnect=false,r.disconnect();let l={did:r.params.did??""};p!==void 0&&(l.pw=p),r.params=l,r.connect();},disconnect(){i||(i=true,r.off("status",s),r.off("connection-close",u),r.shouldConnect=false,r.disconnect(),r.destroy());}}}var Bn=5e3;function co(e,t,o){let n=null,r=null;function s(){let a=new Set;return t.getStates().forEach(l=>{let S=l.participantId;typeof S=="string"&&a.add(S);}),a}function u(){return e.getMap(Ae).get("hostParticipantId")}function i(){r!==null&&(clearTimeout(r),r=null),n=null;}function p(){let a=u(),l=n;if(n=null,!l||a!==l)return;let S=s();if(S.has(a))return;let E=[...S].sort();if(E.length===0)return;let N=E[0];N===o&&Ce(e,{hostParticipantId:N});}function c(){let a=u();if(!a)return;if(s().has(a)){i();return}n!==a&&(i(),n=a,r=setTimeout(()=>{r=null,p();},Bn));}return t.on("change",c),e.getMap(Ae).observe(c),c(),{stop(){i(),t.off("change",c),e.getMap(Ae).unobserve(c);}}}async function po(e){let t,o=await io(e.relayUrl);e.password&&(t=await me(e.password,o.uuid),await bt(e.relayUrl,o.uuid,t));let n=new we.Doc,r=new Awareness(n),s=Date.now();return Kt(n,{roomUuid:o.uuid,createdAt:s,participantId:e.participantId,displayName:e.displayName,defaultScale:e.defaultScale??Le,sessionName:e.sessionName}),r.setLocalStateField("participantId",e.participantId),We({doc:n,awareness:r,roomCode:o.code,roomUuid:o.uuid,participantId:e.participantId,relayUrl:e.relayUrl,passwordHash:t})}async function mo(e){let t=await Pe(e.relayUrl,e.roomCode),o;if(t.accessMode==="password"){if(!e.password)throw new Error("Room requires a password");o=await me(e.password,t.uuid);}let n=new we.Doc,r=new Awareness(n);return Ye(n,{participantId:e.participantId,displayName:e.displayName}),r.setLocalStateField("participantId",e.participantId),We({doc:n,awareness:r,roomCode:e.roomCode,roomUuid:t.uuid,participantId:e.participantId,relayUrl:e.relayUrl,passwordHash:o,onRejection:e.onRejection})}function We(e){let{doc:t,awareness:o,participantId:n,relayUrl:r,roomUuid:s}=e,u=new Set,i=new Set,p=new Set,c=ao({wsBaseUrl:e.relayUrl,roomUuid:e.roomUuid,doc:t,deviceId:n,passwordHash:e.passwordHash,awareness:o,onStatusChange(d){u.forEach(g=>g(d));},onRejection(d){console.error(`Connection rejected: ${d}`),e.onRejection?.(d);}}),a=co(t,o,n),l=()=>{i.forEach(d=>d());};t.getMap("meta").observeDeep(l),t.getMap("settings").observeDeep(l),t.getMap("participants").observeDeep(l),t.getArray("rounds").observeDeep(l);function S(){let d=new Set;return o.getStates().forEach(g=>{let w=g.participantId;typeof w=="string"&&d.add(w);}),d}let E=new Set,N=setInterval(()=>{let d=z(t),g=Z(d);if(!g||g.revealedAt!==void 0||E.has(g.id))return;let w=S();if(w.size===0)return;let h=d.participants.filter(f=>w.has(f.id)&&ue(f)==="voter");if(h.length===0)return;h.filter(f=>f.id in g.votes).length>=h.length&&(E.add(g.id),Ct(t,g.id,Date.now()));},250),R=t.getMap("settings").get("hostParticipantId"),I=()=>{let d=t.getMap("settings").get("hostParticipantId");d!==R&&d!==void 0&&p.forEach(g=>g(d)),R=d;};t.getMap("settings").observe(I);let H=false;return {doc:t,awareness:o,roomCode:e.roomCode,roomUuid:e.roomUuid,projection(){return z(t)},current(){return Z(z(t))},isHost(){return z(t).settings.hostParticipantId===n},onlineIds:S,vote(d){let g=Z(z(t));g&&Xt(t,g.id,n,d);},unvote(){let d=Z(z(t));d&&Zt(t,d.id,n);},reveal(){let d=Z(z(t));d&&Ct(t,d.id,Date.now());},newRound(d){let g=randomUUID();return yt(t,{id:g,createdAt:Date.now(),createdBy:n,title:d}),g},revote(){let d=Z(z(t)),g=randomUUID();return yt(t,{id:g,createdAt:Date.now(),createdBy:n,parentRoundId:d?.id,title:d?.title}),g},updateName(d){Ce(t,{name:d});},updateScale(d){Ce(t,{defaultScale:d});},updateDisplayName(d){let h=z(t).participants.find(M=>M.id===n)?.joinedAt??Date.now();xt(t,n,{displayName:d,joinedAt:h});},async updateAccess(d,g){let w=null;if(d==="password"){if(!g)throw new Error("Password required for password mode");w=await me(g,s);}await bt(r,s,w),c.updateCredentials({passwordHash:w});},handoffHost(d){if(!this.isHost())throw new Error("Only the current host can hand off");if(!S().has(d))throw new Error("Target participant is not online");if(!z(t).participants.some(M=>M.id===d))throw new Error("Target participant does not exist");Ce(t,{hostParticipantId:d});},toggleRole(){let g=z(t).participants.find(f=>f.id===n),h=ue(g??{})==="viewer"?"voter":"viewer",M=g?.joinedAt??Date.now();xt(t,n,{displayName:g?.displayName??"",joinedAt:M,role:h});},endSessionForEveryone(){if(!this.isHost())throw new Error("Only the current host can end the session");eo(t);},onChange(d){return i.add(d),()=>i.delete(d)},onStatusChange(d){return u.add(d),()=>u.delete(d)},onHostChange(d){return p.add(d),()=>p.delete(d)},disconnect(){H||(H=true,clearInterval(N),a.stop(),t.getMap("meta").unobserveDeep(l),t.getMap("settings").unobserveDeep(l),t.getMap("settings").unobserve(I),t.getMap("participants").unobserveDeep(l),t.getArray("rounds").unobserveDeep(l),c.disconnect());}}}function fo(e){let t=vt(e);return t===""?null:t}var Fn=1440*60*1e3;function It(){return join(Ie(),"last-session.json")}function Yn(e){if(typeof e!="object"||e===null)return false;let t=e;return typeof t.roomCode=="string"&&typeof t.roomUuid=="string"&&typeof t.lastSeenAt=="number"&&(t.passwordHash===void 0||typeof t.passwordHash=="string")}function ho(){let e=It();if(!existsSync(e))return null;try{let t=JSON.parse(readFileSync(e,"utf-8"));return !Yn(t)||Date.now()-t.lastSeenAt>Fn?(k(),null):t}catch{return k(),null}}function qe(e){let t=Ie();mkdirSync(t,{recursive:true,mode:448}),chmodSync(t,448);let o=It();writeFileSync(o,JSON.stringify(e,null,2)+`
5
- `,{encoding:"utf-8",mode:384}),chmodSync(o,384);}function k(){let e=It();try{unlinkSync(e);}catch(t){if(t.code!=="ENOENT")throw t}}async function Ge(e){let t=true;try{await e.clearLastSession();}catch(o){t=false,e.logger.error(`Failed to clear cached session: ${o instanceof Error?o.message:String(o)}`);}return t?e.logger.error("Session expired (room no longer exists). Cached session cleared."):e.logger.error("Session expired. Could not delete ~/.config/hnch/last-session.json \u2014 please remove it manually."),e.exit(1)}function Qe(e){let t=e.doc.getMap("meta"),o=null,n=false;function r(){if(n)return;let s=t.get("sessionEndedAt");if(s==null||s===o)return;o=s;let u=t.get("createdBy"),i=u===e.localParticipantId,p=e.doc.getMap("participants"),c=e.doc.getMap("settings").get("hostParticipantId")??u,a="host";if(c){let S=p.get(c)?.get("displayName");typeof S=="string"&&S.length>0&&(a=S);}setImmediate(()=>{n||e.onEnded({isLocal:i,hostName:a,timestamp:s});});}return t.observe(r),r(),()=>{n=true,t.unobserve(r);}}var Vn="@xauyxau/hnch";function ze(e){return so(e)}function Ke(e){return `${process.env.HNCH_SHARE_ORIGIN??"https://hnch.dev"}/join?code=${e}`}function Xe(e){return `npx ${Vn} join ${e}`}var Jn={connecting:"yellow",connected:"green",disconnected:"red"};function yo({roomCode:e,sessionName:t,status:o,isHost:n}){let r=t?`${e} \xB7 ${t}`:e;return jsxs(Box,{borderStyle:"single",borderColor:"gray",paddingX:1,justifyContent:"space-between",children:[jsxs(Text,{bold:true,children:["hnch \xB7 ",r]}),jsxs(Box,{gap:2,children:[n&&jsx(Text,{color:"yellow",children:"[host]"}),jsx(Text,{color:Jn[o],children:o})]})]})}function ne({items:e,onSelect:t,onClose:o,title:n}){if(e.length===0)return jsxs(Box,{flexDirection:"column",gap:1,children:[n&&jsx(Text,{bold:true,children:n}),jsx(Text,{dimColor:true,children:"(no items)"})]});let r=e.findIndex(i=>!i.disabled),[s,u]=useState(r>=0?r:0);return useInput((i,p)=>{if(p.upArrow)u(c=>{let a=(c-1+e.length)%e.length,l=0;for(;e[a]?.disabled&&l<e.length;)a=(a-1+e.length)%e.length,l++;return a});else if(p.downArrow)u(c=>{let a=(c+1)%e.length,l=0;for(;e[a]?.disabled&&l<e.length;)a=(a+1)%e.length,l++;return a});else if(p.return){let c=e[s];c&&!c.disabled&&t(c.value);}else p.escape&&o?.();}),jsxs(Box,{flexDirection:"column",gap:1,children:[n&&jsx(Text,{bold:true,children:n}),jsx(Box,{flexDirection:"column",children:e.map((i,p)=>{let c=p===s,a=i.disabled??false;return jsxs(Box,{gap:1,children:[jsx(Text,{color:c?"cyan":void 0,dimColor:a,children:c?">":" "}),jsx(Text,{bold:c,dimColor:a,children:i.label}),i.detail&&jsx(Text,{dimColor:true,children:i.detail})]},i.value)})}),jsx(Text,{dimColor:true,children:"\u2191\u2193 select \u23CE confirm esc close"})]})}function W({value:e,onChange:t,onSubmit:o,onCancel:n,placeholder:r,isActive:s=true,mask:u=false}){useInput((a,l)=>{l.escape?n?.():l.return?o(e):l.backspace||l.delete?t(e.slice(0,-1)):a&&!l.ctrl&&!l.meta&&t(e+a);},{isActive:s});let i=u&&e.length>0?"*".repeat(e.length):e,p=i.length>0?i:r??"",c=e.length===0&&r!==void 0;return jsxs(Text,{children:[jsx(Text,{dimColor:c,children:p}),jsx(Text,{inverse:true,children:" "})]})}function bo({mode:e,password:t,onModeChange:o,onPasswordChange:n,onClose:r}){let[s,u]=useState(e==="open"?0:1),[i,p]=useState(false);return useEffect(()=>{u(e==="open"?0:1);},[e]),useInput((c,a)=>{if(a.leftArrow)u(l=>Math.max(0,l-1));else if(a.rightArrow)u(l=>Math.min(1,l+1));else if(a.return){let l=s===0?"open":"password";l!==e&&o(l),l==="password"&&p(true);}else a.escape&&r?.();},{isActive:!i}),jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Box,{gap:1,children:["open","password"].map((c,a)=>{let l=a===s,S=c===e,E=c==="open"?"Open":"Password";return jsx(Text,{inverse:l,bold:S,color:S?"cyan":void 0,children:S?`[${E}]`:` ${E} `},c)})}),e==="password"&&jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{children:"Password: "}),jsx(W,{value:t,onChange:n,onSubmit:()=>p(false),onCancel:()=>p(false),placeholder:"(required to join)",isActive:i,mask:true}),!i&&jsx(Text,{dimColor:true,children:"\u23CE edit \u2190 \u2192 switch mode esc close"}),i&&jsx(Text,{dimColor:true,children:"\u23CE done esc cancel"})]}),e==="open"&&jsx(Text,{dimColor:true,children:"\u2190\u2192 toggle \u23CE confirm esc close"})]})}function To({scaleId:e,cards:t,disabledIndices:o,onOverrideChange:n,onClose:r}){let s=useMemo(()=>new Set(o),[o]),[u,i]=useState(0),p=c=>{let a=new Set(s);if(a.has(c))a.delete(c);else {if(a.size>=t.length-1)return;a.add(c);}let l=Array.from(a).sort((S,E)=>S-E);n(e,l);};return useInput((c,a)=>{a.leftArrow?i(l=>Math.max(0,l-1)):a.rightArrow?i(l=>Math.min(t.length-1,l+1)):c===" "?p(u):(a.return||a.escape)&&r();}),jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Box,{gap:1,children:t.map((c,a)=>{let l=a===u,S=s.has(a);return jsx(Text,{inverse:l,bold:!S,color:S?void 0:"cyan",strikethrough:S,dimColor:S,children:c},c)})}),jsx(Text,{dimColor:true,children:" \u2190\u2192 navigate space toggle \u23CE/esc done"})]})}function Mt({initialName:e="",initialCards:t=[],onSave:o,onCancel:n}){let r=e.length>0,[s,u]=useState(e),[i,p]=useState(t.length>0?t:[""]),[c,a]=useState(0),[l,S]=useState("editing-name"),[E,N]=useState(""),R=()=>{let d=s.trim();if(d.length<1||d.length>40){N("Name must be 1-40 characters");return}u(d),N(""),S("editing-cards");},I=()=>{let d=s.trim();if(d.length<1||d.length>40){N("Name must be 1-40 characters");return}let g=i.map(h=>h.trim()).filter(h=>h.length>0),w=Array.from(new Set(g));if(w.length<2){N("Need at least 2 unique non-empty cards");return}N(""),o(d,w);};return useInput((d,g)=>{if(l==="editing-cards"){if(g.leftArrow)a(w=>Math.max(0,w-1));else if(g.rightArrow)a(w=>{let h=w+1;return h>=i.length&&i.length<25?(p(M=>[...M,""]),h):Math.min(h,i.length-1)});else if(g.return)I();else if(g.escape)n();else if(g.tab)S("editing-name");else if(g.backspace){let w=i[c]||"";if(w.length===0){if(i.length>2){let h=i.filter((M,f)=>f!==c);p(h),a(Math.max(0,c-1));}}else {let h=[...i];h[c]=w.slice(0,-1),p(h);}}else if(d&&!g.ctrl&&!g.meta){let w=i[c]||"";if(w.length<10){let h=[...i];h[c]=w+d,p(h);}}}},{isActive:l==="editing-cards"}),jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:r?"\u2500\u2500 Edit Custom Scale \u2500\u2500":"\u2500\u2500 Create Custom Scale \u2500\u2500"}),jsxs(Box,{flexDirection:"column",gap:0,children:[jsx(Text,{children:"Name:"}),jsx(W,{value:s,onChange:u,onSubmit:R,onCancel:n,placeholder:"Scale name",isActive:l==="editing-name"})]}),l==="editing-cards"&&jsxs(Box,{flexDirection:"column",gap:0,children:[jsx(Text,{children:"Cards:"}),jsx(Box,{gap:1,flexWrap:"wrap",children:i.map((d,g)=>{let w=g===c;return jsx(Text,{inverse:w,color:w?void 0:"cyan",children:d.length>0?d:"\xB7"},g)})})]}),E&&jsx(Text,{color:"red",children:E}),jsx(Text,{dimColor:true,children:l==="editing-name"?"\u23CE next esc cancel":"\u2190\u2192 navigate typing adds card bsp delete \u23CE save tab name esc cancel"})]})}function tt({scales:e,selectedScaleId:t,presetOverrides:o,customScales:n=[],onSelect:r,onOverrideChange:s,onCustomScaleCreate:u,onCustomScaleEdit:i,onCustomScaleDelete:p,onClose:c,showNoneOption:a=false}){let[l,S]=useState(0),[E,N]=useState(null),[R,I]=useState("browsing"),[H,d]=useState(null),[g,w]=useState(null),h=u?[...e,{id:"__create-custom__",cards:[]}]:e,M=h.length;if(useInput((f,P)=>{if(R==="browsing"){if(P.escape){c();return}if(M===0)return;if(P.upArrow)S(y=>(y-1+M)%M);else if(P.downArrow)S(y=>(y+1)%M);else if(P.return){let y=h[l];if(!y)return;if(y.id==="__create-custom__"){I("creating");return}y.id in X?N(E===y.id?null:y.id):r(y.id);}else if(f===" "){let y=h[l];y&&y.id!=="__create-custom__"&&r(y.id);}else if(f==="e"||f==="E"){let y=h[l];i&&y?.id.startsWith("custom-")&&(d(y.id),I("editing"));}else if(f==="d"||f==="D"){let y=h[l];p&&y?.id.startsWith("custom-")&&(w(y.id),I("confirming-delete"));}}else R==="confirming-delete"&&(f==="y"||f==="Y"?(g&&p&&p(g),w(null),I("browsing")):(f==="n"||f==="N"||P.escape)&&(w(null),I("browsing")));},{isActive:R==="browsing"&&E===null||R==="confirming-delete"}),R==="creating")return jsx(Mt,{onSave:(f,P)=>{u&&u(f,P),I("browsing");},onCancel:()=>I("browsing")});if(R==="editing"&&H){let f=n?.find(y=>y.id===H);if(e.find(y=>y.id===H)&&f)return jsx(Mt,{initialName:f.label,initialCards:f.cards,onSave:(y,B)=>{i&&i(H,y,B),d(null),I("browsing");},onCancel:()=>{d(null),I("browsing");}})}if(R==="confirming-delete"&&g){let f=h.find(y=>y.id===g),P=f?pe(f.id):"Unknown";return jsxs(Box,{flexDirection:"column",gap:1,children:[jsxs(Text,{children:["Delete ",P,"? "]}),jsx(Text,{children:"[y] yes [n] no esc cancel"})]})}return h.length===0?jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{dimColor:true,children:"(no scales available)"}),jsx(Text,{dimColor:true,children:"esc back"})]}):jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Box,{flexDirection:"column",children:h.map((f,P)=>{let y=P===l,B=f.id===t,J=E===f.id,V=f.id in X;f.id.startsWith("custom-");let U=f.id==="__create-custom__",x=f.cards.join(", "),C=a&&f.id==="",T=C?"(none)":U?"[+ Create custom scale]":pe(f.id);return jsxs(Box,{flexDirection:"column",children:[jsxs(Box,{gap:1,children:[jsx(Text,{color:y?"cyan":void 0,children:y?">":" "}),jsx(Text,{bold:y,children:T}),!C&&!U&&jsx(Text,{dimColor:true,children:x}),B&&jsx(Text,{color:"cyan",children:"[current]"})]}),J&&!C&&!U&&jsx(Box,{paddingLeft:2,children:V?jsx(To,{scaleId:f.id,cards:X[f.id]?.cards??[],disabledIndices:o?.[f.id]??[],onOverrideChange:s,onClose:()=>N(null)}):null})]},f.id||"none")})}),jsx(Text,{dimColor:true,children:(()=>{let f=l<h.length?h[l]:null;return f?.id.startsWith("custom-")?`\u2191\u2193 navigate space select${i?" [e] edit":""}${p?" [d] delete":""} esc back`:f&&f.id in X?"\u2191\u2193 navigate \u23CE customize space select esc back":"\u2191\u2193 navigate \u23CE/space select esc back"})()})]})}function sr(e,t){return t?[...e]:e.filter(o=>o.value==="display-name")}function Eo({sessionName:e,displayName:t,currentScale:o,accessMode:n,presetOverrides:r,customScales:s,isHost:u=true,onChangeName:i,onChangeScale:p,onChangeDisplayName:c,onChangeAccess:a,onClose:l}){let[S,E]=useState(r??{}),N=useMemo(()=>Je(S,s),[S,s]),[R,I]=useState("main-menu"),[H,d]=useState(e??""),[g,w]=useState(t),[h,M]=useState(n==="password"?"password":"open"),[f,P]=useState(""),[y,B]=useState(false),J=[{label:"Room name",value:"room-name",detail:`[${e??"(none)"}]`},{label:"Scale",value:"scale",detail:`[${pe(o.id)}]`},{label:"Access",value:"access",detail:`[${n==="open"?"Open":"Password"}]`},{label:"Display name",value:"display-name",detail:`[${t}]`}],V=sr(J,u);function G(m){!u&&m!=="display-name"||(m==="room-name"?(d(e??""),I("editing-room-name")):m==="scale"?I("picking-scale"):m==="access"?I("picking-access"):m==="display-name"&&(w(t),I("editing-display-name")));}function U(m){let O=m.trim()===""?void 0:m.trim();i(O),I("main-menu");}function x(m){let O=m.trim();O.length>0&&(c(O),process.nextTick(()=>$({displayName:O}))),I("main-menu");}function C(m){let O=N.find(K=>K.id===m);O&&p(O),I("main-menu");}function T(m,O){let K={...S};O.length===0?delete K[m]:K[m]=O,E(K),$({presetOverrides:Object.keys(K).length>0?K:void 0});}async function _(m,O){B(true);try{await a(m,O),M(m),P(m==="password"?O??"":"");}catch(K){console.error(`Failed to update access: ${K}`);}finally{B(false),I("main-menu");}}return R==="main-menu"?jsx(ne,{title:"\u2500\u2500 Settings \u2500\u2500",items:V,onSelect:G,onClose:l}):R==="editing-room-name"?jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Settings \u2500\u2500"}),jsx(Text,{children:"Room name: "}),jsx(W,{value:H,onChange:d,onSubmit:U,onCancel:()=>I("main-menu"),placeholder:"(leave empty to remove)",isActive:true}),jsx(Text,{dimColor:true,children:"\u23CE confirm esc back"})]}):R==="editing-display-name"?jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Settings \u2500\u2500"}),jsx(Text,{children:"Display name: "}),jsx(W,{value:g,onChange:w,onSubmit:x,onCancel:()=>I("main-menu"),placeholder:"(required)",isActive:true}),jsx(Text,{dimColor:true,children:"\u23CE confirm esc back"})]}):R==="picking-scale"?jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Settings \u2500\u2500"}),jsx(tt,{scales:N,selectedScaleId:o.id,presetOverrides:S,onSelect:C,onOverrideChange:T,onClose:()=>I("main-menu")})]}):R==="picking-access"?jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Settings \u2500\u2500"}),y&&jsx(Text,{dimColor:true,children:"Updating access..."}),!y&&jsx(bo,{mode:h,password:f,onModeChange:m=>{m==="open"&&_("open");},onPasswordChange:P,onClose:()=>I("main-menu")})]}):jsx(Box,{})}function st({participants:e,onlineIds:t,hostId:o,votes:n,revealed:r}){return jsxs(Box,{flexDirection:"column",children:[jsxs(Text,{bold:true,children:["Participants (",e.length,")"]}),e.map(s=>{let u=t.has(s.id),i=u?"\u25CF":"\u25CB",p=u?"green":"gray",c=s.id===o,a=ue(s)==="viewer",l=s.id in n,S=a?" (viewer)":r?n[s.id]?` \u2192 ${n[s.id]}`:"":l?" \u2713":" \xB7";return jsxs(Box,{gap:1,children:[jsx(Text,{color:p,children:i}),jsxs(Text,{children:[s.displayName,c?" (host)":""]}),jsx(Text,{color:a?"gray":l?"green":"gray",children:S})]},s.id)})]})}var ir=[{label:"End session for everyone",value:"end-session",detail:"Disconnects all participants. Cannot be undone."}];function No({onPick:e,onClose:t}){return jsx(ne,{title:"Host Actions",items:ir,onSelect:e,onClose:t})}function it({roomCode:e,onPick:t,onClose:o}){let n=[{label:"Copy code",value:"code",detail:ze(e)},{label:"Copy share link",value:"url",detail:Ke(e)},{label:"Copy npx command",value:"npx",detail:Xe(e)}];return jsx(ne,{title:"Share",items:n,onSelect:r=>t(r),onClose:o})}function dr(e){let t=Object.values(e).map(a=>parseFloat(a)).filter(a=>!isNaN(a)).sort((a,l)=>a-l);if(t.length===0)return {average:"-",median:"-",agreement:"-"};let n=t.reduce((a,l)=>a+l,0)/t.length,r=Math.floor(t.length/2),s=t.length%2===0?(t[r-1]+t[r])/2:t[r],u=Object.values(e),i=new Map;for(let a of u)i.set(a,(i.get(a)??0)+1);let p=Math.max(...i.values()),c=u.length>0?Math.round(p/u.length*100):0;return {average:n.toFixed(1),median:s.toFixed(1),agreement:`${c}%`}}function Oo(e,t){let o=Math.floor((t-e)/1e3),n=Math.floor(o/60),r=o%60;return `${n}:${r.toString().padStart(2,"0")}`}function ur(e,t){let o=new Map;for(let u of Object.values(e.votes))o.set(u,(o.get(u)??0)+1);let n=[...o.entries()].sort((u,i)=>i[1]-u[1]||u[0].localeCompare(i[0])).map(([u,i])=>`${u}\xD7${i}`).join(", "),r=e.title?` ${e.title}`:"",s=e.timerStartedAt!==void 0&&e.revealedAt!==void 0?` \u2014 ${Oo(e.timerStartedAt,e.revealedAt)}`:"";return `Round ${t}:${r} \u2014 ${n||"(no votes)"}${s}`}function Do({projection:e,onlineIds:t,isHost:o,participantId:n,rounds:r,currentRound:s,settingsOpen:u,onStartRound:i,onNewRound:p,onRevote:c,onOpenSettings:a,onHandoffHost:l,onEndSession:S,onShare:E,shareOpen:N,onSetShareOpen:R,roomCode:I,confirmingQuit:H,roundTitlePromptActive:d,roundTitleInput:g,onRoundTitleChange:w,onRoundTitleSubmit:h,onRoundTitleCancel:M}){let[f,P]=at.useState("idle"),[y,B]=at.useState(null),[J,V]=at.useState(false),[G,U]=at.useState(false);useEffect(()=>{o||(P("idle"),B(null),V(false),U(false));},[o]),useInput(m=>{if(m==="c"||m==="C"){R(true);return}o&&((m==="e"||m==="E")&&a(),(m==="s"||m==="S")&&i(),(m==="h"||m==="H")&&P("picking"),(m==="m"||m==="M")&&V(true),s?.revealedAt!==void 0&&((m==="n"||m==="N")&&p(),(m==="v"||m==="V")&&c()));},{isActive:!u&&!H&&f==="idle"&&!d&&!J&&!G&&!N}),useInput((m,O)=>{m==="y"||m==="Y"?(S(),U(false)):(m==="n"||m==="N"||O.escape)&&U(false);},{isActive:G});let x=e.participants.filter(m=>m.id!==n&&t.has(m.id));useInput(m=>{if(f==="picking"){let O=parseInt(m,10);!isNaN(O)&&O>=1&&O<=x.length?(B(x[O-1].id),P("confirming")):(m==="Escape"||m==="q"||m==="Q")&&P("idle");}},{isActive:f==="picking"}),useInput(m=>{f==="confirming"&&y&&(m==="y"||m==="Y"?(l(y),P("idle"),B(null)):(m==="n"||m==="N"||m==="Escape")&&(P("idle"),B(null)));},{isActive:f==="confirming"});let C=r.length>0,T=r.filter(m=>m.revealedAt!==void 0),_=s!==null?T.slice(0,-1):T;return J?jsx(No,{onPick:m=>{V(false),m==="end-session"&&U(true);},onClose:()=>V(false)}):N?jsx(it,{roomCode:I,onPick:m=>{R(false),E(m);},onClose:()=>R(false)}):jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:C?"Session":"Waiting for participants..."}),!C&&jsx(Text,{dimColor:true,children:"No rounds started yet."}),jsx(st,{participants:e.participants,onlineIds:t,hostId:e.settings.hostParticipantId,votes:{},revealed:false}),s?.revealedAt!==void 0&&jsx(pr,{projection:e,round:s,rounds:r}),_.length>0&&jsxs(Box,{flexDirection:"column",children:[jsx(Text,{dimColor:true,children:"\u2500\u2500 History \u2500\u2500"}),_.map(m=>{let O=r.indexOf(m)+1;return jsx(Text,{dimColor:true,children:ur(m,O)},m.id)})]}),f==="picking"&&x.length>0&&jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{children:"Hand off to:"}),x.map((m,O)=>jsxs(Text,{children:["[",O+1,"] ",m.displayName]},m.id)),jsx(Text,{dimColor:true,children:"[q] Cancel"})]}),f==="picking"&&x.length===0&&jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{dimColor:true,children:"No other online participants"}),jsx(Text,{dimColor:true,children:"[q] Cancel"})]}),f==="confirming"&&y&&jsx(Box,{flexDirection:"column",children:jsxs(Text,{children:["Hand off to ",e.participants.find(m=>m.id===y)?.displayName,"? [y/n]"]})}),d&&jsxs(Box,{flexDirection:"column",children:[jsxs(Box,{gap:1,children:[jsx(Text,{children:"Round title (optional, Enter to skip):"}),jsx(W,{value:g,onChange:w,onSubmit:h,onCancel:M,placeholder:"Enter to skip",isActive:d})]}),jsx(Text,{dimColor:true,children:"[Esc] Cancel"})]}),G&&jsxs(Box,{children:[jsx(Text,{color:"red",children:"End session for everyone? This cannot be undone. "}),jsx(Text,{dimColor:true,children:"y/n [Esc] Cancel"})]}),f==="idle"&&!d&&!G&&jsxs(Box,{gap:2,children:[o?jsx(Fragment,{children:s?.revealedAt!==void 0?jsxs(Fragment,{children:[jsx(Text,{color:"yellow",children:"[v] Revote"}),jsx(Text,{color:"yellow",children:"[n] Next"}),jsx(Text,{color:"yellow",children:"[h] Handoff"}),jsx(Text,{color:"yellow",children:"[e] Settings"}),jsx(Text,{color:"yellow",children:"[c] Share"}),jsx(Text,{color:"yellow",children:"[m] Menu"})]}):jsxs(Fragment,{children:[jsx(Text,{color:"yellow",children:"[s] Start round"}),jsx(Text,{color:"yellow",children:"[h] Handoff"}),jsx(Text,{color:"yellow",children:"[e] Settings"}),jsx(Text,{color:"yellow",children:"[c] Share"}),jsx(Text,{color:"yellow",children:"[m] Menu"})]})}):jsxs(Fragment,{children:[jsx(Text,{dimColor:true,children:"Waiting for host to start a round..."}),jsx(Text,{children:"[c] Share"})]}),jsx(Text,{children:"[q] Quit"})]})]})}function pr({projection:e,round:t,rounds:o}){let n=o.indexOf(t)+1,r=t.title?` \u2014 ${t.title}`:"",s=dr(t.votes),u=new Map(e.participants.map(c=>[c.id,c.displayName])),i=t.timerStartedAt!==void 0&&t.revealedAt!==void 0?Oo(t.timerStartedAt,t.revealedAt):null,p=Object.values(t.votes)[0];return jsxs(Box,{flexDirection:"column",gap:1,children:[jsxs(Text,{bold:true,children:["Round ",n,r," \u2014 Results"]}),to(t)&&jsxs(Text,{bold:true,color:"green",children:["Consensus! Everyone voted ",p]}),jsxs(Box,{flexDirection:"column",children:[jsxs(Box,{gap:2,children:[jsx(Text,{bold:true,underline:true,children:"Name".padEnd(20)}),jsx(Text,{bold:true,underline:true,children:"Estimate".padEnd(10)})]}),Object.entries(t.votes).map(([c,a])=>jsxs(Box,{gap:2,children:[jsx(Text,{children:(u.get(c)??c).padEnd(20)}),jsx(Text,{color:"cyan",bold:true,children:a.padEnd(10)})]},c)),e.participants.filter(c=>!(c.id in t.votes)).map(c=>jsxs(Box,{gap:2,children:[jsx(Text,{dimColor:true,children:c.displayName.padEnd(20)}),jsx(Text,{dimColor:true,children:"(no vote)".padEnd(10)})]},c.id))]}),jsxs(Box,{gap:2,children:[jsxs(Text,{children:["Avg: ",jsx(Text,{bold:true,children:s.average})]}),jsxs(Text,{children:["Med: ",jsx(Text,{bold:true,children:s.median})]}),jsxs(Text,{children:["Agreement: ",jsx(Text,{bold:true,children:s.agreement})]}),i!==null&&jsxs(Text,{dimColor:true,children:["\u23F1 ",i]})]})]})}function Bo({scale:e,selected:t,onSelect:o,onClear:n,disabled:r}){let[s,u]=useState(0);return useInput((i,p)=>{if(!r)if(p.leftArrow)u(c=>Math.max(0,c-1));else if(p.rightArrow)u(c=>Math.min(e.cards.length-1,c+1));else if(p.return){let c=e.cards[s];c===t?n():o(c);}else (p.backspace||p.delete)&&n();}),jsxs(Box,{flexDirection:"column",gap:0,children:[jsx(Box,{gap:1,children:e.cards.map((i,p)=>{let c=p===s,a=i===t;return jsx(Text,{inverse:c,bold:a,color:a?"cyan":void 0,children:a?`[${i}]`:` ${i} `},i)})}),jsx(Text,{dimColor:true,children:" \u2190\u2192 navigate \u23CE select \u232B clear"})]})}function Sr(e){let t=Math.floor(e/1e3),o=Math.floor(t/60),n=t%60;return `${o}:${n.toString().padStart(2,"0")}`}function Uo({projection:e,round:t,onlineIds:o,isHost:n,myId:r,myRole:s,elapsedMs:u,roomCode:i,shareOpen:p,settingsOpen:c,onVote:a,onClearVote:l,onReveal:S,onToggleRole:E,onShare:N,onSetShareOpen:R,onOpenSettings:I,confirmingQuit:H}){if(useInput(h=>{if(h==="e"||h==="E"){I();return}if(h==="c"||h==="C"){R(true);return}n&&(h==="r"||h==="R")&&S(),(h==="w"||h==="W")&&E();},{isActive:!H&&!c&&!p}),p)return jsx(it,{roomCode:i,onPick:h=>{R(false),N(h);},onClose:()=>R(false)});let d=t.votes[r],g=e.rounds.indexOf(t)+1,w=t.title?` \u2014 ${t.title}`:"";return jsxs(Box,{flexDirection:"column",gap:1,children:[jsxs(Box,{gap:2,children:[jsxs(Text,{bold:true,children:["Round ",g,w]}),u!==void 0&&jsxs(Text,{dimColor:true,children:["\u23F1 ",Sr(u)]})]}),jsxs(Box,{gap:4,children:[jsx(st,{participants:e.participants,onlineIds:o,hostId:e.settings.hostParticipantId,votes:t.votes,revealed:false}),s==="viewer"?jsx(Box,{flexDirection:"column",children:jsx(Text,{dimColor:true,children:"You're observing"})}):jsx(Bo,{scale:t.scale,selected:d,onSelect:a,onClear:l,disabled:H})]}),jsxs(Box,{gap:2,children:[n&&jsx(Text,{color:"yellow",children:"[r] Reveal"}),jsxs(Text,{color:"yellow",children:["[w] ",s==="viewer"?"Vote":"Observe"]}),jsx(Text,{children:"[e] Settings"}),jsx(Text,{children:"[c] Share"}),jsx(Text,{children:"[q] Quit"})]})]})}var Cr=3e3;function Ut(e=Cr){let[t,o]=useState(null),n=useRef(null),r=useCallback(()=>{n.current!==null&&(clearTimeout(n.current),n.current=null);},[]),s=useCallback(i=>{r(),o(i),n.current=setTimeout(()=>{o(null),n.current=null;},e);},[r,e]),u=useCallback(()=>{r(),o(null);},[r]);return useEffect(()=>()=>{r();},[r]),{notice:t,show:s,dismiss:u}}function Ir(e){let t=Z(e);return !t||t.revealedAt!==void 0?"lobby":"voting"}function ut({session:e,participantId:t,displayName:o,accessMode:n,presetOverrides:r,customScales:s}){let{exit:u}=useApp(),[i,p]=useState(e.projection()),[c,a]=useState("connecting"),[l,S]=useState(e.onlineIds()),[E,N]=useState(void 0),[R,I]=useState(false),[H,d]=useState(n==="password"?"password":"open"),[g,w]=useState(o),[h,M]=useState(false),[f,P]=useState(false),[y,B]=useState(""),J=Ut(3e3),[V,G]=useState(false),U=Ut(3e3);useEffect(()=>{let A=e.onChange(()=>{p(e.projection()),S(e.onlineIds());}),Q=e.onStatusChange(a),nn=e.onHostChange(Ue=>{let sn=e.projection().participants.find(cn=>cn.id===Ue),an=Ue===t?"You are now the host":`${sn?.displayName||"Unknown"} is now the host`;J.show(an);}),rn=setInterval(()=>{S(e.onlineIds());let Ue=e.projection(),je=Z(Ue);je?.timerStartedAt!==void 0&&je.revealedAt===void 0?N(Date.now()-je.timerStartedAt):N(void 0);},1e3);return ()=>{A(),Q(),nn(),clearInterval(rn);}},[e,t,J.show]),useInput(A=>{(A==="q"||A==="Q")&&M(true);},{isActive:!R&&!h&&!f&&!V}),useInput((A,Q)=>{A==="y"||A==="Y"?(e.disconnect(),u()):(A==="n"||A==="N"||Q.escape)&&M(false);},{isActive:h});let x=Ir(i),C=Z(i),T=i.settings.hostParticipantId===t,_=i.participants.find(A=>A.id===t),m=ue(_??{}),O=useCallback(()=>{B(""),P(true);},[]),K=useCallback(A=>{P(false),B(""),e.newRound(A.trim()||void 0);},[e]),Fo=useCallback(()=>{P(false),B("");},[]),Yo=useCallback(A=>{e.vote(A);},[e]),Vo=useCallback(()=>{e.unvote();},[e]),Jo=useCallback(()=>{e.reveal();},[e]),Wo=useCallback(()=>{B(""),P(true);},[]),qo=useCallback(()=>{e.revote();},[e]),Go=useCallback(()=>{e.toggleRole();},[e]),Qo=useCallback(A=>{e.updateName(A);},[e]),zo=useCallback(A=>{e.updateScale(A);},[e]),Ko=useCallback(A=>{e.updateDisplayName(A),w(A);},[e]),Xo=useCallback(async(A,Q)=>{await e.updateAccess(A,Q),d(A);},[e]),Zo=useCallback(A=>{try{e.handoffHost(A);}catch(Q){console.error(`Handoff failed: ${Q instanceof Error?Q.message:String(Q)}`);}},[e]),_t=useCallback(()=>{I(true);},[]),en=useCallback(()=>{I(false);},[]),tn=useCallback(()=>{e.endSessionForEveryone();},[e]),$t=useCallback(async A=>{let Q=A==="code"?ze(e.roomCode):A==="url"?Ke(e.roomCode):Xe(e.roomCode);try{await Rr.write(Q),U.show("Copied!");}catch{U.show(`Copy failed \u2014 ${Q}`);}},[e.roomCode,U]),ft=i.rounds.filter(A=>A.revealedAt!==void 0),on=ft.length>0?ft[ft.length-1]:null;return jsxs(Box,{flexDirection:"column",children:[jsx(yo,{roomCode:e.roomCode,sessionName:i.settings.name,status:c,isHost:T}),J.notice&&jsx(Box,{paddingX:1,children:jsx(Text,{color:"yellow",children:J.notice})}),U.notice&&jsx(Box,{paddingX:1,children:jsx(Text,{color:U.notice.startsWith("Copy failed")?"red":"green",children:U.notice})}),R?jsx(Eo,{sessionName:i.settings.name,displayName:g,currentScale:i.settings.defaultScale,accessMode:H,presetOverrides:r,customScales:s,isHost:T,onChangeName:Qo,onChangeScale:zo,onChangeDisplayName:Ko,onChangeAccess:Xo,onClose:en}):x==="lobby"?jsx(Do,{projection:i,onlineIds:l,isHost:T,participantId:t,rounds:i.rounds,currentRound:on,settingsOpen:R,onStartRound:O,onNewRound:Wo,roundTitlePromptActive:f,roundTitleInput:y,onRoundTitleChange:B,onRoundTitleSubmit:K,onRoundTitleCancel:Fo,onRevote:qo,onOpenSettings:_t,onHandoffHost:Zo,onEndSession:tn,onShare:$t,shareOpen:V,onSetShareOpen:G,roomCode:e.roomCode,confirmingQuit:h}):x==="voting"&&C?jsx(Uo,{projection:i,round:C,onlineIds:l,isHost:T,myId:t,myRole:m,elapsedMs:E,onVote:Yo,onClearVote:Vo,onReveal:Jo,onToggleRole:Go,onShare:$t,onSetShareOpen:G,shareOpen:V,settingsOpen:R,onOpenSettings:_t,roomCode:e.roomCode,confirmingQuit:h}):null,h&&jsx(Box,{children:jsx(Text,{dimColor:true,children:"Leave session? y/n"})})]})}function Lo({presetOverrides:e,customScales:t}){let{exit:o}=useApp(),[n,r]=useState("main-menu"),[s,u]=useState(de()),[i,p]=useState(s.displayName??""),[c,a]=useState(s.relayUrl??""),[l,S]=useState(s.defaultSessionName??""),[E,N]=useState(t??[]),[R,I]=useState(e??{});useInput(x=>{(x==="q"||x==="Q")&&o();},{isActive:n==="main-menu"});let H=useMemo(()=>Je(R,E),[R,E]),d=s.defaultScale,g=d?H.find(x=>x.id===d):void 0,w=[{label:"Display name",value:"display-name",detail:`[${s.displayName??"(none)"}]`},{label:"Relay URL",value:"relay-url",detail:`[${s.relayUrl??"(none)"}]`},{label:"Default scale",value:"default-scale",detail:`[${g?pe(g.id):"(none)"}]`},{label:"Default session name",value:"default-session-name",detail:`[${s.defaultSessionName??"(none)"}]`},{label:"Default password",value:"default-password",detail:`[${s.defaultPassword?"on":"off"}]`}];function h(x){x==="display-name"?(p(s.displayName??""),r("editing-display-name")):x==="relay-url"?(a(s.relayUrl??""),r("editing-relay-url")):x==="default-scale"?r("picking-scale"):x==="default-session-name"?(S(s.defaultSessionName??""),r("editing-session-name")):x==="default-password"&&r("picking-default-password");}function M(x){let C=x.trim();if(C.length>0){let T={...s,displayName:C};u(T),$({displayName:C});}r("main-menu");}function f(x){let C=x.trim();if(C.length>0){let T={...s,relayUrl:C};u(T),$({relayUrl:C});}r("main-menu");}function P(x){let C=x.trim(),T=C.length>0?{...s,defaultSessionName:C}:{...s};C.length===0?(delete T.defaultSessionName,u(T),Ee({defaultSessionName:""})):(u(T),$({defaultSessionName:C})),r("main-menu");}function y(x){let C=x.length>0?{...s,defaultScale:x}:{...s};x.length===0?(delete C.defaultScale,u(C),Ee({defaultScale:""})):(u(C),$({defaultScale:x})),r("main-menu");}function B(x,C){let T={...R};C.length===0?delete T[x]:T[x]=C,I(T),$({presetOverrides:Object.keys(T).length>0?T:void 0});}function J(x){let C=x==="on",T={...s,defaultPassword:C};u(T),$({defaultPassword:C}),r("main-menu");}function V(x,C){let T=`custom-${randomUUID()}`,_=[...E,{id:T,label:x,cards:C}];N(_),u({...s,customScales:_}),$({customScales:_});}function G(x,C,T){let _=E.map(m=>m.id===x?{id:x,label:C,cards:T}:m);N(_),u({...s,customScales:_}),$({customScales:_});}function U(x){let C=E.filter(_=>_.id!==x);N(C);let T={...s,customScales:C};s.defaultScale===x?(T.defaultScale="fibonacci",$({customScales:C,defaultScale:"fibonacci"})):$({customScales:C}),u(T);}if(n==="main-menu")return jsx(ne,{title:"\u2500\u2500 Config Settings \u2500\u2500",items:w,onSelect:h,onClose:()=>o()});if(n==="editing-display-name")return jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Config Settings \u2500\u2500"}),jsx(Text,{children:"Display name: "}),jsx(W,{value:i,onChange:p,onSubmit:M,onCancel:()=>r("main-menu"),placeholder:"(required)",isActive:true}),jsx(Text,{dimColor:true,children:"\u23CE confirm esc back"})]});if(n==="editing-relay-url")return jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Config Settings \u2500\u2500"}),jsx(Text,{children:"Relay URL: "}),jsx(W,{value:c,onChange:a,onSubmit:f,onCancel:()=>r("main-menu"),placeholder:"(required)",isActive:true}),jsx(Text,{dimColor:true,children:"\u23CE confirm esc back"})]});if(n==="editing-session-name")return jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Config Settings \u2500\u2500"}),jsx(Text,{children:"Default session name: "}),jsx(W,{value:l,onChange:S,onSubmit:P,onCancel:()=>r("main-menu"),placeholder:"(leave empty to clear)",isActive:true}),jsx(Text,{dimColor:true,children:"\u23CE confirm esc back"})]});if(n==="picking-scale"){let x=[{id:"",cards:[]},...H];return jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Config Settings \u2500\u2500"}),jsx(tt,{scales:x,selectedScaleId:d??"",presetOverrides:R,customScales:E,onSelect:y,onOverrideChange:B,onCustomScaleCreate:V,onCustomScaleEdit:G,onCustomScaleDelete:U,onClose:()=>r("main-menu"),showNoneOption:true})]})}if(n==="picking-default-password"){let x=[{label:"On",value:"on",detail:s.defaultPassword?"[current]":void 0},{label:"Off",value:"off",detail:s.defaultPassword?void 0:"[current]"}];return jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Config Settings \u2500\u2500"}),jsx(ne,{title:"Default password:",items:x,onSelect:J,onClose:()=>r("main-menu")})]})}return jsx(Box,{})}async function kt(){return new Promise(e=>{let t=createInterface({input:process.stdin,output:process.stderr});process.stderr.write("Password: "),t.question("",o=>{t.close(),e(o);});})}async function Hr(e){let t=await Te(),o=e.scale??t.defaultScale??"fibonacci",n=e.name??t.defaultSessionName,r=e.password??t.defaultPassword??false,s;r&&(s=await kt());let u=Ve(o,t.presetOverrides,t.customScales);if(!u){let l=Object.keys(X).join(", ");console.error(`Unknown or fully-disabled scale: "${o}". Builtin IDs: ${l}`),process.exit(1);}let i=await po({participantId:t.participantId,displayName:t.displayName,relayUrl:t.relayUrl,sessionName:n,password:s,defaultScale:u}),p;s&&(p=await me(s,i.roomUuid)),qe({roomCode:i.roomCode,roomUuid:i.roomUuid,passwordHash:p,lastSeenAt:Date.now()}),console.error(`Room created: ${i.roomCode}`),console.error(`Share: hnch join ${i.roomCode}`),console.error("");let{waitUntilExit:c,unmount:a}=render(at.createElement(ut,{session:i,participantId:t.participantId,displayName:t.displayName,accessMode:s?"password":"open",presetOverrides:t.presetOverrides,customScales:t.customScales}));Qe({doc:i.doc,localParticipantId:t.participantId,onEnded:async l=>{console.error(l.isLocal?"Session ended.":`Session ended by ${l.hostName}.`),a();try{await k();}catch(S){console.error(`Failed to clear cached session: ${S instanceof Error?S.message:String(S)}`);}i.disconnect(),process.exit(0);}}),await c(),i.disconnect(),k(),process.exit(0);}async function Br(e){let t=await Te(),o=fo(e.code);o===null&&(console.error("invalid room code"),process.exit(1));let n;e.password&&(n=await kt());let r=await Pe(t.relayUrl,o),s;r.accessMode==="password"&&!e.password?s=await kt():e.password&&(s=n);let u,i=false,p=await mo({participantId:t.participantId,displayName:t.displayName,relayUrl:t.relayUrl,roomCode:o,password:s,onRejection(l){if(l==="unknown-room"){Ge({clearLastSession:k,logger:console,exit:process.exit});return}l==="room-full"&&(i=true,u?.());}}),c;s&&(c=await me(s,p.roomUuid)),qe({roomCode:p.roomCode,roomUuid:p.roomUuid,passwordHash:c,lastSeenAt:Date.now()}),console.error(`Joined room: ${p.roomCode}`),console.error("");let a=render(at.createElement(ut,{session:p,participantId:t.participantId,displayName:t.displayName,accessMode:r.accessMode,presetOverrides:t.presetOverrides,customScales:t.customScales}));u=a.unmount,Qe({doc:p.doc,localParticipantId:t.participantId,onEnded:async l=>{console.error(l.isLocal?"Session ended.":`Session ended by ${l.hostName}.`),u?.();try{await k();}catch(S){console.error(`Failed to clear cached session: ${S instanceof Error?S.message:String(S)}`);}p.disconnect(),process.exit(0);}}),await a.waitUntilExit(),i&&(console.error("This room is full (max 20 participants)."),p.disconnect(),k(),process.exit(1)),p.disconnect(),k(),process.exit(0);}async function Ur(){let e=await Te(),t=ho();t||(console.error("No recent session to resume."),process.exit(0));let o;try{o=await Pe(e.relayUrl,t.roomCode);}catch(a){if((a instanceof Error?a.message:String(a)).includes("Room not found"))return await Ge({clearLastSession:k,logger:console,exit:process.exit});console.error("Could not reach relay \u2014 try again later."),process.exit(0);}o.accessMode==="password"&&!t.passwordHash&&(console.error(`Password changed \u2014 rejoin with: hnch join ${t.roomCode} --password`),k(),process.exit(0)),console.error(`Rejoining room ${t.roomCode}\u2026`),console.error("");let n=new we.Doc,r=new Awareness(n);Ye(n,{participantId:e.participantId,displayName:e.displayName}),r.setLocalStateField("participantId",e.participantId);let s=false,u=false,i,p=We({doc:n,awareness:r,roomCode:t.roomCode,roomUuid:t.roomUuid,participantId:e.participantId,relayUrl:e.relayUrl,passwordHash:t.passwordHash,onRejection(a){if(a==="unknown-room"){Ge({clearLastSession:k,logger:console,exit:process.exit});return}a==="wrong-password"&&(s=true,i?.()),a==="room-full"&&(u=true,i?.());}});qe({roomCode:p.roomCode,roomUuid:p.roomUuid,passwordHash:t.passwordHash,lastSeenAt:Date.now()});let c=render(at.createElement(ut,{session:p,participantId:e.participantId,displayName:e.displayName,accessMode:o.accessMode,presetOverrides:e.presetOverrides,customScales:e.customScales}));i=c.unmount,Qe({doc:p.doc,localParticipantId:e.participantId,onEnded:async a=>{console.error(a.isLocal?"Session ended.":`Session ended by ${a.hostName}.`),i?.();try{await k();}catch(l){console.error(`Failed to clear cached session: ${l instanceof Error?l.message:String(l)}`);}p.disconnect(),process.exit(0);}}),await c.waitUntilExit(),u&&(console.error("This room is full (max 20 participants)."),k(),process.exit(1)),s&&(console.error(`Password changed \u2014 rejoin with: hnch join ${t.roomCode} --password`),k(),process.exit(0)),p.disconnect(),k(),process.exit(0);}async function jr(e){if(!(e.name!==void 0||e.relayUrl!==void 0||e.scale!==void 0||e.sessionName!==void 0||e.password!==void 0)){let n=await Te(),{waitUntilExit:r}=render(at.createElement(Lo,{presetOverrides:n.presetOverrides,customScales:n.customScales}));await r(),process.exit(0);}let o={};if(e.name!==void 0&&(o.displayName=e.name),e.relayUrl!==void 0&&(o.relayUrl=e.relayUrl),e.scale!==void 0){if(e.scale!==""){let n=de();if(!Ve(e.scale,n.presetOverrides,n.customScales)){console.error(`Unknown scale: "${e.scale}". Builtin IDs: ${Object.keys(X).join(", ")}`);return}}o.defaultScale=e.scale;}e.sessionName!==void 0&&(o.defaultSessionName=e.sessionName),e.password!==void 0&&(o.defaultPassword=e.password),Ee(o),console.log("Config updated.");}var $o=Or(hideBin(process.argv)).scriptName("hnch").usage("$0 <command> \u2014 terminal pointing poker").command("create","Create a new room",e=>e.option("name",{type:"string",describe:"Session name"}).option("password",{type:"boolean",describe:"Protect room with a password"}).option("scale",{type:"string",describe:"Estimation scale ID (fibonacci, t-shirt, powers-of-2, linear, risk, or custom-<id>)"}),e=>{Hr(e).catch(t=>{console.error(t instanceof Error?t.message:t),process.exit(1);});}).command("join <code>","Join an existing room",e=>e.positional("code",{type:"string",demandOption:true,describe:"6-character room code"}).option("password",{type:"boolean",describe:"Room requires a password",default:false}),e=>{Br(e).catch(t=>{console.error(t instanceof Error?t.message:t),process.exit(1);});}).command("resume","Rejoin your last session",e=>e,()=>{Ur().catch(e=>{console.error(e instanceof Error?e.message:e),process.exit(1);});}).command("config","View or update CLI config",e=>e.option("name",{type:"string",describe:"Set display name"}).option("relay-url",{type:"string",describe:"Set relay URL"}).option("scale",{type:"string",describe:"Set default estimation scale ID (empty string clears)"}).option("session-name",{type:"string",describe:"Set default session name (empty string clears)"}).option("password",{type:"boolean",describe:"Set default password prompt on (--password) or off (--no-password)"}),e=>{jr(e).catch(t=>{console.error(t instanceof Error?t.message:t),process.exit(1);});}).demandCommand(1,"Specify a command: create, join, resume, or config").strict().help(),Lt=hideBin(process.argv);(Lt.length===0||Lt.length===1&&Lt[0]==="--")&&($o.showHelp(),process.exit(0));$o.parse();
2
+ import Or from'yargs';import {hideBin}from'yargs/helpers';import ct,{useState,useEffect,useCallback,useMemo,useRef}from'react';import {render,useApp,useInput,Box,Text}from'ink';import*as ve from'yjs';import {Awareness}from'y-protocols/awareness';import {mkdirSync,chmodSync,writeFileSync,unlinkSync,existsSync,readFileSync}from'fs';import {homedir}from'os';import {join}from'path';import {randomUUID}from'crypto';import {createInterface}from'readline';import {WebsocketProvider}from'y-websocket';import kn from'ws';import Rr from'clipboardy';import {jsxs,jsx,Fragment}from'react/jsx-runtime';var fn="https://hnch.dev";function Te(){let t=process.env.XDG_CONFIG_HOME||join(homedir(),".config");return join(t,"hnch")}function St(){return join(Te(),"config.json")}function qt(){let e=Te();existsSync(e)||mkdirSync(e,{recursive:true});}function ue(){let e=St();if(!existsSync(e))return {};try{return JSON.parse(readFileSync(e,"utf-8"))}catch{return {}}}function V(e){qt();let o={...ue(),...e};writeFileSync(St(),JSON.stringify(o,null,2)+`
3
+ `,"utf-8");}async function gn(e){let t=createInterface({input:process.stdin,output:process.stderr});return new Promise(o=>{t.question(e,n=>{t.close(),o(n.trim());});})}function hn(e){return e.startsWith("wss://")?"https://"+e.slice(6):e.startsWith("ws://")?"http://"+e.slice(5):e}async function Ee(){let e=ue(),t=false;if(e.participantId||(e.participantId=randomUUID(),t=true),!e.displayName){let n=await gn("Enter your display name: ");e.displayName=n||`user-${e.participantId.slice(0,6)}`,t=true;}e.relayUrl||(e.relayUrl=fn,t=true),t&&V(e);let o=process.env.HNCH_RELAY_URL||e.relayUrl;return o=hn(o),{participantId:e.participantId,displayName:e.displayName,relayUrl:o,defaultScale:e.defaultScale,defaultSessionName:e.defaultSessionName,defaultPassword:e.defaultPassword,presetOverrides:e.presetOverrides,customScales:e.customScales}}function Ae(e){qt();let o={...ue()};for(let[n,r]of Object.entries(e))r===""?delete o[n]:r!==void 0&&(o[n]=r);writeFileSync(St(),JSON.stringify(o,null,2)+`
4
+ `,"utf-8");}var Sn={"unknown-room":4001,"wrong-password":4002,"invalid-invite":4003,"expired-invite":4004,"invite-already-redeemed":4005,"room-full":4006,"malformed-credentials":4010},Gt=new Map(Object.entries(Sn).map(([e,t])=>[t,e]));function pe(e){return e.role??"voter"}var Le={id:"fibonacci",cards:["0","1","2","3","5","8","13","21"]},zt={id:"t-shirt",cards:["XS","S","M","L","XL"]},Qt={id:"powers-of-2",cards:["1","2","4","8","16","32","64"]},Kt={id:"linear",cards:["1","2","3","4","5","6","7","8","9","10"]},Xt={id:"risk",cards:["Low","Medium","High","Critical"]},X={[Le.id]:Le,[zt.id]:zt,[Qt.id]:Qt,[Kt.id]:Kt,[Xt.id]:Xt},Zt={fibonacci:"Fibonacci","t-shirt":"T-shirt","powers-of-2":"Powers of 2",linear:"Linear (1\u201310)",risk:"Risk"};function me(e){return e in Zt?Zt[e]:e.length===0?e:(e.startsWith("custom-")?e.slice(7):e).split("-").map(o=>o.length===0?o:o.charAt(0).toUpperCase()+o.slice(1)).join(" ")}var xn="meta",Ne="settings",wn="participants",yn="rounds",vn=["roomUuid","createdAt","createdBy","sessionEndedAt"],Cn=["name","defaultScale","hostParticipantId"],bn=["displayName","joinedAt","role","leftAt","removedBy"],Rn=["id","title","description","ticketId","parentRoundId","scale","timerStartedAt","revealedAt","createdBy","createdAt"];function _e(e){return e.getMap(xn)}function Fe(e){return e.getMap(Ne)}function Ve(e){return e.getMap(wn)}function xt(e){return e.getArray(yn)}function Ye(e,t){let o={};for(let n of t){let r=e.get(n);r!==void 0&&(o[n]=r);}return o}function In(e){return Ye(_e(e),vn)}function Tn(e){return Ye(Fe(e),Cn)}function En(e){let t=Ve(e),o=[];return t.forEach((n,r)=>{let a=Ye(n,bn);o.push({id:r,...a});}),o.sort((n,r)=>n.joinedAt-r.joinedAt||n.id.localeCompare(r.id)),o}function An(e){let t={};return e.forEach((o,n)=>{t[n]=o;}),t}function Nn(e){let t=Ye(e,Rn),o=e.get("votes"),n=o?An(o):{};return {...t,votes:n}}function Pn(e){let t=xt(e),o=[];return t.forEach(n=>{o.push(Nn(n));}),o}function K(e){return {meta:In(e),settings:Tn(e),participants:En(e),rounds:Pn(e)}}function eo(e,t){if(_e(e).has("roomUuid"))throw new Error("initDoc: doc already seeded");e.transact(()=>{let o=_e(e);o.set("roomUuid",t.roomUuid),o.set("createdAt",t.createdAt),o.set("createdBy",t.participantId);let n=Fe(e);t.sessionName!==void 0&&n.set("name",t.sessionName),n.set("defaultScale",t.defaultScale),n.set("hostParticipantId",t.participantId);let r=new ve.Map;r.set("displayName",t.displayName),r.set("joinedAt",t.createdAt),Ve(e).set(t.participantId,r);});}function Je(e,t){e.transact(()=>{let o=Ve(e),n=o.get(t.participantId),r=n??new ve.Map;r.set("displayName",t.displayName),n===void 0&&r.set("joinedAt",Date.now()),r.delete("leftAt"),r.delete("removedBy"),n===void 0&&o.set(t.participantId,r);});}function Ce(e,t){e.transact(()=>{let o=Fe(e);for(let[n,r]of Object.entries(t))r===void 0?o.delete(n):o.set(n,r);});}function wt(e,t,o){e.transact(()=>{let n=Ve(e),r=n.get(t),a=r??new ve.Map;a.set("displayName",o.displayName),a.set("joinedAt",o.joinedAt),o.role!==void 0&&a.set("role",o.role),r===void 0&&n.set(t,a);});}function yt(e,t){let o=t.scale??Fe(e).get("defaultScale");if(!o)throw new Error("appendRound: no scale (settings.defaultScale missing)");return e.transact(()=>{let n=new ve.Map;n.set("id",t.id),n.set("createdAt",t.createdAt),n.set("createdBy",t.createdBy),n.set("scale",o),t.title!==void 0&&n.set("title",t.title),t.description!==void 0&&n.set("description",t.description),t.ticketId!==void 0&&n.set("ticketId",t.ticketId),t.parentRoundId!==void 0&&n.set("parentRoundId",t.parentRoundId),n.set("timerStartedAt",t.createdAt),n.set("votes",new ve.Map),xt(e).push([n]);}),t.id}function vt(e,t){let o=xt(e);for(let n=0;n<o.length;n+=1){let r=o.get(n);if(r.get("id")===t)return r}}function to(e,t,o,n){let r=vt(e,t);if(!r)throw new Error(`castVote: round ${t} not found`);r.get("revealedAt")===void 0&&e.transact(()=>{r.get("votes").set(o,n);});}function oo(e,t,o){let n=vt(e,t);if(!n)throw new Error(`clearVote: round ${t} not found`);n.get("revealedAt")===void 0&&e.transact(()=>{n.get("votes").delete(o);});}function Ct(e,t,o){let n=vt(e,t);if(!n)throw new Error(`revealRound: round ${t} not found`);e.transact(()=>{n.set("revealedAt",o);});}function no(e){e.transact(()=>{_e(e).set("sessionEndedAt",Date.now());});}function Z(e){let{rounds:t}=e;if(t.length===0)return null;for(let o=t.length-1;o>=0;o-=1){let n=t[o];if(n.revealedAt===void 0)return n}return t[t.length-1]}function ro(e){let t=Object.values(e.votes);return t.length<2?false:t.every(o=>o===t[0])}var so="PBKDF2",On="SHA-256",io=new TextEncoder;function Mn(e){return Array.from(new Uint8Array(e)).map(t=>t.toString(16).padStart(2,"0")).join("")}async function fe(e,t){let o=await crypto.subtle.importKey("raw",io.encode(e),so,false,["deriveBits"]),n=await crypto.subtle.deriveBits({name:so,salt:io.encode(t),iterations:1e5,hash:On},o,256);return Mn(n)}var ao="ABCDEFGHJKMNPQRSTUVWXYZ23456789";if(ao.length!==31)throw new Error(`Room code alphabet must be 31 characters, got ${ao.length}`);function bt(e){return e.trim().replace(/-/g,"").toUpperCase()}function co(e,t=true){let o=bt(e);return !t||o.length!==6?o:`${o.slice(0,3)}-${o.slice(3)}`}function We(e,t,o){if(e.startsWith("custom-")){let r=(o??[]).find(a=>a.id===e);return r?{id:r.id,cards:r.cards}:void 0}let n=X[e];if(n){let r=(t??{})[e]??[];if(r.length===0)return n;let a=n.cards.filter((d,i)=>!r.includes(i));return a.length===0?void 0:{id:e,cards:a}}}function qe(e,t){let o=Object.keys(X).map(r=>We(r,e,t)).filter(r=>r!==void 0),n=(t??[]).map(r=>({id:r.id,cards:r.cards}));return [...o,...n]}async function lo(e,t){let o=await fetch(`${e}/rooms`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({})});if(!o.ok)throw new Error(`POST /rooms failed: ${o.status}`);return await o.json()}async function Pe(e,t){let o=await fetch(`${e}/rooms/${encodeURIComponent(t)}`);if(o.status===404){let n=await o.json().catch(()=>null);throw new Error(`Room not found: ${n?.reason??"unknown-room"}`)}if(!o.ok)throw new Error(`GET /rooms/:code failed: ${o.status}`);return await o.json()}async function Rt(e,t,o){let n=await fetch(`${e}/rooms/${t}/access`,{method:"PATCH",headers:{"content-type":"application/json"},body:JSON.stringify({passwordHash:o})});if(!n.ok)throw new Error(`PATCH /rooms/:uuid/access failed: ${n.status}`)}function uo(e){let o=`${e.wsBaseUrl.replace(/^http/,"ws")}/ws`,n={did:e.deviceId};e.passwordHash!==void 0&&(n.pw=e.passwordHash);let r=new WebsocketProvider(o,e.roomUuid,e.doc,{params:n,connect:true,...e.awareness!==void 0&&{awareness:e.awareness},WebSocketPolyfill:kn}),a=l=>{e.onStatusChange?.(l.status);},d=l=>{if(l===null)return;let s=Gt.get(l.code);s!==void 0&&(e.onRejection?.(s),r.shouldConnect=false,r.disconnect());};r.on("status",a),r.on("connection-close",d);let i=false,u=e.passwordHash;return {doc:e.doc,awareness:r.awareness,updateCredentials(l){if(l.passwordHash===u)return;u=l.passwordHash??void 0,r.shouldConnect=false,r.disconnect();let c={did:r.params.did??""};u!==void 0&&(c.pw=u),r.params=c,r.connect();},disconnect(){i||(i=true,r.off("status",a),r.off("connection-close",d),r.shouldConnect=false,r.disconnect(),r.destroy());}}}var Hn=5e3;function po(e,t,o){let n=null,r=null;function a(){let s=new Set;return t.getStates().forEach(c=>{let g=c.participantId;typeof g=="string"&&s.add(g);}),s}function d(){return e.getMap(Ne).get("hostParticipantId")}function i(){r!==null&&(clearTimeout(r),r=null),n=null;}function u(){let s=d(),c=n;if(n=null,!c||s!==c)return;let g=a();if(g.has(s))return;let E=[...g].sort();if(E.length===0)return;let P=E[0];P===o&&Ce(e,{hostParticipantId:P});}function l(){let s=d();if(!s)return;if(a().has(s)){i();return}n!==s&&(i(),n=s,r=setTimeout(()=>{r=null,u();},Hn));}return t.on("change",l),e.getMap(Ne).observe(l),l(),{stop(){i(),t.off("change",l),e.getMap(Ne).unobserve(l);}}}async function go(e){let t,o=await lo(e.relayUrl);e.password&&(t=await fe(e.password,o.uuid),await Rt(e.relayUrl,o.uuid,t));let n=new ve.Doc,r=new Awareness(n),a=Date.now();return eo(n,{roomUuid:o.uuid,createdAt:a,participantId:e.participantId,displayName:e.displayName,defaultScale:e.defaultScale??Le,sessionName:e.sessionName}),r.setLocalStateField("participantId",e.participantId),Ge({doc:n,awareness:r,roomCode:o.code,roomUuid:o.uuid,participantId:e.participantId,relayUrl:e.relayUrl,passwordHash:t})}async function ho(e){let t=await Pe(e.relayUrl,e.roomCode),o;if(t.accessMode==="password"){if(!e.password)throw new Error("Room requires a password");o=await fe(e.password,t.uuid);}let n=new ve.Doc,r=new Awareness(n);return Je(n,{participantId:e.participantId,displayName:e.displayName}),r.setLocalStateField("participantId",e.participantId),Ge({doc:n,awareness:r,roomCode:e.roomCode,roomUuid:t.uuid,participantId:e.participantId,relayUrl:e.relayUrl,passwordHash:o,onRejection:e.onRejection})}function Ge(e){let{doc:t,awareness:o,participantId:n,relayUrl:r,roomUuid:a}=e,d=new Set,i=new Set,u=new Set,l=uo({wsBaseUrl:e.relayUrl,roomUuid:e.roomUuid,doc:t,deviceId:n,passwordHash:e.passwordHash,awareness:o,onStatusChange(p){d.forEach(f=>f(p));},onRejection(p){console.error(`Connection rejected: ${p}`),e.onRejection?.(p);}}),s=po(t,o,n),c=()=>{i.forEach(p=>p());};t.getMap("meta").observeDeep(c),t.getMap("settings").observeDeep(c),t.getMap("participants").observeDeep(c),t.getArray("rounds").observeDeep(c);function g(){let p=new Set;return o.getStates().forEach(f=>{let y=f.participantId;typeof y=="string"&&p.add(y);}),p}let E=new Set,P=setInterval(()=>{let p=K(t),f=Z(p);if(!f||f.revealedAt!==void 0||E.has(f.id))return;let y=g();if(y.size===0)return;let x=p.participants.filter(h=>y.has(h.id)&&pe(h)==="voter");if(x.length===0)return;x.filter(h=>h.id in f.votes).length>=x.length&&(E.add(f.id),Ct(t,f.id,Date.now()));},250),R=t.getMap("settings").get("hostParticipantId"),T=()=>{let p=t.getMap("settings").get("hostParticipantId");p!==R&&p!==void 0&&u.forEach(f=>f(p)),R=p;};t.getMap("settings").observe(T);let H=false;return {doc:t,awareness:o,roomCode:e.roomCode,roomUuid:e.roomUuid,projection(){return K(t)},current(){return Z(K(t))},isHost(){return K(t).settings.hostParticipantId===n},onlineIds:g,vote(p){let f=Z(K(t));f&&to(t,f.id,n,p);},unvote(){let p=Z(K(t));p&&oo(t,p.id,n);},reveal(){let p=Z(K(t));p&&Ct(t,p.id,Date.now());},newRound(p){let f=randomUUID();return yt(t,{id:f,createdAt:Date.now(),createdBy:n,title:p}),f},revote(){let p=Z(K(t)),f=randomUUID();return yt(t,{id:f,createdAt:Date.now(),createdBy:n,parentRoundId:p?.id,title:p?.title,description:p?.description,ticketId:p?.ticketId}),f},updateName(p){Ce(t,{name:p});},updateScale(p){Ce(t,{defaultScale:p});},updateDisplayName(p){let x=K(t).participants.find(A=>A.id===n)?.joinedAt??Date.now();wt(t,n,{displayName:p,joinedAt:x});},async updateAccess(p,f){let y=null;if(p==="password"){if(!f)throw new Error("Password required for password mode");y=await fe(f,a);}await Rt(r,a,y),l.updateCredentials({passwordHash:y});},handoffHost(p){if(!this.isHost())throw new Error("Only the current host can hand off");if(!g().has(p))throw new Error("Target participant is not online");if(!K(t).participants.some(A=>A.id===p))throw new Error("Target participant does not exist");Ce(t,{hostParticipantId:p});},toggleRole(){let f=K(t).participants.find(h=>h.id===n),x=pe(f??{})==="viewer"?"voter":"viewer",A=f?.joinedAt??Date.now();wt(t,n,{displayName:f?.displayName??"",joinedAt:A,role:x});},endSessionForEveryone(){if(!this.isHost())throw new Error("Only the current host can end the session");no(t);},onChange(p){return i.add(p),()=>i.delete(p)},onStatusChange(p){return d.add(p),()=>d.delete(p)},onHostChange(p){return u.add(p),()=>u.delete(p)},disconnect(){H||(H=true,clearInterval(P),s.stop(),t.getMap("meta").unobserveDeep(c),t.getMap("settings").unobserveDeep(c),t.getMap("settings").unobserve(T),t.getMap("participants").unobserveDeep(c),t.getArray("rounds").unobserveDeep(c),l.disconnect());}}}function So(e){let t=bt(e);return t===""?null:t}var Fn=1440*60*1e3;function Tt(){return join(Te(),"last-session.json")}function Vn(e){if(typeof e!="object"||e===null)return false;let t=e;return typeof t.roomCode=="string"&&typeof t.roomUuid=="string"&&typeof t.lastSeenAt=="number"&&(t.passwordHash===void 0||typeof t.passwordHash=="string")}function wo(){let e=Tt();if(!existsSync(e))return null;try{let t=JSON.parse(readFileSync(e,"utf-8"));return !Vn(t)||Date.now()-t.lastSeenAt>Fn?(L(),null):t}catch{return L(),null}}function ze(e){let t=Te();mkdirSync(t,{recursive:true,mode:448}),chmodSync(t,448);let o=Tt();writeFileSync(o,JSON.stringify(e,null,2)+`
5
+ `,{encoding:"utf-8",mode:384}),chmodSync(o,384);}function L(){let e=Tt();try{unlinkSync(e);}catch(t){if(t.code!=="ENOENT")throw t}}async function Qe(e){let t=true;try{await e.clearLastSession();}catch(o){t=false,e.logger.error(`Failed to clear cached session: ${o instanceof Error?o.message:String(o)}`);}return t?e.logger.error("Session expired (room no longer exists). Cached session cleared."):e.logger.error("Session expired. Could not delete ~/.config/hnch/last-session.json \u2014 please remove it manually."),e.exit(1)}function Ke(e){let t=e.doc.getMap("meta"),o=null,n=false;function r(){if(n)return;let a=t.get("sessionEndedAt");if(a==null||a===o)return;o=a;let d=t.get("createdBy"),i=d===e.localParticipantId,u=e.doc.getMap("participants"),l=e.doc.getMap("settings").get("hostParticipantId")??d,s="host";if(l){let g=u.get(l)?.get("displayName");typeof g=="string"&&g.length>0&&(s=g);}setImmediate(()=>{n||e.onEnded({isLocal:i,hostName:s,timestamp:a});});}return t.observe(r),r(),()=>{n=true,t.unobserve(r);}}var Yn="@xauyxau/hnch";function Xe(e){return co(e)}function Ze(e){return `${process.env.HNCH_SHARE_ORIGIN??"https://hnch.dev"}/join?code=${e}`}function et(e){return `npx ${Yn} join ${e}`}var Jn={connecting:"yellow",connected:"green",disconnected:"red"};function Co({roomCode:e,sessionName:t,status:o,isHost:n}){let r=t?`${e} \xB7 ${t}`:e;return jsxs(Box,{borderStyle:"single",borderColor:"gray",paddingX:1,justifyContent:"space-between",children:[jsxs(Text,{bold:true,children:["hnch \xB7 ",r]}),jsxs(Box,{gap:2,children:[n&&jsx(Text,{color:"yellow",children:"[host]"}),jsx(Text,{color:Jn[o],children:o})]})]})}function re({items:e,onSelect:t,onClose:o,title:n}){if(e.length===0)return jsxs(Box,{flexDirection:"column",gap:1,children:[n&&jsx(Text,{bold:true,children:n}),jsx(Text,{dimColor:true,children:"(no items)"})]});let r=e.findIndex(i=>!i.disabled),[a,d]=useState(r>=0?r:0);return useInput((i,u)=>{if(u.upArrow)d(l=>{let s=(l-1+e.length)%e.length,c=0;for(;e[s]?.disabled&&c<e.length;)s=(s-1+e.length)%e.length,c++;return s});else if(u.downArrow)d(l=>{let s=(l+1)%e.length,c=0;for(;e[s]?.disabled&&c<e.length;)s=(s+1)%e.length,c++;return s});else if(u.return){let l=e[a];l&&!l.disabled&&t(l.value);}else u.escape&&o?.();}),jsxs(Box,{flexDirection:"column",gap:1,children:[n&&jsx(Text,{bold:true,children:n}),jsx(Box,{flexDirection:"column",children:e.map((i,u)=>{let l=u===a,s=i.disabled??false;return jsxs(Box,{gap:1,children:[jsx(Text,{color:l?"cyan":void 0,dimColor:s,children:l?">":" "}),jsx(Text,{bold:l,dimColor:s,children:i.label}),i.detail&&jsx(Text,{dimColor:true,children:i.detail})]},i.value)})}),jsx(Text,{dimColor:true,children:"\u2191\u2193 select \u23CE confirm esc close"})]})}function q({value:e,onChange:t,onSubmit:o,onCancel:n,placeholder:r,isActive:a=true,mask:d=false}){useInput((s,c)=>{c.escape?n?.():c.return?o(e):c.backspace||c.delete?t(e.slice(0,-1)):s&&!c.ctrl&&!c.meta&&t(e+s);},{isActive:a});let i=d&&e.length>0?"*".repeat(e.length):e,u=i.length>0?i:r??"",l=e.length===0&&r!==void 0;return jsxs(Text,{children:[jsx(Text,{dimColor:l,children:u}),jsx(Text,{inverse:true,children:" "})]})}function To({mode:e,password:t,onModeChange:o,onPasswordChange:n,onClose:r}){let[a,d]=useState(e==="open"?0:1),[i,u]=useState(false);return useEffect(()=>{d(e==="open"?0:1);},[e]),useInput((l,s)=>{if(s.leftArrow)d(c=>Math.max(0,c-1));else if(s.rightArrow)d(c=>Math.min(1,c+1));else if(s.return){let c=a===0?"open":"password";c!==e&&o(c),c==="password"&&u(true);}else s.escape&&r?.();},{isActive:!i}),jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Box,{gap:1,children:["open","password"].map((l,s)=>{let c=s===a,g=l===e,E=l==="open"?"Open":"Password";return jsx(Text,{inverse:c,bold:g,color:g?"cyan":void 0,children:g?`[${E}]`:` ${E} `},l)})}),e==="password"&&jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{children:"Password: "}),jsx(q,{value:t,onChange:n,onSubmit:()=>u(false),onCancel:()=>u(false),placeholder:"(required to join)",isActive:i,mask:true}),!i&&jsx(Text,{dimColor:true,children:"\u23CE edit \u2190 \u2192 switch mode esc close"}),i&&jsx(Text,{dimColor:true,children:"\u23CE done esc cancel"})]}),e==="open"&&jsx(Text,{dimColor:true,children:"\u2190\u2192 toggle \u23CE confirm esc close"})]})}function No({scaleId:e,cards:t,disabledIndices:o,onOverrideChange:n,onClose:r}){let a=useMemo(()=>new Set(o),[o]),[d,i]=useState(0),u=l=>{let s=new Set(a);if(s.has(l))s.delete(l);else {if(s.size>=t.length-1)return;s.add(l);}let c=Array.from(s).sort((g,E)=>g-E);n(e,c);};return useInput((l,s)=>{s.leftArrow?i(c=>Math.max(0,c-1)):s.rightArrow?i(c=>Math.min(t.length-1,c+1)):l===" "?u(d):(s.return||s.escape)&&r();}),jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Box,{gap:1,children:t.map((l,s)=>{let c=s===d,g=a.has(s);return jsx(Text,{inverse:c,bold:!g,color:g?void 0:"cyan",strikethrough:g,dimColor:g,children:l},l)})}),jsx(Text,{dimColor:true,children:" \u2190\u2192 navigate space toggle \u23CE/esc done"})]})}function kt({initialName:e="",initialCards:t=[],onSave:o,onCancel:n}){let r=e.length>0,[a,d]=useState(e),[i,u]=useState(t.length>0?t:[""]),[l,s]=useState(0),[c,g]=useState("editing-name"),[E,P]=useState(""),R=()=>{let p=a.trim();if(p.length<1||p.length>40){P("Name must be 1-40 characters");return}d(p),P(""),g("editing-cards");},T=()=>{let p=a.trim();if(p.length<1||p.length>40){P("Name must be 1-40 characters");return}let f=i.map(x=>x.trim()).filter(x=>x.length>0),y=Array.from(new Set(f));if(y.length<2){P("Need at least 2 unique non-empty cards");return}P(""),o(p,y);};return useInput((p,f)=>{if(c==="editing-cards"){if(f.leftArrow)s(y=>Math.max(0,y-1));else if(f.rightArrow)s(y=>{let x=y+1;return x>=i.length&&i.length<25?(u(A=>[...A,""]),x):Math.min(x,i.length-1)});else if(f.return)T();else if(f.escape)n();else if(f.tab)g("editing-name");else if(f.backspace){let y=i[l]||"";if(y.length===0){if(i.length>2){let x=i.filter((A,h)=>h!==l);u(x),s(Math.max(0,l-1));}}else {let x=[...i];x[l]=y.slice(0,-1),u(x);}}else if(p&&!f.ctrl&&!f.meta){let y=i[l]||"";if(y.length<10){let x=[...i];x[l]=y+p,u(x);}}}},{isActive:c==="editing-cards"}),jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:r?"\u2500\u2500 Edit Custom Scale \u2500\u2500":"\u2500\u2500 Create Custom Scale \u2500\u2500"}),jsxs(Box,{flexDirection:"column",gap:0,children:[jsx(Text,{children:"Name:"}),jsx(q,{value:a,onChange:d,onSubmit:R,onCancel:n,placeholder:"Scale name",isActive:c==="editing-name"})]}),c==="editing-cards"&&jsxs(Box,{flexDirection:"column",gap:0,children:[jsx(Text,{children:"Cards:"}),jsx(Box,{gap:1,flexWrap:"wrap",children:i.map((p,f)=>{let y=f===l;return jsx(Text,{inverse:y,color:y?void 0:"cyan",children:p.length>0?p:"\xB7"},f)})})]}),E&&jsx(Text,{color:"red",children:E}),jsx(Text,{dimColor:true,children:c==="editing-name"?"\u23CE next esc cancel":"\u2190\u2192 navigate typing adds card bsp delete \u23CE save tab name esc cancel"})]})}function nt({scales:e,selectedScaleId:t,presetOverrides:o,customScales:n=[],onSelect:r,onOverrideChange:a,onCustomScaleCreate:d,onCustomScaleEdit:i,onCustomScaleDelete:u,onClose:l,showNoneOption:s=false}){let[c,g]=useState(0),[E,P]=useState(null),[R,T]=useState("browsing"),[H,p]=useState(null),[f,y]=useState(null),x=d?[...e,{id:"__create-custom__",cards:[]}]:e,A=x.length;if(useInput((h,D)=>{if(R==="browsing"){if(D.escape){l();return}if(A===0)return;if(D.upArrow)g(w=>(w-1+A)%A);else if(D.downArrow)g(w=>(w+1)%A);else if(D.return){let w=x[c];if(!w)return;if(w.id==="__create-custom__"){T("creating");return}w.id in X?P(E===w.id?null:w.id):r(w.id);}else if(h===" "){let w=x[c];w&&w.id!=="__create-custom__"&&r(w.id);}else if(h==="e"||h==="E"){let w=x[c];i&&w?.id.startsWith("custom-")&&(p(w.id),T("editing"));}else if(h==="d"||h==="D"){let w=x[c];u&&w?.id.startsWith("custom-")&&(y(w.id),T("confirming-delete"));}}else R==="confirming-delete"&&(h==="y"||h==="Y"?(f&&u&&u(f),y(null),T("browsing")):(h==="n"||h==="N"||D.escape)&&(y(null),T("browsing")));},{isActive:R==="browsing"&&E===null||R==="confirming-delete"}),R==="creating")return jsx(kt,{onSave:(h,D)=>{d&&d(h,D),T("browsing");},onCancel:()=>T("browsing")});if(R==="editing"&&H){let h=n?.find(w=>w.id===H);if(e.find(w=>w.id===H)&&h)return jsx(kt,{initialName:h.label,initialCards:h.cards,onSave:(w,k)=>{i&&i(H,w,k),p(null),T("browsing");},onCancel:()=>{p(null),T("browsing");}})}if(R==="confirming-delete"&&f){let h=x.find(w=>w.id===f),D=h?me(h.id):"Unknown";return jsxs(Box,{flexDirection:"column",gap:1,children:[jsxs(Text,{children:["Delete ",D,"? "]}),jsx(Text,{children:"[y] yes [n] no esc cancel"})]})}return x.length===0?jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{dimColor:true,children:"(no scales available)"}),jsx(Text,{dimColor:true,children:"esc back"})]}):jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Box,{flexDirection:"column",children:x.map((h,D)=>{let w=D===c,k=h.id===t,F=E===h.id,W=h.id in X;h.id.startsWith("custom-");let B=h.id==="__create-custom__",S=h.cards.join(", "),C=s&&h.id==="",I=C?"(none)":B?"[+ Create custom scale]":me(h.id);return jsxs(Box,{flexDirection:"column",children:[jsxs(Box,{gap:1,children:[jsx(Text,{color:w?"cyan":void 0,children:w?">":" "}),jsx(Text,{bold:w,children:I}),!C&&!B&&jsx(Text,{dimColor:true,children:S}),k&&jsx(Text,{color:"cyan",children:"[current]"})]}),F&&!C&&!B&&jsx(Box,{paddingLeft:2,children:W?jsx(No,{scaleId:h.id,cards:X[h.id]?.cards??[],disabledIndices:o?.[h.id]??[],onOverrideChange:a,onClose:()=>P(null)}):null})]},h.id||"none")})}),jsx(Text,{dimColor:true,children:(()=>{let h=c<x.length?x[c]:null;return h?.id.startsWith("custom-")?`\u2191\u2193 navigate space select${i?" [e] edit":""}${u?" [d] delete":""} esc back`:h&&h.id in X?"\u2191\u2193 navigate \u23CE customize space select esc back":"\u2191\u2193 navigate \u23CE/space select esc back"})()})]})}function sr(e,t){return t?[...e]:e.filter(o=>o.value==="display-name")}function Po({sessionName:e,displayName:t,currentScale:o,accessMode:n,presetOverrides:r,customScales:a,isHost:d=true,onChangeName:i,onChangeScale:u,onChangeDisplayName:l,onChangeAccess:s,onClose:c}){let[g,E]=useState(r??{}),P=useMemo(()=>qe(g,a),[g,a]),[R,T]=useState("main-menu"),[H,p]=useState(e??""),[f,y]=useState(t),[x,A]=useState(n==="password"?"password":"open"),[h,D]=useState(""),[w,k]=useState(false),F=[{label:"Room name",value:"room-name",detail:`[${e??"(none)"}]`},{label:"Scale",value:"scale",detail:`[${me(o.id)}]`},{label:"Access",value:"access",detail:`[${n==="open"?"Open":"Password"}]`},{label:"Display name",value:"display-name",detail:`[${t}]`}],W=sr(F,d);function oe(O){!d&&O!=="display-name"||(O==="room-name"?(p(e??""),T("editing-room-name")):O==="scale"?T("picking-scale"):O==="access"?T("picking-access"):O==="display-name"&&(y(t),T("editing-display-name")));}function B(O){let U=O.trim()===""?void 0:O.trim();i(U),T("main-menu");}function S(O){let U=O.trim();U.length>0&&(l(U),process.nextTick(()=>V({displayName:U}))),T("main-menu");}function C(O){let U=P.find(m=>m.id===O);U&&u(U),T("main-menu");}function I(O,U){let m={...g};U.length===0?delete m[O]:m[O]=U,E(m),V({presetOverrides:Object.keys(m).length>0?m:void 0});}async function _(O,U){k(true);try{await s(O,U),A(O),D(O==="password"?U??"":"");}catch(m){console.error(`Failed to update access: ${m}`);}finally{k(false),T("main-menu");}}return R==="main-menu"?jsx(re,{title:"\u2500\u2500 Settings \u2500\u2500",items:W,onSelect:oe,onClose:c}):R==="editing-room-name"?jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Settings \u2500\u2500"}),jsx(Text,{children:"Room name: "}),jsx(q,{value:H,onChange:p,onSubmit:B,onCancel:()=>T("main-menu"),placeholder:"(leave empty to remove)",isActive:true}),jsx(Text,{dimColor:true,children:"\u23CE confirm esc back"})]}):R==="editing-display-name"?jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Settings \u2500\u2500"}),jsx(Text,{children:"Display name: "}),jsx(q,{value:f,onChange:y,onSubmit:S,onCancel:()=>T("main-menu"),placeholder:"(required)",isActive:true}),jsx(Text,{dimColor:true,children:"\u23CE confirm esc back"})]}):R==="picking-scale"?jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Settings \u2500\u2500"}),jsx(nt,{scales:P,selectedScaleId:o.id,presetOverrides:g,onSelect:C,onOverrideChange:I,onClose:()=>T("main-menu")})]}):R==="picking-access"?jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Settings \u2500\u2500"}),w&&jsx(Text,{dimColor:true,children:"Updating access..."}),!w&&jsx(To,{mode:x,password:h,onModeChange:O=>{O==="open"&&_("open");},onPasswordChange:D,onClose:()=>T("main-menu")})]}):jsx(Box,{})}function it({participants:e,onlineIds:t,hostId:o,votes:n,revealed:r,participantId:a}){return jsxs(Box,{flexDirection:"column",children:[jsxs(Text,{bold:true,children:["Participants (",e.length,")"]}),e.map(d=>{let i=t.has(d.id),u=i?"\u25CF":"\u25CB",l=i?"green":"gray",s=d.id===o,c=pe(d)==="viewer",g=d.id===a,E=d.id in n,P=c?" (viewer)":r?n[d.id]?` \u2192 ${n[d.id]}`:"":E?" \u2713":" \xB7",R=g&&!c&&!(r&&n[d.id]);return jsxs(Box,{gap:1,children:[jsx(Text,{color:l,children:u}),jsxs(Text,{children:[d.displayName,s?" (host)":""]}),R?jsx(Text,{color:"cyan",children:"(voter)"}):jsx(Text,{color:c?"gray":E?"green":"gray",children:P})]},d.id)})]})}var ir=[{label:"End session for everyone",value:"end-session",detail:"Disconnects all participants. Cannot be undone."}];function Mo({onPick:e,onClose:t}){return jsx(re,{title:"Host Actions",items:ir,onSelect:e,onClose:t})}function at({roomCode:e,onPick:t,onClose:o}){let n=[{label:"Copy code",value:"code",detail:Xe(e)},{label:"Copy share link",value:"url",detail:Ze(e)},{label:"Copy npx command",value:"npx",detail:et(e)}];return jsx(re,{title:"Share",items:n,onSelect:r=>t(r),onClose:o})}function dr(e){let t=Object.values(e).map(s=>parseFloat(s)).filter(s=>!isNaN(s)).sort((s,c)=>s-c);if(t.length===0)return {average:"-",median:"-",agreement:"-"};let n=t.reduce((s,c)=>s+c,0)/t.length,r=Math.floor(t.length/2),a=t.length%2===0?(t[r-1]+t[r])/2:t[r],d=Object.values(e),i=new Map;for(let s of d)i.set(s,(i.get(s)??0)+1);let u=Math.max(...i.values()),l=d.length>0?Math.round(u/d.length*100):0;return {average:n.toFixed(1),median:a.toFixed(1),agreement:`${l}%`}}function Do(e,t){let o=Math.floor((t-e)/1e3),n=Math.floor(o/60),r=o%60;return `${n}:${r.toString().padStart(2,"0")}`}function ur(e,t){let o=new Map;for(let i of Object.values(e.votes))o.set(i,(o.get(i)??0)+1);let n=[...o.entries()].sort((i,u)=>u[1]-i[1]||i[0].localeCompare(u[0])).map(([i,u])=>`${i}\xD7${u}`).join(", "),r=e.title?` ${e.title}`:"",a=e.ticketId?` [${e.ticketId}]`:"",d=e.timerStartedAt!==void 0&&e.revealedAt!==void 0?` \u2014 ${Do(e.timerStartedAt,e.revealedAt)}`:"";return `Round ${t}:${r}${a} \u2014 ${n||"(no votes)"}${d}`}function ko({projection:e,onlineIds:t,isHost:o,participantId:n,rounds:r,currentRound:a,settingsOpen:d,onStartRound:i,onNewRound:u,onRevote:l,onOpenSettings:s,onHandoffHost:c,onEndSession:g,onShare:E,shareOpen:P,onSetShareOpen:R,roomCode:T,confirmingQuit:H,myRole:p,onToggleRole:f,roundTitlePromptActive:y,roundTitleInput:x,onRoundTitleChange:A,onRoundTitleSubmit:h,onRoundTitleCancel:D}){let[w,k]=ct.useState("idle"),[F,W]=ct.useState(null),[oe,B]=ct.useState(false),[S,C]=ct.useState(false);useEffect(()=>{o||(k("idle"),W(null),B(false),C(false));},[o]),useInput(m=>{if(m==="c"||m==="C"){R(true);return}if(m==="w"||m==="W"){f();return}o&&((m==="e"||m==="E")&&s(),(m==="s"||m==="S")&&i(),(m==="h"||m==="H")&&k("picking"),(m==="m"||m==="M")&&B(true),a?.revealedAt!==void 0&&((m==="n"||m==="N")&&u(),(m==="v"||m==="V")&&l()));},{isActive:!d&&!H&&w==="idle"&&!y&&!oe&&!S&&!P}),useInput((m,z)=>{m==="y"||m==="Y"?(g(),C(false)):(m==="n"||m==="N"||z.escape)&&C(false);},{isActive:S});let I=e.participants.filter(m=>m.id!==n&&t.has(m.id));useInput(m=>{if(w==="picking"){let z=parseInt(m,10);!isNaN(z)&&z>=1&&z<=I.length?(W(I[z-1].id),k("confirming")):(m==="Escape"||m==="q"||m==="Q")&&k("idle");}},{isActive:w==="picking"}),useInput(m=>{w==="confirming"&&F&&(m==="y"||m==="Y"?(c(F),k("idle"),W(null)):(m==="n"||m==="N"||m==="Escape")&&(k("idle"),W(null)));},{isActive:w==="confirming"});let _=r.length>0,O=r.filter(m=>m.revealedAt!==void 0),U=a!==null?O.slice(0,-1):O;return oe?jsx(Mo,{onPick:m=>{B(false),m==="end-session"&&C(true);},onClose:()=>B(false)}):P?jsx(at,{roomCode:T,onPick:m=>{R(false),E(m);},onClose:()=>R(false)}):jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:_?"Session":"Waiting for participants..."}),!_&&jsx(Text,{dimColor:true,children:"No rounds started yet."}),jsx(it,{participants:e.participants,onlineIds:t,hostId:e.settings.hostParticipantId,votes:{},revealed:false,participantId:n}),a?.revealedAt!==void 0&&jsx(pr,{projection:e,round:a,rounds:r}),U.length>0&&jsxs(Box,{flexDirection:"column",children:[jsx(Text,{dimColor:true,children:"\u2500\u2500 History \u2500\u2500"}),U.map(m=>{let z=r.indexOf(m)+1;return jsx(Text,{dimColor:true,children:ur(m,z)},m.id)})]}),w==="picking"&&I.length>0&&jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{children:"Hand off to:"}),I.map((m,z)=>jsxs(Text,{children:["[",z+1,"] ",m.displayName]},m.id)),jsx(Text,{dimColor:true,children:"[q] Cancel"})]}),w==="picking"&&I.length===0&&jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{dimColor:true,children:"No other online participants"}),jsx(Text,{dimColor:true,children:"[q] Cancel"})]}),w==="confirming"&&F&&jsx(Box,{flexDirection:"column",children:jsxs(Text,{children:["Hand off to ",e.participants.find(m=>m.id===F)?.displayName,"? [y/n]"]})}),y&&jsxs(Box,{flexDirection:"column",children:[jsxs(Box,{gap:1,children:[jsx(Text,{children:"Round title (optional, Enter to skip):"}),jsx(q,{value:x,onChange:A,onSubmit:h,onCancel:D,placeholder:"Enter to skip",isActive:y})]}),jsx(Text,{dimColor:true,children:"[Esc] Cancel"})]}),S&&jsxs(Box,{children:[jsx(Text,{color:"red",children:"End session for everyone? This cannot be undone. "}),jsx(Text,{dimColor:true,children:"y/n [Esc] Cancel"})]}),w==="idle"&&!y&&!S&&jsxs(Box,{gap:2,children:[o?jsx(Fragment,{children:a?.revealedAt!==void 0?jsxs(Fragment,{children:[jsx(Text,{color:"yellow",children:"[v] Revote"}),jsx(Text,{color:"yellow",children:"[n] Next"}),jsx(Text,{color:"yellow",children:"[h] Handoff"}),jsx(Text,{color:"yellow",children:"[e] Settings"}),jsx(Text,{color:"yellow",children:"[c] Share"}),jsxs(Text,{color:"yellow",children:["[w] ",p==="viewer"?"Vote":"Observe"]}),jsx(Text,{color:"yellow",children:"[m] Menu"})]}):jsxs(Fragment,{children:[jsx(Text,{color:"yellow",children:"[s] Start round"}),jsx(Text,{color:"yellow",children:"[h] Handoff"}),jsx(Text,{color:"yellow",children:"[e] Settings"}),jsx(Text,{color:"yellow",children:"[c] Share"}),jsxs(Text,{color:"yellow",children:["[w] ",p==="viewer"?"Vote":"Observe"]}),jsx(Text,{color:"yellow",children:"[m] Menu"})]})}):jsxs(Fragment,{children:[jsx(Text,{dimColor:true,children:"Waiting for host to start a round..."}),jsx(Text,{children:"[c] Share"}),jsxs(Text,{color:"yellow",children:["[w] ",p==="viewer"?"Vote":"Observe"]})]}),jsx(Text,{children:"[q] Quit"})]})]})}function pr({projection:e,round:t,rounds:o}){let n=o.indexOf(t)+1,r=t.title?` \u2014 ${t.title}`:"",a=t.ticketId?` [${t.ticketId}]`:"",d=dr(t.votes),i=new Map(e.participants.map(s=>[s.id,s.displayName])),u=t.timerStartedAt!==void 0&&t.revealedAt!==void 0?Do(t.timerStartedAt,t.revealedAt):null,l=Object.values(t.votes)[0];return jsxs(Box,{flexDirection:"column",gap:1,children:[jsxs(Text,{bold:true,children:["Round ",n,r,a," \u2014 Results"]}),ro(t)&&jsxs(Text,{bold:true,color:"green",children:["Consensus! Everyone voted ",l]}),jsxs(Box,{flexDirection:"column",children:[jsxs(Box,{gap:2,children:[jsx(Text,{bold:true,underline:true,children:"Name".padEnd(20)}),jsx(Text,{bold:true,underline:true,children:"Estimate".padEnd(10)})]}),Object.entries(t.votes).map(([s,c])=>jsxs(Box,{gap:2,children:[jsx(Text,{children:(i.get(s)??s).padEnd(20)}),jsx(Text,{color:"cyan",bold:true,children:c.padEnd(10)})]},s)),e.participants.filter(s=>!(s.id in t.votes)).map(s=>jsxs(Box,{gap:2,children:[jsx(Text,{dimColor:true,children:s.displayName.padEnd(20)}),jsx(Text,{dimColor:true,children:"(no vote)".padEnd(10)})]},s.id))]}),jsxs(Box,{gap:2,children:[jsxs(Text,{children:["Avg: ",jsx(Text,{bold:true,children:d.average})]}),jsxs(Text,{children:["Med: ",jsx(Text,{bold:true,children:d.median})]}),jsxs(Text,{children:["Agreement: ",jsx(Text,{bold:true,children:d.agreement})]}),u!==null&&jsxs(Text,{dimColor:true,children:["\u23F1 ",u]})]})]})}function Uo({scale:e,selected:t,onSelect:o,onClear:n,disabled:r}){let[a,d]=useState(0);return useInput((i,u)=>{if(!r)if(u.leftArrow)d(l=>Math.max(0,l-1));else if(u.rightArrow)d(l=>Math.min(e.cards.length-1,l+1));else if(u.return){let l=e.cards[a];l===t?n():o(l);}else (u.backspace||u.delete)&&n();}),jsxs(Box,{flexDirection:"column",gap:0,children:[jsx(Box,{gap:1,children:e.cards.map((i,u)=>{let l=u===a,s=i===t;return jsx(Text,{inverse:l,bold:s,color:s?"cyan":void 0,children:s?`[${i}]`:` ${i} `},i)})}),jsx(Text,{dimColor:true,children:" \u2190\u2192 navigate \u23CE select \u232B clear"})]})}function Sr(e){let t=Math.floor(e/1e3),o=Math.floor(t/60),n=t%60;return `${o}:${n.toString().padStart(2,"0")}`}function $o({projection:e,round:t,onlineIds:o,isHost:n,myId:r,myRole:a,elapsedMs:d,roomCode:i,shareOpen:u,settingsOpen:l,onVote:s,onClearVote:c,onReveal:g,onToggleRole:E,onShare:P,onSetShareOpen:R,onOpenSettings:T,confirmingQuit:H}){if(useInput(A=>{if(A==="e"||A==="E"){T();return}if(A==="c"||A==="C"){R(true);return}n&&(A==="r"||A==="R")&&g(),(A==="w"||A==="W")&&E();},{isActive:!H&&!l&&!u}),u)return jsx(at,{roomCode:i,onPick:A=>{R(false),P(A);},onClose:()=>R(false)});let p=t.votes[r],f=e.rounds.indexOf(t)+1,y=t.title?` \u2014 ${t.title}`:"",x=t.ticketId?` [${t.ticketId}]`:"";return jsxs(Box,{flexDirection:"column",gap:1,children:[jsxs(Box,{gap:2,children:[jsxs(Text,{bold:true,children:["Round ",f,y,x]}),d!==void 0&&jsxs(Text,{dimColor:true,children:["\u23F1 ",Sr(d)]})]}),jsxs(Box,{gap:4,children:[jsx(it,{participants:e.participants,onlineIds:o,hostId:e.settings.hostParticipantId,votes:t.votes,revealed:false}),a==="viewer"?jsx(Box,{flexDirection:"column",children:jsx(Text,{dimColor:true,children:"You're observing"})}):jsx(Uo,{scale:t.scale,selected:p,onSelect:s,onClear:c,disabled:H})]}),jsxs(Box,{gap:2,children:[n&&jsx(Text,{color:"yellow",children:"[r] Reveal"}),jsxs(Text,{color:"yellow",children:["[w] ",a==="viewer"?"Vote":"Observe"]}),jsx(Text,{children:"[e] Settings"}),jsx(Text,{children:"[c] Share"}),jsx(Text,{children:"[q] Quit"})]})]})}var vr=3e3;function $t(e=vr){let[t,o]=useState(null),n=useRef(null),r=useCallback(()=>{n.current!==null&&(clearTimeout(n.current),n.current=null);},[]),a=useCallback(i=>{r(),o(i),n.current=setTimeout(()=>{o(null),n.current=null;},e);},[r,e]),d=useCallback(()=>{r(),o(null);},[r]);return useEffect(()=>()=>{r();},[r]),{notice:t,show:a,dismiss:d}}function Ir(e){let t=Z(e);return !t||t.revealedAt!==void 0?"lobby":"voting"}function pt({session:e,participantId:t,displayName:o,accessMode:n,presetOverrides:r,customScales:a}){let{exit:d}=useApp(),[i,u]=useState(e.projection()),[l,s]=useState("connecting"),[c,g]=useState(e.onlineIds()),[E,P]=useState(void 0),[R,T]=useState(false),[H,p]=useState(n==="password"?"password":"open"),[f,y]=useState(o),[x,A]=useState(false),[h,D]=useState(false),[w,k]=useState(""),F=$t(3e3),[W,oe]=useState(false),B=$t(3e3);useEffect(()=>{let N=e.onChange(()=>{u(e.projection()),g(e.onlineIds());}),Q=e.onStatusChange(s),nn=e.onHostChange($e=>{let sn=e.projection().participants.find(cn=>cn.id===$e),an=$e===t?"You are now the host":`${sn?.displayName||"Unknown"} is now the host`;F.show(an);}),rn=setInterval(()=>{g(e.onlineIds());let $e=e.projection(),je=Z($e);je?.timerStartedAt!==void 0&&je.revealedAt===void 0?P(Date.now()-je.timerStartedAt):P(void 0);},1e3);return ()=>{N(),Q(),nn(),clearInterval(rn);}},[e,t,F.show]),useInput(N=>{(N==="q"||N==="Q")&&A(true);},{isActive:!R&&!x&&!h&&!W}),useInput((N,Q)=>{N==="y"||N==="Y"?(e.disconnect(),d()):(N==="n"||N==="N"||Q.escape)&&A(false);},{isActive:x});let S=Ir(i),C=Z(i),I=i.settings.hostParticipantId===t,_=i.participants.find(N=>N.id===t),O=pe(_??{}),U=useCallback(()=>{k(""),D(true);},[]),m=useCallback(N=>{D(false),k(""),e.newRound(N.trim()||void 0);},[e]),z=useCallback(()=>{D(false),k("");},[]),Yo=useCallback(N=>{e.vote(N);},[e]),Jo=useCallback(()=>{e.unvote();},[e]),Wo=useCallback(()=>{e.reveal();},[e]),qo=useCallback(()=>{k(""),D(true);},[]),Go=useCallback(()=>{e.revote();},[e]),Ft=useCallback(()=>{e.toggleRole();},[e]),zo=useCallback(N=>{e.updateName(N);},[e]),Qo=useCallback(N=>{e.updateScale(N);},[e]),Ko=useCallback(N=>{e.updateDisplayName(N),y(N);},[e]),Xo=useCallback(async(N,Q)=>{await e.updateAccess(N,Q),p(N);},[e]),Zo=useCallback(N=>{try{e.handoffHost(N);}catch(Q){console.error(`Handoff failed: ${Q instanceof Error?Q.message:String(Q)}`);}},[e]),Vt=useCallback(()=>{T(true);},[]),en=useCallback(()=>{T(false);},[]),tn=useCallback(()=>{e.endSessionForEveryone();},[e]),Yt=useCallback(async N=>{let Q=N==="code"?Xe(e.roomCode):N==="url"?Ze(e.roomCode):et(e.roomCode);try{await Rr.write(Q),B.show("Copied!");}catch{B.show(`Copy failed \u2014 ${Q}`);}},[e.roomCode,B]),gt=i.rounds.filter(N=>N.revealedAt!==void 0),on=gt.length>0?gt[gt.length-1]:null;return jsxs(Box,{flexDirection:"column",children:[jsx(Co,{roomCode:e.roomCode,sessionName:i.settings.name,status:l,isHost:I}),F.notice&&jsx(Box,{paddingX:1,children:jsx(Text,{color:"yellow",children:F.notice})}),B.notice&&jsx(Box,{paddingX:1,children:jsx(Text,{color:B.notice.startsWith("Copy failed")?"red":"green",children:B.notice})}),R?jsx(Po,{sessionName:i.settings.name,displayName:f,currentScale:i.settings.defaultScale,accessMode:H,presetOverrides:r,customScales:a,isHost:I,onChangeName:zo,onChangeScale:Qo,onChangeDisplayName:Ko,onChangeAccess:Xo,onClose:en}):S==="lobby"?jsx(ko,{projection:i,onlineIds:c,isHost:I,participantId:t,rounds:i.rounds,currentRound:on,settingsOpen:R,onStartRound:U,onNewRound:qo,roundTitlePromptActive:h,roundTitleInput:w,onRoundTitleChange:k,onRoundTitleSubmit:m,onRoundTitleCancel:z,onRevote:Go,onOpenSettings:Vt,onHandoffHost:Zo,onEndSession:tn,onShare:Yt,shareOpen:W,onSetShareOpen:oe,roomCode:e.roomCode,confirmingQuit:x,myRole:O,onToggleRole:Ft}):S==="voting"&&C?jsx($o,{projection:i,round:C,onlineIds:c,isHost:I,myId:t,myRole:O,elapsedMs:E,onVote:Yo,onClearVote:Jo,onReveal:Wo,onToggleRole:Ft,onShare:Yt,onSetShareOpen:oe,shareOpen:W,settingsOpen:R,onOpenSettings:Vt,roomCode:e.roomCode,confirmingQuit:x}):null,x&&jsx(Box,{children:jsx(Text,{dimColor:true,children:"Leave session? y/n"})})]})}function Lo({presetOverrides:e,customScales:t}){let{exit:o}=useApp(),[n,r]=useState("main-menu"),[a,d]=useState(ue()),[i,u]=useState(a.displayName??""),[l,s]=useState(a.relayUrl??""),[c,g]=useState(a.defaultSessionName??""),[E,P]=useState(t??[]),[R,T]=useState(e??{});useInput(S=>{(S==="q"||S==="Q")&&o();},{isActive:n==="main-menu"});let H=useMemo(()=>qe(R,E),[R,E]),p=a.defaultScale,f=p?H.find(S=>S.id===p):void 0,y=[{label:"Display name",value:"display-name",detail:`[${a.displayName??"(none)"}]`},{label:"Relay URL",value:"relay-url",detail:`[${a.relayUrl??"(none)"}]`},{label:"Default scale",value:"default-scale",detail:`[${f?me(f.id):"(none)"}]`},{label:"Default session name",value:"default-session-name",detail:`[${a.defaultSessionName??"(none)"}]`},{label:"Default password",value:"default-password",detail:`[${a.defaultPassword?"on":"off"}]`}];function x(S){S==="display-name"?(u(a.displayName??""),r("editing-display-name")):S==="relay-url"?(s(a.relayUrl??""),r("editing-relay-url")):S==="default-scale"?r("picking-scale"):S==="default-session-name"?(g(a.defaultSessionName??""),r("editing-session-name")):S==="default-password"&&r("picking-default-password");}function A(S){let C=S.trim();if(C.length>0){let I={...a,displayName:C};d(I),V({displayName:C});}r("main-menu");}function h(S){let C=S.trim();if(C.length>0){let I={...a,relayUrl:C};d(I),V({relayUrl:C});}r("main-menu");}function D(S){let C=S.trim(),I=C.length>0?{...a,defaultSessionName:C}:{...a};C.length===0?(delete I.defaultSessionName,d(I),Ae({defaultSessionName:""})):(d(I),V({defaultSessionName:C})),r("main-menu");}function w(S){let C=S.length>0?{...a,defaultScale:S}:{...a};S.length===0?(delete C.defaultScale,d(C),Ae({defaultScale:""})):(d(C),V({defaultScale:S})),r("main-menu");}function k(S,C){let I={...R};C.length===0?delete I[S]:I[S]=C,T(I),V({presetOverrides:Object.keys(I).length>0?I:void 0});}function F(S){let C=S==="on",I={...a,defaultPassword:C};d(I),V({defaultPassword:C}),r("main-menu");}function W(S,C){let I=`custom-${randomUUID()}`,_=[...E,{id:I,label:S,cards:C}];P(_),d({...a,customScales:_}),V({customScales:_});}function oe(S,C,I){let _=E.map(O=>O.id===S?{id:S,label:C,cards:I}:O);P(_),d({...a,customScales:_}),V({customScales:_});}function B(S){let C=E.filter(_=>_.id!==S);P(C);let I={...a,customScales:C};a.defaultScale===S?(I.defaultScale="fibonacci",V({customScales:C,defaultScale:"fibonacci"})):V({customScales:C}),d(I);}if(n==="main-menu")return jsx(re,{title:"\u2500\u2500 Config Settings \u2500\u2500",items:y,onSelect:x,onClose:()=>o()});if(n==="editing-display-name")return jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Config Settings \u2500\u2500"}),jsx(Text,{children:"Display name: "}),jsx(q,{value:i,onChange:u,onSubmit:A,onCancel:()=>r("main-menu"),placeholder:"(required)",isActive:true}),jsx(Text,{dimColor:true,children:"\u23CE confirm esc back"})]});if(n==="editing-relay-url")return jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Config Settings \u2500\u2500"}),jsx(Text,{children:"Relay URL: "}),jsx(q,{value:l,onChange:s,onSubmit:h,onCancel:()=>r("main-menu"),placeholder:"(required)",isActive:true}),jsx(Text,{dimColor:true,children:"\u23CE confirm esc back"})]});if(n==="editing-session-name")return jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Config Settings \u2500\u2500"}),jsx(Text,{children:"Default session name: "}),jsx(q,{value:c,onChange:g,onSubmit:D,onCancel:()=>r("main-menu"),placeholder:"(leave empty to clear)",isActive:true}),jsx(Text,{dimColor:true,children:"\u23CE confirm esc back"})]});if(n==="picking-scale"){let S=[{id:"",cards:[]},...H];return jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Config Settings \u2500\u2500"}),jsx(nt,{scales:S,selectedScaleId:p??"",presetOverrides:R,customScales:E,onSelect:w,onOverrideChange:k,onCustomScaleCreate:W,onCustomScaleEdit:oe,onCustomScaleDelete:B,onClose:()=>r("main-menu"),showNoneOption:true})]})}if(n==="picking-default-password"){let S=[{label:"On",value:"on",detail:a.defaultPassword?"[current]":void 0},{label:"Off",value:"off",detail:a.defaultPassword?void 0:"[current]"}];return jsxs(Box,{flexDirection:"column",gap:1,children:[jsx(Text,{bold:true,children:"\u2500\u2500 Config Settings \u2500\u2500"}),jsx(re,{title:"Default password:",items:S,onSelect:F,onClose:()=>r("main-menu")})]})}return jsx(Box,{})}async function _t(){return new Promise(e=>{let t=createInterface({input:process.stdin,output:process.stderr});process.stderr.write("Password: "),t.question("",o=>{t.close(),e(o);});})}async function kr(e){let t=await Ee(),o=e.scale??t.defaultScale??"fibonacci",n=e.name??t.defaultSessionName,r=e.password??t.defaultPassword??false,a;r&&(a=await _t());let d=We(o,t.presetOverrides,t.customScales);if(!d){let c=Object.keys(X).join(", ");console.error(`Unknown or fully-disabled scale: "${o}". Builtin IDs: ${c}`),process.exit(1);}let i=await go({participantId:t.participantId,displayName:t.displayName,relayUrl:t.relayUrl,sessionName:n,password:a,defaultScale:d}),u;a&&(u=await fe(a,i.roomUuid)),ze({roomCode:i.roomCode,roomUuid:i.roomUuid,passwordHash:u,lastSeenAt:Date.now()}),console.error(`Room created: ${i.roomCode}`),console.error(`Share: hnch join ${i.roomCode}`),console.error("");let{waitUntilExit:l,unmount:s}=render(ct.createElement(pt,{session:i,participantId:t.participantId,displayName:t.displayName,accessMode:a?"password":"open",presetOverrides:t.presetOverrides,customScales:t.customScales}));Ke({doc:i.doc,localParticipantId:t.participantId,onEnded:async c=>{console.error(c.isLocal?"Session ended.":`Session ended by ${c.hostName}.`),s();try{await L();}catch(g){console.error(`Failed to clear cached session: ${g instanceof Error?g.message:String(g)}`);}i.disconnect(),process.exit(0);}}),await l(),i.disconnect(),L(),process.exit(0);}async function Hr(e){let t=await Ee(),o=So(e.code);o===null&&(console.error("invalid room code"),process.exit(1));let n;e.password&&(n=await _t());let r=await Pe(t.relayUrl,o),a;r.accessMode==="password"&&!e.password?a=await _t():e.password&&(a=n);let d,i=false,u=await ho({participantId:t.participantId,displayName:t.displayName,relayUrl:t.relayUrl,roomCode:o,password:a,onRejection(c){if(c==="unknown-room"){Qe({clearLastSession:L,logger:console,exit:process.exit});return}c==="room-full"&&(i=true,d?.());}}),l;a&&(l=await fe(a,u.roomUuid)),ze({roomCode:u.roomCode,roomUuid:u.roomUuid,passwordHash:l,lastSeenAt:Date.now()}),console.error(`Joined room: ${u.roomCode}`),console.error("");let s=render(ct.createElement(pt,{session:u,participantId:t.participantId,displayName:t.displayName,accessMode:r.accessMode,presetOverrides:t.presetOverrides,customScales:t.customScales}));d=s.unmount,Ke({doc:u.doc,localParticipantId:t.participantId,onEnded:async c=>{console.error(c.isLocal?"Session ended.":`Session ended by ${c.hostName}.`),d?.();try{await L();}catch(g){console.error(`Failed to clear cached session: ${g instanceof Error?g.message:String(g)}`);}u.disconnect(),process.exit(0);}}),await s.waitUntilExit(),i&&(console.error("This room is full (max 20 participants)."),u.disconnect(),L(),process.exit(1)),u.disconnect(),L(),process.exit(0);}async function Br(){let e=await Ee(),t=wo();t||(console.error("No recent session to resume."),process.exit(0));let o;try{o=await Pe(e.relayUrl,t.roomCode);}catch(s){if((s instanceof Error?s.message:String(s)).includes("Room not found"))return await Qe({clearLastSession:L,logger:console,exit:process.exit});console.error("Could not reach relay \u2014 try again later."),process.exit(0);}o.accessMode==="password"&&!t.passwordHash&&(console.error(`Password changed \u2014 rejoin with: hnch join ${t.roomCode} --password`),L(),process.exit(0)),console.error(`Rejoining room ${t.roomCode}\u2026`),console.error("");let n=new ve.Doc,r=new Awareness(n);Je(n,{participantId:e.participantId,displayName:e.displayName}),r.setLocalStateField("participantId",e.participantId);let a=false,d=false,i,u=Ge({doc:n,awareness:r,roomCode:t.roomCode,roomUuid:t.roomUuid,participantId:e.participantId,relayUrl:e.relayUrl,passwordHash:t.passwordHash,onRejection(s){if(s==="unknown-room"){Qe({clearLastSession:L,logger:console,exit:process.exit});return}s==="wrong-password"&&(a=true,i?.()),s==="room-full"&&(d=true,i?.());}});ze({roomCode:u.roomCode,roomUuid:u.roomUuid,passwordHash:t.passwordHash,lastSeenAt:Date.now()});let l=render(ct.createElement(pt,{session:u,participantId:e.participantId,displayName:e.displayName,accessMode:o.accessMode,presetOverrides:e.presetOverrides,customScales:e.customScales}));i=l.unmount,Ke({doc:u.doc,localParticipantId:e.participantId,onEnded:async s=>{console.error(s.isLocal?"Session ended.":`Session ended by ${s.hostName}.`),i?.();try{await L();}catch(c){console.error(`Failed to clear cached session: ${c instanceof Error?c.message:String(c)}`);}u.disconnect(),process.exit(0);}}),await l.waitUntilExit(),d&&(console.error("This room is full (max 20 participants)."),L(),process.exit(1)),a&&(console.error(`Password changed \u2014 rejoin with: hnch join ${t.roomCode} --password`),L(),process.exit(0)),u.disconnect(),L(),process.exit(0);}async function Ur(e){if(!(e.name!==void 0||e.relayUrl!==void 0||e.scale!==void 0||e.sessionName!==void 0||e.password!==void 0)){let n=await Ee(),{waitUntilExit:r}=render(ct.createElement(Lo,{presetOverrides:n.presetOverrides,customScales:n.customScales}));await r(),process.exit(0);}let o={};if(e.name!==void 0&&(o.displayName=e.name),e.relayUrl!==void 0&&(o.relayUrl=e.relayUrl),e.scale!==void 0){if(e.scale!==""){let n=ue();if(!We(e.scale,n.presetOverrides,n.customScales)){console.error(`Unknown scale: "${e.scale}". Builtin IDs: ${Object.keys(X).join(", ")}`);return}}o.defaultScale=e.scale;}e.sessionName!==void 0&&(o.defaultSessionName=e.sessionName),e.password!==void 0&&(o.defaultPassword=e.password),Ae(o),console.log("Config updated.");}var Vo=Or(hideBin(process.argv)).scriptName("hnch").usage("$0 <command> \u2014 terminal pointing poker").command("create","Create a new room",e=>e.option("name",{type:"string",describe:"Session name"}).option("password",{type:"boolean",describe:"Protect room with a password"}).option("scale",{type:"string",describe:"Estimation scale ID (fibonacci, t-shirt, powers-of-2, linear, risk, or custom-<id>)"}),e=>{kr(e).catch(t=>{console.error(t instanceof Error?t.message:t),process.exit(1);});}).command("join <code>","Join an existing room",e=>e.positional("code",{type:"string",demandOption:true,describe:"6-character room code"}).option("password",{type:"boolean",describe:"Room requires a password",default:false}),e=>{Hr(e).catch(t=>{console.error(t instanceof Error?t.message:t),process.exit(1);});}).command("resume","Rejoin your last session",e=>e,()=>{Br().catch(e=>{console.error(e instanceof Error?e.message:e),process.exit(1);});}).command("config","View or update CLI config",e=>e.option("name",{type:"string",describe:"Set display name"}).option("relay-url",{type:"string",describe:"Set relay URL"}).option("scale",{type:"string",describe:"Set default estimation scale ID (empty string clears)"}).option("session-name",{type:"string",describe:"Set default session name (empty string clears)"}).option("password",{type:"boolean",describe:"Set default password prompt on (--password) or off (--no-password)"}),e=>{Ur(e).catch(t=>{console.error(t instanceof Error?t.message:t),process.exit(1);});}).demandCommand(1,"Specify a command: create, join, resume, or config").strict().help(),Lt=hideBin(process.argv);(Lt.length===0||Lt.length===1&&Lt[0]==="--")&&(Vo.showHelp(),process.exit(0));Vo.parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xauyxau/hnch",
3
- "version": "1.2.1",
3
+ "version": "1.4.0",
4
4
  "description": "Pointing poker CLI for agile teams",
5
5
  "keywords": [
6
6
  "pointing-poker",