(()=>{"use strict";window.React;const e=window.wp.element,a=window.wp.components,t=window.wp.i18n,n=["","php","html","htm","tmp"],l={pdf:"dashicons-pdf",zip:"dashicons-media-archive",gz:"dashicons-media-archive",tar:"dashicons-media-archive",rar:"dashicons-media-archive",xls:"dashicons-media-spreadsheet",xlsx:"dashicons-media-spreadsheet",csv:"dashicons-media-spreadsheet",mp4:"dashicons-media-video",mov:"dashicons-media-video",avi:"dashicons-media-video",mp3:"dashicons-media-audio",wav:"dashicons-media-audio",jpg:"dashicons-media-image",jpeg:"dashicons-media-image",png:"dashicons-media-image",gif:"dashicons-media-image",webp:"dashicons-media-image",svg:"dashicons-media-image"};function o({item:s,depth:c,onSelect:m}){const[d,i]=(0,e.useState)(!1),[r,p]=(0,e.useState)([]),[f,w]=(0,e.useState)(!1),u={paddingLeft:12+22*c+"px"},h=(0,e.useCallback)(()=>{d?i(!1):(w(!0),window.jQuery.post(window.dlmFileBrowser.ajaxUrl,{action:"download_monitor_list_files",path:s.path,security:window.dlmFileBrowser.nonce},e=>{w(!1),e.success&&p(e.data),i(!0)}))},[d,s.path]);return"folder"===s.type?React.createElement("li",{className:"dlm-fb-item dlm-fb-folder"+(d?" dlm-fb-folder--open":"")},React.createElement("button",{type:"button",className:"dlm-fb-item__row dlm-fb-folder__row",onClick:h,style:u},React.createElement("span",{className:"dlm-fb-item__arrow"},React.createElement("span",{className:"dashicons "+(d?"dashicons-arrow-down-alt2":"dashicons-arrow-right-alt2")})),React.createElement("span",{className:"dlm-fb-item__icon"},React.createElement("span",{className:"dashicons dashicons-category"})),React.createElement("span",{className:"dlm-fb-item__name"},s.name),f&&React.createElement("span",{className:"dlm-fb-item__spinner"},React.createElement(a.Spinner,null))),d&&React.createElement("ul",{className:"dlm-fb-tree"},0!==r.length||f?r.map((e,a)=>React.createElement(o,{key:a,item:e,depth:c+1,onSelect:m})):React.createElement("li",{className:"dlm-fb-item dlm-fb-empty",style:{paddingLeft:12+22*(c+1)+"px"}},(0,t.__)("No files found","download-monitor")))):n.includes(s.ext)?null:React.createElement("li",{className:"dlm-fb-item dlm-fb-file"},React.createElement("button",{type:"button",className:"dlm-fb-item__row dlm-fb-file__row",onClick:()=>m(s.path),title:s.path,style:u},React.createElement("span",{className:"dlm-fb-item__arrow dlm-fb-item__arrow--spacer"}),React.createElement("span",{className:"dlm-fb-item__icon dlm-fb-file__icon"},React.createElement("span",{className:`dashicons ${_=s.ext,l[_?.toLowerCase()]||"dashicons-media-document"}`})),React.createElement("span",{className:"dlm-fb-item__name"},s.name)));var _}function s({items:e,onSelect:a}){return e&&0!==e.length?React.createElement("ul",{className:"dlm-fb-tree dlm-fb-tree--root"},e.map((e,t)=>React.createElement(o,{key:t,item:e,depth:0,onSelect:a}))):React.createElement("p",{className:"dlm-fb-empty dlm-fb-empty--root"},(0,t.__)("No files found.","download-monitor"))}function c({onClose:n,onSelect:l}){const[o,c]=(0,e.useState)([]),[m,d]=(0,e.useState)(!0),[i,r]=(0,e.useState)(null);return(0,e.useEffect)(()=>{window.jQuery.post(window.dlmFileBrowser.ajaxUrl,{action:"download_monitor_list_files",path:"",security:window.dlmFileBrowser.nonce},e=>{d(!1),e.success?c(e.data):r((0,t.__)("Could not load files.","download-monitor"))}).fail(()=>{d(!1),r((0,t.__)("Could not load files.","download-monitor"))})},[]),React.createElement(a.Modal,{title:(0,t.__)("Browse for a file","download-monitor"),onRequestClose:n,className:"dlm-file-browser-modal",size:"medium"},React.createElement("div",{className:"dlm-fb-wrap"},React.createElement("p",{className:"dlm-fb-desc"},(0,t.__)("Choose files under your site uploads directory. Click a file to add it as a download source.","download-monitor")),React.createElement("div",{className:"dlm-fb-tree-container"},m&&React.createElement("div",{className:"dlm-fb-loading"},React.createElement(a.Spinner,null)),i&&React.createElement("p",{className:"dlm-fb-error"},i),!m&&!i&&React.createElement(s,{items:o,onSelect:l})),React.createElement("div",{className:"dlm-fb-footer"},React.createElement(a.Button,{variant:"secondary",onClick:n},(0,t.__)("Cancel","download-monitor")))))}let m=null;function d(){const[a,t]=(0,e.useState)({open:!1,target:null});return m=t,a.open?React.createElement(c,{onClose:()=>{t({open:!1,target:null})},onSelect:e=>{const n=window.jQuery(a.target),l=n.val().trim();n.val(l?l+"\n"+e:e),t({open:!1,target:null})}}):null}document.addEventListener("DOMContentLoaded",()=>{const a=document.getElementById("dlm-file-browser-root");a&&(0,e.createRoot)(a).render(React.createElement(d,null))}),window.dlmOpenFileBrowser=e=>{m&&m({open:!0,target:e})}})();