import{s as q,a as w,t as k,e as Z,b as y}from"../chunks/legacy.BFrtrUTY.js";import{b as $,I as tt,s as j,c as et,r as nt}from"../chunks/index.svelte_svelte_type_style_lang.oHDPoyLK.js";import{c as at}from"../chunks/dataset.DUSx8dem.js";import{p as J,Q as it,d as p,u as O,a as Q,g as e,b as u,c as T,e as F,f as ot,s as st,t as N,r as B}from"../chunks/index.Chpxrmiw.js";import{a as U,b as X,i as rt}from"../chunks/index-client.D0CpKjVb.js";import{G as dt}from"../chunks/Grid.CTo5z7L1.js";import{u as lt}from"../chunks/useGlobalStorage.B7aqgx7w.js";import{r as ct}from"../chunks/routes.BJiwio4j.js";import{S as ht,P as ft}from"../chunks/SelectableBox.DAf3j8HI.js";const gt=async({dataset_id:_,offset:t=0,limit:a=10,annotation_label_ids:i,tag_ids:d})=>{const s={data:[],error:void 0};try{const n=await at.GET("/api/datasets/{dataset_id}/annotations",{params:{path:{dataset_id:_},query:{annotation_label_ids:i,tag_ids:d,offset:t,limit:a}}});if(n.error)throw new Error(JSON.stringify(n.error,null,2));if(!n.data)throw new Error("No data");s.data=n.data}catch(n){s.error="Error loading annotations: "+String(n)}return s};var mt=k('<div><!> <a class="block"><!></a></div>'),ut=k('<div slot="noMore"></div>'),_t=k('<div slot="noResults"></div>'),vt=k('<div class="viewport flex-1 svelte-1ppli2f"><!></div>');function bt(_,t){J(t,!0);const a=q(),i=()=>y(x,"$infiniteLoaderIdentifier",a),d=()=>y(t.selectedAnnotationsIds,"$selectedAnnotationsIds",a),s=()=>y(t.tagsSelected,"$tagsSelected",a),n=()=>y(L,"$pickedAnnotationIds",a);let x=it([t.selectedAnnotationsIds,t.tagsSelected],([h,A])=>h.join(",")+Array.from(A).join(",")),c=p(0);const S=100;let o=p(U([])),f=p(null);O(()=>{i(),u(o,U([])),u(c,0)});async function H({detail:{loaded:h,complete:A}}){const b=await gt({dataset_id:t.dataset_id,limit:S,offset:e(c),annotation_label_ids:d().length>0?d():void 0,tag_ids:s().size>0?Array.from(s()):void 0});b.data.length?(u(c,e(c)+S),e(o).push(...b.data),b.data.length<S?A():h()):A()}const{selectedAnnotationIds:L,toggleAnnotationSelection:C}=lt(),W=$(),[g,r]=W;var v=vt(),E=T(v),P=F(()=>{var h;return((h=e(f))==null?void 0:h.clientHeight)||0});dt(E,{get itemCount(){return e(o).length},get itemHeight(){return t.itemHeight},get itemWidth(){return t.itemWidth},get height(){return e(P)},get style(){return`--sample-width: ${t.itemWidth??""}px; --sample-height: ${t.itemHeight??""}px;`},item:(b,l)=>{let m=()=>l==null?void 0:l().index,I=()=>l==null?void 0:l().style;var M=Z(),Y=ot(M);rt(Y,()=>e(o)[m()]&&e(o)[m()].sample,D=>{var z=mt(),R=T(z),K=F(()=>n().has(e(o)[m()].annotation_id));ht(R,{onSelect:()=>C(e(o)[m()].annotation_id),get isSelected(){return e(K)}});var G=st(R,2);N(()=>j(G,"href",g(ct.toSample(e(o)[m()].sample.sample_id),void 0)));var V=T(G);wt(V,{get annotation(){return e(o)[m()]}}),B(G),B(z),N(()=>j(z,"style",I())),w(D,z)}),w(b,M)},footer:b=>{tt(b,{get identifier(){return i()},$$events:{infinite:H},$$slots:{noMore:(l,m)=>{var I=ut();w(l,I)},noResults:(l,m)=>{var I=_t();w(l,I)}}})},$$slots:{item:!0,footer:!0}}),B(v),X(v,h=>u(f,h),()=>e(f)),w(_,v),Q()}var St=k('<canvas class="image"></canvas>');function wt(_,t){var S,o;J(t,!0);let a=p(null);const i=new Image;i.src=ft+((o=(S=t.annotation)==null?void 0:S.sample)==null?void 0:o.file_path_abs);let d=p(!1),s=p(!1);const n=20,x=()=>{var f,H,L;if(e(a)){const C=((f=e(a))==null?void 0:f.width)||0,W=((H=e(a))==null?void 0:H.height)||0,g=e(a).getContext("2d");if(!g)return;if(e(s)){g.fillStyle="red",g.font="30px Arial",g.fillText("Error loading image",10,50);return}const r=et({coords:{width:t.annotation.width+n*2,height:t.annotation.height+n*2},container:{width:C,height:W}}),v=t.annotation.width*n/r.width,E=t.annotation.height*n/r.height;g.drawImage(i,t.annotation.x-v,t.annotation.y-E,t.annotation.width+v*2,t.annotation.height+E*2,r.x,r.y,r.width,r.height);const P=(L=t.annotation)==null?void 0:L.annotation_label;P&&nt({ctx:g,coords:{x:n+r.x,y:n+r.y,width:r.width-n*2,height:r.height-n*2},colors:{stroke:"red",fill:"rgba(255, 0, 0, 0)",textBackground:"rgba(255, 0, 0, 0.3)",text:"white"},lineWidth:1,labelText:P.annotation_label_name})}};i.onload=()=>{u(d,!0)},i.onerror=()=>{u(d,!0),u(s,!0)},O(()=>{e(d)&&x()});var c=St();X(c,f=>u(a,f),()=>e(a)),w(_,c),Q()}function Pt(_,t){const a=q(),i=()=>y(c,"$boundedSampleSize",a),{datasetId:d,sampleSize:s,selectedAnnotationsIds:n,tagsSelected:x}=t.data,c=s.sampleSize;bt(_,{get itemHeight(){return i().height},get itemWidth(){return i().width},dataset_id:d,tagsSelected:x,selectedAnnotationsIds:n})}export{Pt as component};
