import{c as L,a as l,f as w,t as X}from"../chunks/vfPE-AD7.js";import{i as k,p as be,j as De,d as c,s as j,e as s,r,f as o,g as a,u as Pe,t as U,n as E,c as Me,k as pe}from"../chunks/DhUiKsxN.js";import{s as y,e as ke}from"../chunks/Dsv8gxIT.js";import{l as ee,b as te,i as F}from"../chunks/DUil1G3I.js";import{I as ae,a as re,e as he,m as ce,r as $e}from"../chunks/QNXDtpsh.js";import{C as ge,B as Y,I as Ne,b as ye}from"../chunks/BWzyK28a.js";import{g as ze}from"../chunks/b2QY-nV2.js";import{r as Be}from"../chunks/DyqxJaEr.js";import{s as V}from"../chunks/BvTRQjz0.js";import{H as Se,f as we,B as ie,T as Ue}from"../chunks/CPs-cmJs.js";import"../chunks/1jpszzm8.js";function Ce(N,e){const i=ee(e,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const x=[["path",{d:"M12 17V3"}],["path",{d:"m6 11 6 6 6-6"}],["path",{d:"M19 21H5"}]];ae(N,te({name:"arrow-down-to-line"},()=>i,{get iconNode(){return x},children:(m,b)=>{var f=L(),D=k(f);re(D,e,"default",{}),l(m,f)},$$slots:{default:!0}}))}function je(N,e){const i=ee(e,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const x=[["path",{d:"M20 6 9 17l-5-5"}]];ae(N,te({name:"check"},()=>i,{get iconNode(){return x},children:(m,b)=>{var f=L(),D=k(f);re(D,e,"default",{}),l(m,f)},$$slots:{default:!0}}))}function Fe(N,e){const i=ee(e,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const x=[["path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"}],["polyline",{points:"7 10 12 15 17 10"}],["line",{x1:"12",x2:"12",y1:"15",y2:"3"}]];ae(N,te({name:"download"},()=>i,{get iconNode(){return x},children:(m,b)=>{var f=L(),D=k(f);re(D,e,"default",{}),l(m,f)},$$slots:{default:!0}}))}function He(N,e){const i=ee(e,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const x=[["path",{d:"M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z"}]];ae(N,te({name:"heart"},()=>i,{get iconNode(){return x},children:(m,b)=>{var f=L(),D=k(f);re(D,e,"default",{}),l(m,f)},$$slots:{default:!0}}))}function Ie(N,e){const i=ee(e,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const x=[["circle",{cx:"11",cy:"11",r:"8"}],["path",{d:"m21 21-4.3-4.3"}]];ae(N,te({name:"search"},()=>i,{get iconNode(){return x},children:(m,b)=>{var f=L(),D=k(f);re(D,e,"default",{}),l(m,f)},$$slots:{default:!0}}))}var Re=w("<!> Downloaded",1),Ee=w('<div class="mt-3 flex flex-wrap gap-1"><!> <!></div>'),Ge=w("<!> Delete",1),Ae=w("<!> <!>",1),Le=w("<!> ",1),Oe=w('<div class="mt-2 text-sm text-red-500"> </div>'),Te=w('<div class="flex items-start justify-between"><div class="flex-1 min-w-0"><h3 class="font-semibold text-base truncate"> </h3> <div class="flex items-center gap-4 mt-1 text-sm text-muted-foreground"><span class="flex items-center gap-1"><!> </span> <span class="flex items-center gap-1"><!> </span> <span class="flex items-center gap-1"><!> </span></div></div> <div class="flex items-center gap-2"><!></div></div> <!> <div class="mt-4 flex justify-end gap-2"><!></div> <!>',1);function Je(N,e){be(e,!0);let i=j(!1),x=j(!1),m=j(null),b=j(null),f=Pe(()=>a(b)??e.model.is_downloaded);De(()=>{e.model.model_id,c(b,null)});async function D(){c(i,!0),c(m,null);try{const{task_id:M}=await ce.startDownload(e.model.model_id),z=new EventSource(`/api/models/download/${M}/progress`);z.onmessage=O=>{const H=JSON.parse(O.data);H.status==="completed"?(c(b,!0),c(i,!1),z.close()):H.status==="failed"&&(c(m,H.error||"Download failed",!0),c(i,!1),z.close())},z.onerror=()=>{c(m,"Connection lost"),c(i,!1),z.close()}}catch(M){c(m,M instanceof Error?M.message:"Download failed",!0),c(i,!1)}}async function Z(){var M;if(confirm(`Delete ${e.model.model_id}?`)){c(x,!0),c(m,null);try{await ce.delete(e.model.model_id),c(b,!1),(M=e.onDeleted)==null||M.call(e)}catch(z){c(m,z instanceof Error?z.message:"Delete failed",!0)}finally{c(x,!1)}}}function se(){var M;(M=e.onUse)==null||M.call(e,e.model.model_id)}ge(N,{class:"p-4",children:(M,z)=>{var O=Te(),H=k(O),G=s(H),A=s(G),ve=s(A,!0);r(A);var oe=o(A,2),T=s(oe),q=s(T);Ce(q,{class:"w-4 h-4"});var me=o(q);r(T);var J=o(T,2),le=s(J);He(le,{class:"w-4 h-4"});var fe=o(le);r(J);var ne=o(J,2),de=s(ne);Se(de,{class:"w-4 h-4"});var ue=o(de);r(ne),r(oe),r(G);var t=o(G,2),v=s(t);{var g=n=>{ie(n,{variant:"success",children:(h,P)=>{var S=Re(),d=k(S);je(d,{class:"w-3 h-3 mr-1"}),E(),l(h,S)},$$slots:{default:!0}})};F(v,n=>{a(f)&&n(g)})}r(t),r(H);var p=o(H,2);{var u=n=>{var h=Ee(),P=s(h);he(P,16,()=>e.model.tags.slice(0,5),$=>$,($,B)=>{ie($,{variant:"secondary",class:"text-xs",children:(Q,_e)=>{E();var xe=X();U(()=>y(xe,B)),l(Q,xe)},$$slots:{default:!0}})});var S=o(P,2);{var d=$=>{ie($,{variant:"outline",class:"text-xs",children:(B,Q)=>{E();var _e=X();U(()=>y(_e,`+${e.model.tags.length-5}`)),l(B,_e)},$$slots:{default:!0}})};F(S,$=>{e.model.tags.length>5&&$(d)})}r(h),l(n,h)};F(p,n=>{e.model.tags.length>0&&n(u)})}var _=o(p,2),C=s(_);{var K=n=>{var h=Ae(),P=k(h);Y(P,{variant:"outline",size:"sm",onclick:Z,get disabled(){return a(x)},children:(d,$)=>{var B=Ge(),Q=k(B);Ue(Q,{class:"w-4 h-4 mr-1"}),E(),l(d,B)},$$slots:{default:!0}});var S=o(P,2);Y(S,{variant:"default",size:"sm",onclick:se,children:(d,$)=>{E();var B=X("Use");l(d,B)},$$slots:{default:!0}}),l(n,h)},W=n=>{Y(n,{variant:"default",size:"sm",onclick:D,get disabled(){return a(i)},children:(h,P)=>{var S=Le(),d=k(S);Fe(d,{class:"w-4 h-4 mr-1"});var $=o(d);U(()=>y($,` ${a(i)?"Downloading...":"Download"}`)),l(h,S)},$$slots:{default:!0}})};F(C,n=>{a(f)?n(K):n(W,!1)})}r(_);var I=o(_,2);{var R=n=>{var h=Oe(),P=s(h,!0);r(h),U(()=>y(P,a(m))),l(n,h)};F(I,n=>{a(m)&&n(R)})}U((n,h)=>{y(ve,e.model.model_id),y(me,` ${n??""}`),y(fe,` ${h??""}`),y(ue,` ~${e.model.estimated_size_gb??""} GB`)},[()=>we(e.model.downloads),()=>we(e.model.likes)]),l(M,O)},$$slots:{default:!0}}),Me()}var Qe=w("<!> ",1),Ve=w('<form class="flex gap-4"><div class="flex-1 relative"><!> <!></div> <!></form> <div class="flex items-center gap-4 mt-4"><label class="flex items-center gap-2 text-sm"><input type="checkbox" class="rounded"/> <span> </span></label> <label class="flex items-center gap-2 text-sm"><input type="checkbox" class="rounded"/> <span>Downloaded only</span></label></div>',1),Ze=w('<div class="text-center py-8 text-red-500"> </div>'),qe=w('<section><h2 class="text-lg font-semibold mb-4"> </h2> <div class="grid gap-4"></div></section>'),Ke=w('<div class="text-center py-8 text-muted-foreground">No models found. Try a different search term.</div>'),We=w('<div class="flex items-center justify-between"><div><h3 class="font-medium"> </h3> <p class="text-sm text-muted-foreground"> </p></div> <!></div>'),Xe=w('<section><h2 class="text-lg font-semibold mb-4"> </h2> <div class="grid gap-4"></div></section>'),Ye=w('<div class="text-center py-12 text-muted-foreground">Search for models to download, or browse your local models.</div>'),et=w('<div class="space-y-6"><div class="flex items-center justify-between"><h1 class="text-2xl font-bold">Model Browser</h1> <!></div> <!> <!> <!> <!></div>');function mt(N,e){be(e,!0);let i=j(""),x=j(pe([])),m=j(pe([])),b=j(!1),f=j(null),D=j(!0),Z=j(!1);De(()=>{se(),V.refreshMemory()});async function se(){try{c(m,await ce.listLocal(),!0)}catch(t){console.error("Failed to load local models:",t)}}async function M(){if(a(i).trim()){c(b,!0),c(f,null);try{const t=a(D)&&V.memory?V.memory.mlx_recommended_gb:void 0;c(x,await ce.search(a(i),t,20),!0)}catch(t){c(f,t instanceof Error?t.message:"Search failed",!0)}finally{c(b,!1)}}}async function z(t){const v=`${Be("/profiles/new")}?model=${encodeURIComponent(t)}`;await ze(v)}function O(){se()}const H=Pe(()=>a(Z)?a(x).filter(t=>t.is_downloaded):a(x));var G=et(),A=s(G),ve=o(s(A),2);{var oe=t=>{ie(t,{variant:"outline",class:"text-sm",children:(v,g)=>{var p=Qe(),u=k(p);Se(u,{class:"w-4 h-4 mr-1"});var _=o(u);U(C=>y(_,` Recommended: <${C??""} GB`),[()=>V.memory.mlx_recommended_gb.toFixed(0)]),l(v,p)},$$slots:{default:!0}})};F(ve,t=>{V.memory&&t(oe)})}r(A);var T=o(A,2);ge(T,{class:"p-4",children:(t,v)=>{var g=Ve(),p=k(g),u=s(p),_=s(u);Ie(_,{class:"absolute left-3 top-1/2 transform -translate-y-1/2 w-4 h-4 text-muted-foreground"});var C=o(_,2);Ne(C,{placeholder:"Search mlx-community models...",class:"pl-10",get value(){return a(i)},set value(d){c(i,d,!0)}}),r(u);var K=o(u,2);Y(K,{type:"submit",get disabled(){return a(b)},children:(d,$)=>{E();var B=X();U(()=>y(B,a(b)?"Searching...":"Search")),l(d,B)},$$slots:{default:!0}}),r(p);var W=o(p,2),I=s(W),R=s(I);$e(R);var n=o(R,2),h=s(n);r(n),r(I);var P=o(I,2),S=s(P);$e(S),E(2),r(P),r(W),U(d=>y(h,`Fits in memory (<${d??""} GB)`),[()=>{var d;return((d=V.memory)==null?void 0:d.mlx_recommended_gb.toFixed(0))??80}]),ke("submit",p,d=>{d.preventDefault(),M()}),ye(R,()=>a(D),d=>c(D,d)),ye(S,()=>a(Z),d=>c(Z,d)),l(t,g)},$$slots:{default:!0}});var q=o(T,2);{var me=t=>{var v=Ze(),g=s(v,!0);r(v),U(()=>y(g,a(f))),l(t,v)};F(q,t=>{a(f)&&t(me)})}var J=o(q,2);{var le=t=>{var v=qe(),g=s(v),p=s(g);r(g);var u=o(g,2);he(u,21,()=>a(H),_=>_.model_id,(_,C)=>{Je(_,{get model(){return a(C)},onUse:z,onDeleted:O})}),r(u),r(v),U(()=>y(p,`Search Results (${a(H).length??""})`)),l(t,v)},fe=t=>{var v=L(),g=k(v);{var p=u=>{var _=Ke();l(u,_)};F(g,u=>{a(i)&&!a(b)&&u(p)},!0)}l(t,v)};F(J,t=>{a(x).length>0?t(le):t(fe,!1)})}var ne=o(J,2);{var de=t=>{var v=Xe(),g=s(v),p=s(g);r(g);var u=o(g,2);he(u,21,()=>a(m),_=>_.model_id,(_,C)=>{ge(_,{class:"p-4",children:(K,W)=>{var I=We(),R=s(I),n=s(R),h=s(n,!0);r(n);var P=o(n,2),S=s(P);r(P),r(R);var d=o(R,2);Y(d,{size:"sm",onclick:()=>z(a(C).model_id),children:($,B)=>{E();var Q=X("Use");l($,Q)},$$slots:{default:!0}}),r(I),U($=>{y(h,a(C).model_id),y(S,`${$??""} GB`)},[()=>a(C).size_gb.toFixed(2)]),l(K,I)},$$slots:{default:!0}})}),r(u),r(v),U(()=>y(p,`Downloaded Models (${a(m).length??""})`)),l(t,v)},ue=t=>{var v=L(),g=k(v);{var p=u=>{var _=Ye();l(u,_)};F(g,u=>{a(i)||u(p)},!0)}l(t,v)};F(ne,t=>{a(m).length>0&&!a(i)?t(de):t(ue,!1)})}r(G),l(N,G),Me()}export{mt as component};
