import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";import{useEffect,useState}from"react";import getProvider from"../../utils/getProvider.js";import{useDebounce}from"../../utils/useDebounce.js";import"./Gallery.scss";const PROVIDER=getProvider();export default function Gallery({currentFile:e,setCurrentFile:a,close:l,fileTypes:n}){const[t,r]=useState([]),[s,i]=useState(e),[o,c]=useState(e),[_,d]=useState(""),g=useDebounce(_,500);function u(e){c(e),i(e)}useEffect((()=>{PROVIDER&&PROVIDER.gallery.getFiles(r,n,g)}),[g]);const[f,p]=useState(!1),m=n;return _jsx("div",{className:"gallery",onClick:l,children:_jsxs("div",{className:"gallery__container",onClick:e=>e.stopPropagation(),onDragOver:function(e){e.preventDefault(),e.stopPropagation()},onDrop:function(e){e.preventDefault(),e.stopPropagation();const a=[...e.dataTransfer?.files??[]];p(!1),a.length&&(m&&a.some((e=>!m.some((a=>e.name.toLowerCase().endsWith(a.toLowerCase())))))?console.log(`Only following file formats are acceptable: ${m.join(", ")}`):async function(e){if(PROVIDER){const a=await PROVIDER.gallery.uploadFiles(e);r((e=>[...a,...e]))}}(a))},onDragEnter:function(e){e.preventDefault(),e.stopPropagation(),"draggingOverlay"!==e.target.id&&p(!0)},onDragLeave:function(e){e.preventDefault(),e.stopPropagation(),"draggingOverlay"===e.target.id&&p(!1)},children:[f&&_jsx("div",{id:"draggingOverlay",className:"gallery__container__drag-overlay",children:_jsx("div",{children:"Drop here"})}),_jsx("i",{className:"gallery__container__close-btn fa-solid fa-sharp fa-times",onClick:l}),_jsxs("div",{className:"gallery__container__inner",children:[_jsx("div",{className:"gallery__container__topbar",children:_jsx("input",{type:"text",placeholder:"Search...",className:"gallery__container__topbar__search",onChange:e=>d(e.target.value)})}),_jsx("div",{className:"gallery__container__file-container "+(t.length>0?"gallery__container__file-container--has-files":""),children:t.length>0?PROVIDER?PROVIDER.gallery.renderFiles(t,o,u):null:_jsx("div",{className:"gallery__container__file-container__empty",children:"Drop files to upload"})})]}),o&&_jsxs("div",{className:"gallery__container__selected-file-panel",children:[_jsxs("div",{children:[function(e){if(o)return PROVIDER?PROVIDER.gallery.renderSelectedFile(o,c,e):null}(!0),o!=s&&_jsx("button",{className:"gallery__container__selected-file-panel__save-btn",onClick:function(){o&&(i(o),r((e=>e.map((e=>e.id===o.id?o:e)))),PROVIDER&&PROVIDER.gallery.updateFile(o.id,o))},children:"SAVE"})]}),_jsx("button",{className:"gallery__container__selected-file-panel__select-btn",onClick:()=>{a(o),c(""),i(null),l()},children:"SELECT"})]})]})})}