import{a1 as a,cb as V,$ as n,bR as z,cc as O,cd as X,e as Y,bN as B}from"./App-CSvRzCsn.js";import"./sentry-common-2nFGZClv.js";import"./PrefsViewStore-B9QxYjkz.js";import"./index-F0fMEJXd.js";import"./index-CZYymlJf.js";import"./main-process-state-jW3BbbTQ.js";import"./index-jPDAJ20a.js";import"./report-dialog-DCdDPy0Y.js";(function(){try{var t=typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:{};t.SENTRY_RELEASE={id:"4.2.860"}}catch{}})();try{(function(){var t=typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:{},s=new t.Error().stack;s&&(t._sentryDebugIds=t._sentryDebugIds||{},t._sentryDebugIds[s]="c1ab1997-5375-4069-ae9c-826b64f35e1d",t._sentryDebugIdIdentifier="sentry-dbid-c1ab1997-5375-4069-ae9c-826b64f35e1d")})()}catch{}const G=10,Z="AIzaSyCMaeukXN65XH4rRrGTZPiT9z1K5TFf9H8",J=t=>({id:t.id,title:t.title||t.content_description,thumbnail:{width:t.media_formats.tinymp4.dims[0],height:t.media_formats.tinymp4.dims[1],url:t.media_formats.tinymp4.url},gif:{url:t.media_formats.gif.url,width:parseInt(t.media_formats.gif.dims[0]),height:parseInt(t.media_formats.gif.dims[1])},mp4:{url:t.media_formats.mp4.url,width:parseInt(t.media_formats.mp4.dims[0]),height:parseInt(t.media_formats.mp4.dims[1])}}),Q="https://tenor.googleapis.com/v2",ee={key:Z,limit:G.toString(),contentfilter:"high",media_filter:"gif,tinygif,mp4,tinymp4"};async function A(t,s={}){const p=new URLSearchParams({...ee,...s}),i=`${Q}${t}?${p}`,l=await(await fetch(i)).json();return[l.results,l.next]}async function te({filter:t,next:s}){return t?A("/search",{q:t,pos:s,ar_range:"standard"}):[[],null]}async function ne({next:t}){return A("/featured",{pos:t})}const se=Array(G).fill(0).map((t,s)=>a.jsx(V,{height:200,width:300,style:{minWidth:300}},`skeleton-${s}`)),de=({onSelect:t,onClose:s})=>{const p=n.useRef(null),i=n.useRef(null),w=n.useRef([]),[l,_]=n.useState(!1),[u,F]=n.useState([]),[P,L]=n.useState(""),[N,k]=n.useState(!0),[R,C]=n.useState(),[o,T]=n.useState(null),[f,j]=n.useState(""),v=n.useContext(z),E=()=>{if(o===null)return;const e=u[o];e&&(t(e),s())},d=n.useCallback(async({filter:e,more:r=!0,paginationPos:c=null,existingGifs:y=[]})=>{if(!r)return y;_(!0);const x=e?te:ne,m={next:c??""};e&&(m.filter=e);try{const[h,b]=await x(m);L(b);const H=y.concat(h.map(J));b||k(!1);const U=O(H,"id");F(U)}catch(h){C(h)}finally{_(!1)}},[]),[D,I]=X(e=>d(e),500),S=n.useCallback(Y(function(e){return d(e)},1e3),[d]);n.useEffect(()=>(f?D({filter:f}):(I(),d({})),()=>{I(),S.cancel?.()}),[f,D,d,I,S]);const g=e=>{T(e),e!=null&&w.current[e]?.scrollIntoView()},M=e=>{const r=p.current;switch(e.key){case"ArrowUp":{e.preventDefault();const c=r?.value.length;r?.setSelectionRange(c??null,c??null),r?.focus(),g(null);break}case"ArrowDown":o===null&&(e.preventDefault(),g(0));break;case"ArrowLeft":o!==null&&(g(Math.max(0,o-1)),e.preventDefault());break;case"ArrowRight":o!==null&&(g(Math.min(u.length-1,o+1)),e.preventDefault());break;case"Enter":E();break;case"Escape":s(),v.focusMessageInput(),e.stopPropagation();break}},K=e=>()=>{T(e),i.current?.children[e]?.scrollIntoView()},W=()=>{requestAnimationFrame(()=>{const e=i.current;if(!e)return;const r=e.scrollWidth-e.clientWidth;e.scrollLeft>=r-100&&N&&S({filter:f,paginationPos:P,existingGifs:u})})},$=()=>{if(u.length===0){if(!l)return a.jsx("div",{className:"gif-carousel empty",children:"No results. Try searching something else?"});if(R)return a.jsxs("div",{className:"gif-carousel empty",children:["Something went wrong (",R.message,"). Try searching something else?"]})}return a.jsxs("div",{ref:i,className:"gif-carousel",onScroll:W,"data-sentry-component":"renderCarousel","data-sentry-source-file":"GifPicker.tsx",children:[u.map((e,r)=>{const{width:c,height:y,url:x}=e.thumbnail,m=215,h=m/parseInt(y)*parseInt(c);return a.jsx("video",{tabIndex:0,className:B("gif",{selected:r===o}),height:m,width:h,src:x,ref:b=>{w.current[r]=b??null},autoPlay:!0,loop:!0,muted:!0,controls:!1,onClick:E,onFocus:K(r)},e.id)}),l&&a.jsx("div",{style:{paddingLeft:14,display:"flex"},children:se})]})},q=e=>{i.current&&(i.current.scrollLeft=0),j(e.target.value),k(!0)};return a.jsxs(a.Fragment,{children:[a.jsx("input",{ref:p,className:"gif-search-input",value:f,placeholder:"Search GIFs...",autoFocus:!0,onChange:q,onKeyDown:M}),$()]})};export{se as SkeletonLoader,de as default};
//# sourceMappingURL=GifPicker-Coe9Ztp8.js.map
