import{r as l,f as oe,B as We,u as Sn,a as En,j as a,D as jn,b as Rn,p as Pn,c as An,P as Mn,d as Dn,S as Tn,e as On,v as Ln,g as Bn,C as Fn}from"./index-CW__6L0K.js";/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ve=(...e)=>e.filter((n,r,o)=>!!n&&n.trim()!==""&&o.indexOf(n)===r).join(" ").trim();/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Un=e=>e.replace(/([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase();/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Hn=e=>e.replace(/^([A-Z])|[\s-_]+(\w)/g,(n,r,o)=>o?o.toUpperCase():r.toLowerCase());/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ze=e=>{const n=Hn(e);return n.charAt(0).toUpperCase()+n.slice(1)};/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */var zn={xmlns:"http://www.w3.org/2000/svg",width:24,height:24,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round"};/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Xn=e=>{for(const n in e)if(n.startsWith("aria-")||n==="role"||n==="title")return!0;return!1};/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const $n=l.forwardRef(({color:e="currentColor",size:n=24,strokeWidth:r=2,absoluteStrokeWidth:o,className:c="",children:f,iconNode:p,...m},h)=>l.createElement("svg",{ref:h,...zn,width:n,height:n,stroke:e,strokeWidth:o?Number(r)*24/Number(n):r,className:Ve("lucide",c),...!f&&!Xn(m)&&{"aria-hidden":"true"},...m},[...p.map(([x,k])=>l.createElement(x,k)),...Array.isArray(f)?f:[f]]));/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const je=(e,n)=>{const r=l.forwardRef(({className:o,...c},f)=>l.createElement($n,{ref:f,iconNode:n,className:Ve(`lucide-${Un(ze(e))}`,`lucide-${e}`,o),...c}));return r.displayName=ze(e),r};/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Yn=[["path",{d:"m15 18-6-6 6-6",key:"1wnfg3"}]],Kn=je("chevron-left",Yn);/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const qn=[["path",{d:"m9 18 6-6-6-6",key:"mthhwq"}]],Gn=je("chevron-right",qn);/**
 * @license lucide-react v0.575.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Zn=[["path",{d:"M12 15V3",key:"m9g1x1"}],["path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4",key:"ih7n3h"}],["path",{d:"m7 10 5 5 5-5",key:"brsn70"}]],Wn=je("download",Zn),ce=`${We}/rank`,le={getDataset:()=>oe(`${ce}/dataset`).promise,getProgress:()=>oe(`${ce}/progress`).promise,exportLatest:(e=!1)=>oe(`${ce}/export${e?"?completed_only=true":""}`).promise,save:e=>oe(`${ce}/save`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}).promise};function Je(e){const n=new Set,r=[];for(const o of e)n.has(o)||(n.add(o),r.push(o));return r}function Vn(e){return e.map(n=>[...n])}function Jn(e,n){return n<=0||e<0?0:e>=n?n-1:e}function Xe(e,n){return e==null||Number.isNaN(e)?n:e<0?0:e>n?n:e}function Qn(e,n,r){const o=Je(e),c=new Set(o),f=Array.from({length:Math.max(1,n)},()=>[]),p=new Set;if(Array.isArray(r))for(let h=0;h<r.length&&!(h>=f.length);h+=1){const x=r[h];if(Array.isArray(x))for(const k of x)typeof k=="string"&&(!c.has(k)||p.has(k)||(f[h].push(k),p.add(k)))}return{unranked:o.filter(h=>!p.has(h)),rankColumns:f,selectedImageId:o[0]??null}}function Qe(e,n,r,o){if(!(e.unranked.includes(n)||e.rankColumns.some(m=>m.includes(n))))return e;const f=e.unranked.filter(m=>m!==n),p=Vn(e.rankColumns).map(m=>m.filter(h=>h!==n));if(r==null){const m=Xe(o,f.length);f.splice(m,0,n)}else{const m=p[Jn(r,p.length)],h=Xe(o,m.length);m.splice(h,0,n)}return{unranked:f,rankColumns:p,selectedImageId:n}}function et(e,n,r){if(n.length===0)return null;const o=new Set(n),c=Je(r);if(c.length===0)return n[0]??null;const f=c.indexOf(e),p=f>=0?f+1:0;for(let m=p;m<c.length;m+=1){const h=c[m];if(o.has(h))return h}for(let m=0;m<p;m+=1){const h=c[m];if(o.has(h))return h}return n[0]??null}function nt(e,n,r,o){const c=e.unranked.includes(n),f=Qe(e,n,r);if(f===e)return e;if(!c||r==null)return f;const p=et(n,f.unranked,o);return{...f,selectedImageId:p??n}}function tt(e){return e.rankColumns.filter(n=>n.length>0).map(n=>[...n])}function en(e){return e.unranked.length===0}function rt(e){const n=[...e.unranked];for(const r of e.rankColumns)n.push(...r);return n}function st(e,n){const r=rt(e);if(r.length===0)return null;if(!e.selectedImageId)return r[0];const o=r.indexOf(e.selectedImageId);if(o<0)return r[0];if(n==="left"){const f=o-1;return r[f]??r[0]}const c=o+1;return r[c]??r[r.length-1]}const at=10,it=200,de=120,fe=180;function q(e,n){return Number.isFinite(e)?e:n}function Ie(e,n,r){const o=Math.max(0,Math.trunc(q((r==null?void 0:r.splitterPx)??at,0))),c=Math.max(0,Math.trunc(q((r==null?void 0:r.minTopPx)??de,0))),f=Math.max(0,Math.trunc(q((r==null?void 0:r.minBottomPx)??fe,0))),p=Math.max(0,Math.trunc(q(n,0))),m=Math.max(0,p-o);if(m===0)return 0;const h=Math.max(0,m-f),x=Math.min(c,h),k=Math.trunc(q(e,x));return Math.min(h,Math.max(x,k))}function nn(e,n){if(e<"1"||e>"9")return null;const r=Number(e)-1;return r>=n?null:r}function ot(e,n){var o;if(n.isContentEditable)return!0;const r=((o=n.tagName)==null?void 0:o.toLowerCase())??"";return!!(r==="input"||r==="textarea"||r==="select"||e==="Enter"&&n.insideInteractiveControl)}function ct(e,n){const r=nn(e,n);if(r!=null)return{type:"assign-rank",rankIndex:r};const o=e.toLowerCase();return e==="ArrowLeft"||o==="a"?{type:"select-neighbor",direction:"left"}:e==="ArrowRight"||o==="d"?{type:"select-neighbor",direction:"right"}:e==="Enter"?{type:"fullscreen-open"}:o==="q"?{type:"instance-nav",direction:"prev"}:o==="e"?{type:"instance-nav",direction:"next"}:{type:"none"}}function lt(e,n){const r=nn(e,n);if(r!=null)return{type:"assign-rank",rankIndex:r};if(e==="Escape")return{type:"fullscreen-close"};const o=e.toLowerCase();return e==="ArrowLeft"||o==="a"?{type:"fullscreen-nav",direction:"prev"}:e==="ArrowRight"||o==="d"?{type:"fullscreen-nav",direction:"next"}:{type:"none"}}const Se=["#f97316","#eab308","#22c55e","#14b8a6","#3b82f6","#6366f1","#ec4899","#ef4444"];function ut(e){const n={},r=new Set;let o=0;for(const c of e)r.has(c)||(r.add(c),n[c]=Se[o%Se.length],o+=1);return n}function $e(e,n){return n<=0||e<0?0:e>=n?n-1:e}function dt(e){return e>0}function ft(e,n,r){return!r||e>=n-1?!1:en(r.board)}function ge(e){if(typeof e!="string")return!1;const n=Date.parse(e);return Number.isFinite(n)}function gt(e){const n=e==null?void 0:e.final_ranks;if(!Array.isArray(n))return null;const r=[];for(const o of n){if(!Array.isArray(o))continue;const c=o.filter(f=>typeof f=="string");c.length>0&&r.push(c)}return r}function mt(e,n){const r=new Map;for(const c of n)!c||typeof c.instance_id!="string"||r.set(c.instance_id,c);const o={};for(const c of e.instances){const f=c.images.map(x=>x.image_id),p=r.get(c.instance_id),m=Qn(f,c.max_ranks,gt(p)),h=ge(p==null?void 0:p.started_at)?p.started_at:null;o[c.instance_id]={board:m,startedAt:h,saveStatus:"idle",saveError:null}}return o}function ht(e){const n={};for(const r of e){if(!r||typeof r.instance_id!="string"||!Number.isInteger(r.save_seq)||(r.save_seq??-1)<0)continue;const o=n[r.instance_id],c=r.save_seq;n[r.instance_id]=typeof o=="number"?Math.max(o,c):c}return n}function pt(e,n=Date.now()){const r=Date.parse(e);return Number.isFinite(r)?Math.max(0,Math.trunc(n-r)):0}const H=1,Ye=4,Ke=.18,kt='button, a, [role="button"]',we=Se[0],tn="lenslet.ranking.unranked_height_px.v1",rn="lenslet.ranking.unranked_thumb_size_px.v1",sn=132,an=1560,Ee=4,on=208;function xt(){return a.jsxs("svg",{width:"13",height:"13",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round","aria-hidden":"true",children:[a.jsx("path",{d:"M8 3H3v5"}),a.jsx("path",{d:"M16 3h5v5"}),a.jsx("path",{d:"M3 16v5h5"}),a.jsx("path",{d:"M21 16v5h-5"})]})}function bt(){return new Date().toISOString()}function _e(e){return e.split("/").filter(Boolean).pop()||e}function ue(){return{zoom:H,offsetX:0,offsetY:0}}function ye(){return{active:!1,pointerId:null,startX:0,startY:0,originX:0,originY:0}}function Nt(e){return e<H?H:e>Ye?Ye:e}function Ct(e){if(!e)return null;if(typeof MouseEvent<"u"&&e instanceof MouseEvent)return{x:e.clientX,y:e.clientY};if(typeof TouchEvent<"u"&&e instanceof TouchEvent){const n=e.touches[0]??e.changedTouches[0];return n?{x:n.clientX,y:n.clientY}:null}return null}function vt(){if(typeof window>"u")return null;let e=null;try{e=window.localStorage.getItem(tn)}catch{return null}if(!e)return null;const n=Number(e);return!Number.isFinite(n)||n<=0?null:Math.trunc(n)}function me(e){if(!Number.isFinite(e))return on;const n=Math.round(e/Ee)*Ee;return Math.max(sn,Math.min(an,n))}function It(){if(typeof window>"u")return null;let e=null;try{e=window.localStorage.getItem(rn)}catch{return null}return e?me(Number(e)):null}const R="unranked";function he(e){return`rank-${e}`}function wt(e,n){if(e===R)return R;if(!e.startsWith("rank-"))return null;const r=Number(e.slice(5));return!Number.isInteger(r)||r<0||r>=n?null:he(r)}function cn(e){return e===R?null:Number(e.slice(5))}function qe(e,n){if(e.unranked.includes(n))return R;for(let r=0;r<e.rankColumns.length;r+=1)if(e.rankColumns[r].includes(n))return he(r);return null}function Ge(e,n){if(n===R)return e.unranked;const r=cn(n);return r==null?[]:e.rankColumns[r]??[]}function ln({image:e,label:n,dotColor:r,onOpenFullscreen:o}){return a.jsxs(a.Fragment,{children:[o?a.jsxs("button",{type:"button",className:"ranking-card-fullscreen",onPointerDown:c=>{c.preventDefault(),c.stopPropagation()},onClick:c=>{c.preventDefault(),c.stopPropagation(),o()},"aria-label":`Open ${n} fullscreen`,children:[a.jsx(xt,{}),a.jsx("span",{className:"sr-only",children:"Fullscreen"})]}):null,a.jsx("div",{className:"ranking-card-image-shell",children:a.jsx("img",{src:e.url,alt:e.sourcePath,loading:"lazy",draggable:!1})}),a.jsxs("div",{className:"ranking-card-meta",children:[a.jsx("span",{className:"ranking-card-dot",style:{backgroundColor:r},"aria-hidden":"true"}),a.jsx("div",{className:"ranking-card-label",children:n})]})]})}function _t({imageId:e,image:n,label:r,dotColor:o,containerId:c,isSelected:f,draggingDisabled:p,onSelect:m,onOpenFullscreen:h,registerCardRef:x}){const{attributes:k,listeners:D,setNodeRef:T,transform:O,isDragging:E}=Bn({id:e,data:{containerId:c},disabled:p,transition:null,animateLayoutChanges:()=>!1}),z={transform:Fn.Transform.toString(O),transition:"none"},P=["ranking-card",c===R?"is-unranked":"is-ranked",f&&"is-selected",E&&"is-dragging"].filter(Boolean).join(" ");return a.jsx("article",{className:P,ref:G=>{T(G),x(e,G)},style:z,onClick:m,...k,...D,children:a.jsx(ln,{image:n,label:r,dotColor:o,onOpenFullscreen:h})})}function Ze({containerId:e,title:n,imageIds:r,className:o,cardsClassName:c,cardsStyle:f,isDragOver:p,showHeader:m=!0,headerContent:h,renderCard:x}){const{setNodeRef:k}=Dn({id:e}),D=e===R?On:Ln,T=["ranking-column",o,p&&"is-drag-over"].filter(Boolean).join(" ");return a.jsxs("section",{className:T,children:[m?a.jsx("header",{className:"ranking-column-header ranking-unselectable",children:h??n}):null,a.jsx("div",{ref:k,className:`ranking-column-cards ${c}`,style:f,children:a.jsx(Tn,{id:e,items:r,strategy:D,children:r.map(O=>x(O,e))})})]})}function Et(){const[e,n]=l.useState(null),[r,o]=l.useState({}),[c,f]=l.useState(0),[p,m]=l.useState(!0),[h,x]=l.useState(null),[k,D]=l.useState(null),[T,O]=l.useState(null),[E,z]=l.useState(()=>{const t=vt();return t??(typeof window<"u"&&Number.isFinite(window.innerHeight)?Math.round(window.innerHeight*.5):it)}),[P,G]=l.useState(()=>It()??on),[Z,Re]=l.useState(!1),[v,W]=l.useState(null),[I,V]=l.useState(ue),A=l.useRef(r),J=l.useRef({}),pe=l.useRef({}),Pe=l.useRef({}),X=l.useRef(null),L=l.useRef(null),Q=l.useRef(null),B=l.useRef(ye());l.useEffect(()=>{A.current=r},[r]),l.useEffect(()=>{if(typeof window>"u")return;const t=()=>{const s=X.current;if(!s)return;const{height:i}=s.getBoundingClientRect();z(u=>{const d=u??Math.round(i*.5);return Ie(d,i,{minTopPx:de,minBottomPx:fe,splitterPx:0})})};return t(),window.addEventListener("resize",t),()=>window.removeEventListener("resize",t)},[]),l.useEffect(()=>{const t=X.current;if(!t)return;const{height:s}=t.getBoundingClientRect();z(i=>{const u=i??Math.round(s*.5);return Ie(u,s,{minTopPx:de,minBottomPx:fe,splitterPx:0})})},[c]),l.useEffect(()=>{if(!(typeof window>"u")&&!(!Number.isFinite(E)||E<=0))try{window.localStorage.setItem(tn,String(Math.trunc(E)))}catch{}},[E]),l.useEffect(()=>{if(!(typeof window>"u"))try{window.localStorage.setItem(rn,String(me(P)))}catch{}},[P]);const w=l.useCallback((t,s)=>{o(i=>{const u=i[t];return u?{...i,[t]:s(u)}:i})},[]);l.useEffect(()=>{let t=!0;return m(!0),x(null),Promise.all([le.getDataset(),le.getProgress(),le.exportLatest(!1)]).then(([s,i,u])=>{t&&(n(s),o(mt(s,u.results)),pe.current=ht(u.results),f($e(i.resume_instance_index,s.instances.length)),m(!1))}).catch(s=>{t&&(x(s instanceof Error?s.message:"failed to load ranking session"),m(!1))}),()=>{t=!1}},[]);const Ae=(e==null?void 0:e.instances)??[],g=Ae[c]??null,N=g?r[g.instance_id]??null:null,ke=dt(c),ee=ft(c,Ae.length,N),xe=l.useMemo(()=>{const t=new Map;if(!g)return t;for(const s of g.images)t.set(s.image_id,{url:s.url,sourcePath:s.source_path});return t},[g]),_=l.useMemo(()=>(g==null?void 0:g.images.map(t=>t.image_id))??[],[g]),ne=v?xe.get(v)??null:null,Me=l.useMemo(()=>ut(_),[_]);l.useEffect(()=>{if(typeof Image>"u"||!e)return;const t=e.instances[c+1];if(t)for(const s of t.images){const i=new Image;i.src=s.url}},[e,c]),l.useEffect(()=>{W(null),V(ue()),B.current=ye()},[g==null?void 0:g.instance_id]);const De=l.useCallback(t=>{var u;const s=(u=A.current[t])==null?void 0:u.startedAt;if(ge(s))return s;const i=bt();return w(t,d=>ge(d.startedAt)?d:{...d,startedAt:i}),i},[w]),te=l.useCallback((t,s,i=null)=>{const u=t.instance_id,d=ge(i)?i:De(u),b=(pe.current[u]??0)+1;pe.current[u]=b;const C={instance_id:u,final_ranks:tt(s),started_at:d,duration_ms:pt(d),completed:en(s),save_seq:b},j=(J.current[u]??0)+1;J.current[u]=j,w(u,S=>({...S,saveStatus:"saving",saveError:null})),le.save(C).then(()=>{J.current[u]===j&&w(u,S=>({...S,saveStatus:"saved",saveError:null}))}).catch(S=>{if(J.current[u]!==j)return;const ae=S instanceof Error?S.message:"failed to save ranking";w(u,U=>({...U,saveStatus:"error",saveError:ae}))})},[De,w]),$=l.useCallback((t,s,i)=>{if(!g)return;const u=A.current[g.instance_id];if(!u)return;const d=i!=null&&i.autoAdvance?nt(u.board,t,s,_):Qe(u.board,t,s,i==null?void 0:i.targetInsertIndex);d!==u.board&&(w(g.instance_id,b=>({...b,board:d})),te(g,d,u.startedAt))},[_,g,te,w]),y=l.useCallback(t=>{g&&w(g.instance_id,s=>({...s,board:{...s.board,selectedImageId:t}}))},[g,w]),un=l.useCallback((t,s)=>{Pe.current[t]=s},[]),Y=l.useCallback(t=>{!t||typeof window>"u"||window.requestAnimationFrame(()=>{window.requestAnimationFrame(()=>{var s;(s=Pe.current[t])==null||s.focus()})})},[]),M=l.useCallback(()=>{B.current=ye(),V(ue())},[]),be=l.useCallback(t=>{y(t),W(t),M()},[M,y]),Ne=l.useCallback(()=>{W(t=>(Y(t),null)),M()},[Y,M]),F=l.useCallback(()=>{O(null),D(null),Q.current=null},[]),dn=l.useCallback(t=>{(t.pointerType??"mouse")!=="mouse"||t.button!==0||k||!X.current||(t.preventDefault(),t.stopPropagation(),F(),L.current={startY:t.clientY,startHeight:E},Re(!0))},[k,F,E]);l.useEffect(()=>{const t=i=>{if(!L.current)return;const u=X.current;if(!u)return;const d=i.clientY-L.current.startY,b=L.current.startHeight+d,{height:C}=u.getBoundingClientRect();z(Ie(b,C,{minTopPx:de,minBottomPx:fe,splitterPx:0}))},s=()=>{L.current&&(L.current=null,Re(!1))};return window.addEventListener("pointermove",t),window.addEventListener("pointerup",s),window.addEventListener("pointercancel",s),()=>{window.removeEventListener("pointermove",t),window.removeEventListener("pointerup",s),window.removeEventListener("pointercancel",s)}},[]);const Te=l.useCallback(t=>{if(!v||_.length===0)return;const s=_.indexOf(v);if(s<0)return;const i=t==="prev"?s-1:s+1,u=_[i];u&&(W(u),y(u),M())},[_,v,M,y]),fn=l.useCallback(t=>{t.preventDefault();const s=t.deltaY<0?Ke:-Ke;V(i=>{const u=Nt(i.zoom+s);return u===H?ue():{...i,zoom:u}})},[]),gn=l.useCallback(t=>{t.button!==0||I.zoom<=H||(t.preventDefault(),t.currentTarget.setPointerCapture(t.pointerId),B.current={active:!0,pointerId:t.pointerId,startX:t.clientX,startY:t.clientY,originX:I.offsetX,originY:I.offsetY})},[I.offsetX,I.offsetY,I.zoom]),mn=l.useCallback(t=>{const s=B.current;if(!s.active||s.pointerId!==t.pointerId)return;const i=s.originX+(t.clientX-s.startX),u=s.originY+(t.clientY-s.startY);V(d=>({...d,offsetX:i,offsetY:u}))},[]),Oe=l.useCallback(t=>{t.currentTarget.hasPointerCapture(t.pointerId)&&t.currentTarget.releasePointerCapture(t.pointerId),B.current.active=!1,B.current.pointerId=null},[]),re=l.useCallback(t=>{if(!g||!e)return;const s=$e(t,e.instances.length);if(s===c)return;const i=A.current[g.instance_id];i&&te(g,i.board,i.startedAt),f(s)},[c,g,e,te]),Ce=l.useCallback(()=>{ke&&re(c-1)},[ke,c,re]),ve=l.useCallback(()=>{ee&&re(c+1)},[ee,c,re]);l.useEffect(()=>{if(!N||!g)return;const t=s=>{const i=s.target instanceof HTMLElement?s.target:null;if(ot(s.key,{isContentEditable:!!(i!=null&&i.isContentEditable),tagName:(i==null?void 0:i.tagName)??null,insideInteractiveControl:!!(i!=null&&i.closest(kt))}))return;if(v){const d=lt(s.key,g.max_ranks);if(d.type==="assign-rank"){s.preventDefault(),$(v,d.rankIndex),y(v);return}if(d.type==="fullscreen-nav"){s.preventDefault(),Te(d.direction);return}d.type==="fullscreen-close"&&(s.preventDefault(),Ne());return}const u=ct(s.key,g.max_ranks);if(u.type==="assign-rank"){const d=N.board.selectedImageId;if(!d)return;s.preventDefault();const b=g.instance_id;$(d,u.rankIndex,{autoAdvance:!0}),window.setTimeout(()=>{var j;const C=((j=A.current[b])==null?void 0:j.board.selectedImageId)??null;Y(C)},0);return}if(u.type==="select-neighbor"){s.preventDefault();const d=st(N.board,u.direction);d&&(y(d),Y(d));return}if(u.type==="instance-nav"){s.preventDefault(),u.direction==="prev"?Ce():ve();return}if(u.type==="fullscreen-open"){const d=N.board.selectedImageId;if(!d)return;s.preventDefault(),be(d)}};return window.addEventListener("keydown",t),()=>window.removeEventListener("keydown",t)},[Ne,g,N,v,ve,Ce,$,Te,be,Y,y]);const hn=Sn(En(Mn,{activationConstraint:{distance:4}})),pn=l.useMemo(()=>[({transform:t})=>{const s=Q.current;return s?{...t,x:t.x-s.x,y:t.y-s.y}:t}],[]),K=l.useCallback((t,s)=>{if(!s)return null;const i=wt(s,t.rankColumns.length);return i||qe(t,s)},[]),kn=l.useCallback(t=>{if(Z||!N)return;const s=String(t.active.id);if(!qe(N.board,s))return;const i=Ct(t.activatorEvent),u=t.active.rect.current.initial;i&&u?Q.current={x:Math.max(0,i.x-u.left),y:Math.max(0,i.y-u.top)}:Q.current=null,D(s),y(s)},[N,Z,y]),xn=l.useCallback(t=>{if(!g)return;const s=A.current[g.instance_id];if(!s)return;const i=t.over?String(t.over.id):null;O(K(s.board,i))},[g,K]),bn=l.useCallback(t=>{var Ue;if(!g){F();return}const s=A.current[g.instance_id];if(F(),!s||!t.over)return;const i=String(t.active.id),u=String(t.over.id),d=s.board,b=K(d,i),C=K(d,u);if(!b||!C)return;const j=Ge(d,b),S=j.indexOf(i);if(S<0)return;const ae=Ge(d,C);if(b===C&&u===C&&S===j.length-1)return;let U;if(u===C)U=ae.length;else{const ie=ae.indexOf(u);if(ie<0)return;if(b===C){if(S===ie)return;U=ie}else{const He=(Ue=t.active.rect.current.translated)==null?void 0:Ue.top,yn=He!=null&&He>t.over.rect.top+t.over.rect.height;U=ie+(yn?1:0)}}$(i,cn(C),{targetInsertIndex:U})},[F,g,$,K]);if(p)return a.jsx("div",{className:"ranking-loading",children:"Loading ranking session..."});if(h||!g||!N||!e)return a.jsxs("div",{className:"ranking-error",children:[a.jsx("p",{children:"Failed to load ranking mode."}),a.jsx("pre",{children:h??"missing ranking dataset payload"})]});const Le=`1-${Math.max(1,N.board.rankColumns.length)}`,Nn=`${We}/rank/export?completed_only=true`,Cn=v?_.indexOf(v)+1:0,vn=["ranking-workspace",Z&&"is-resizing"].filter(Boolean).join(" "),In={height:`${Math.max(0,Math.trunc(E))}px`},wn={"--ranking-card-size-unranked":`${me(P)}px`},Be=(t,s)=>{const i=xe.get(t);if(!i)return null;const u=N.board.selectedImageId===t,d=_e(i.sourcePath),b=Me[t]??we;return a.jsx(_t,{imageId:t,image:i,label:d,dotColor:b,containerId:s,isSelected:u,draggingDisabled:Z,onSelect:()=>y(t),onOpenFullscreen:()=>be(t),registerCardRef:un},t)},se=k?xe.get(k)??null:null,Fe=se?_e(se.sourcePath):null,_n=k?Me[k]??we:we;return a.jsxs("div",{className:"ranking-root",style:wn,children:[a.jsx("header",{className:"ranking-header ranking-unselectable",children:a.jsxs("div",{className:"ranking-shell ranking-header-shell",children:[a.jsxs("div",{className:"ranking-header-leading",children:[a.jsx("h1",{className:"ranking-header-title",children:"Image Ranking"}),a.jsxs("strong",{className:"ranking-progress-pill",children:[c+1," / ",e.instances.length]})]}),a.jsxs("div",{className:"ranking-header-trailing",children:[a.jsxs("label",{className:"ranking-thumb-size-control ranking-unselectable ranking-thumb-size-control-header",children:[a.jsx("span",{children:"Thumbs"}),a.jsx("input",{type:"range",min:sn,max:an,step:Ee,value:P,onChange:t=>{G(me(Number(t.currentTarget.value)))},"aria-label":"Unassigned thumbnail size"}),a.jsxs("span",{className:"ranking-thumb-size-value",children:[P,"px"]})]}),a.jsxs("a",{className:"ranking-button ranking-export-button",href:Nn,target:"_blank",rel:"noreferrer",children:[a.jsx(Wn,{className:"ranking-button-icon","aria-hidden":"true"}),"Export"]}),a.jsxs("div",{className:"ranking-nav-group",children:[a.jsxs("button",{type:"button",className:"ranking-button",onClick:Ce,disabled:!ke,children:[a.jsx(Kn,{className:"ranking-button-icon","aria-hidden":"true"}),a.jsx("span",{children:"Prev (Q)"})]}),a.jsx("span",{className:"ranking-next-tooltip",title:ee?void 0:"Rank all images before continuing.",children:a.jsxs("button",{type:"button",className:"ranking-button ranking-button-primary",onClick:ve,disabled:!ee,children:[a.jsx("span",{children:"Next (E)"}),a.jsx(Gn,{className:"ranking-button-icon","aria-hidden":"true"})]})})]})]})]})}),a.jsxs(jn,{sensors:hn,collisionDetection:t=>{const s=Pn(t);return s.length>0?s:An(t)},onDragStart:kn,onDragOver:xn,onDragEnd:bn,onDragCancel:F,children:[a.jsx("main",{className:"ranking-main",children:a.jsx("div",{className:"ranking-shell ranking-main-shell",children:a.jsxs("div",{className:vn,ref:X,children:[a.jsxs("section",{className:"ranking-unranked-panel",children:[a.jsxs("header",{className:"ranking-unranked-head",children:[a.jsx("h1",{className:"ranking-title",children:"Unassigned"}),a.jsxs("p",{className:"ranking-unranked-hint",children:["Press ",Le," to assign selected, Enter for fullscreen"]})]}),a.jsx("div",{className:"ranking-unranked-tray",style:In,children:a.jsx(Ze,{containerId:R,title:"Unassigned",imageIds:N.board.unranked,className:"ranking-column-unranked",cardsClassName:"ranking-column-cards-unranked",isDragOver:T===R,showHeader:!1,renderCard:Be})}),a.jsx("div",{className:"ranking-unranked-resizer",children:a.jsx("button",{type:"button",className:"ranking-unranked-resize-handle",onPointerDown:dn,"aria-label":"Resize unassigned panel",title:"Drag to resize unassigned panel"})})]}),a.jsx("section",{className:"ranking-ranks-panel",children:a.jsx("div",{className:"ranking-ranks-board",children:N.board.rankColumns.map((t,s)=>a.jsx(Ze,{containerId:he(s),title:`${s+1}`,headerContent:a.jsx("span",{className:"ranking-rank-index",children:s+1}),imageIds:t,className:"ranking-column-rank",cardsClassName:"ranking-column-cards-rank",isDragOver:T===he(s),renderCard:Be},s))})})]})})}),a.jsx(Rn,{modifiers:pn,dropAnimation:null,children:se&&Fe?a.jsx("article",{className:"ranking-card ranking-card-overlay",children:a.jsx(ln,{image:se,label:Fe,dotColor:_n})}):null})]}),v&&ne?a.jsxs("div",{className:"ranking-fullscreen",role:"dialog","aria-modal":"true",children:[a.jsxs("header",{className:"ranking-fullscreen-header",children:[a.jsxs("div",{className:"ranking-fullscreen-meta",children:[a.jsxs("strong",{children:[Cn," / ",_.length]}),a.jsx("span",{children:_e(ne.sourcePath)})]}),a.jsxs("div",{className:"ranking-fullscreen-hint",children:["Hotkeys: ",Le," rank, a/d image, Esc close"]}),a.jsx("button",{type:"button",className:"ranking-button",onClick:Ne,children:"Close"})]}),a.jsx("div",{className:`ranking-fullscreen-stage ${I.zoom>H?"is-zoomed":""}`,onWheel:fn,onPointerDown:gn,onPointerMove:mn,onPointerUp:Oe,onPointerCancel:Oe,onDoubleClick:M,children:a.jsx("img",{src:ne.url,alt:ne.sourcePath,className:"ranking-fullscreen-image",draggable:!1,style:{transform:`translate3d(${I.offsetX}px, ${I.offsetY}px, 0) scale(${I.zoom})`}})})]}):null]})}export{Et as default};
