import{j as s}from"./cm-1.0.63-CSIRHMXC.js";import{r as R}from"./redux-1.0.63-Df-eBzJn.js";import{l as e,C as y,cK as H,bz as F,c7 as z,cT as E}from"./lib-1.0.63-DJZPtMem.js";import{a as C}from"./hooks-1.0.63-CWs3kTdT.js";import{u as K}from"./useLocaleMrt-1.0.63-GdlE-vXx.js";import{l as U,m as $,n as q,o as D,p as G}from"./index-1.0.63-Cr9ps5ft.js";import{B as Q}from"./Spinner-1.0.63-zcgIyS7Z.js";import{F as L,b as X}from"./TextField-1.0.63-C35Mfi1Q.js";import{B as S}from"./cjs-1.0.63-4A2xol4v.js";import"./vendor-1.0.63-CN03Eozo.js";import"./loglevel-1.0.63-BZ7XahX3.js";import"./lodash-1.0.63-hMxolcpJ.js";import"./moment-1.0.63-C5S46NFB.js";import"./Languages-1.0.63-DsTK0dJK.js";import"./useLocaleUd-1.0.63-CjhlhnHj.js";import"./RestApi-1.0.63-BUfzocgq.js";import"./notistack-1.0.63-B49ULGrj.js";import"./iconBase-1.0.63-CFvHhC4t.js";import"./Typography-1.0.63-mecuj_Wl.js";import"./ButtonBase-1.0.63-CF10PFMS.js";import"./useEnhancedEffect-1.0.63-DnR0ZFqV.js";import"./Select-1.0.63-BMejr8a7.js";import"./Menu-1.0.63-DLh4HkF0.js";import"./ThemeProvider-1.0.63-Cq0KMgkh.js";import"./index-1.0.63-oIDff4nA.js";import"./Modal-1.0.63-fMXTbfks.js";import"./createSvgIcon-1.0.63-DWL3J-DK.js";const Y="Upload or select %s from your WordPress media library",kr=({appId:m,columnName:o,columnValue:W,columnMetaData:c,onColumnChange:v,columnMedia:d,mediaType:u,locale:_,language:f,formMode:x,storeColumn:n,storeTable:V})=>{e.debug(m,o,W,c,d,u);const A=R.useRef(null),P=C(),{mrtLocale:N,mrtIsLoading:B}=K((f==null?void 0:f.code)??y.defaultLanguage.code),k=r=>{if(e.debug(r),r!=null&&r.id&&(r!=null&&r.url)&&(r!=null&&r.mime)&&(r!=null&&r.title))return{value:r.id,media:{url:r.url,mime_type:r.mime,title:r.title}}},J=()=>{e.debug("update media");const r=z(c.data_type)!=="number";e.debug(r);let t="media",a="";switch(u){case"WP-Image":t="images(s)",a="image";break;case"WP-Audio":t="audio",a="audio";break;case"WP-Video":t="video(s)",a="video";break}e.debug(t,a);const p=window.wp.media({title:Y.replace("%s",t),button:{text:"Select"},library:{type:a},multiple:r,render:!1});e.debug("frame",p),p.on("select",function(){const l=[];if(r){const i=p.state().get("selection").toJSON();e.debug(i),i.map(w=>{e.debug(w);const I=k(w);I&&l.push(I)})}else{const i=k(p.state().get("selection").first().toJSON());i&&l.push(i)}e.debug("media",l);const h=[],g=[];for(let i=0;i<l.length;i++)h.push(l[i].value),g.push(JSON.stringify(l[i].media));e.debug(h,g),v(o,h.toString()),P(E({appId:m,columnName:o,columnValue:g}))}),p.open(),V.formModal&&window.document.body.classList.add("pp-media-modal")},M=()=>{e.debug("delete media"),v(o,null),P(E({appId:m,columnName:o,columnValue:[]}))},T=r=>{switch(u){case"WP-Image":return s.jsx("img",{src:r.url,title:r.title,alt:r.title});case"WP-Attachment":{const t=r.mime_type.split("/");return s.jsxs("a",{href:r.url,title:r.title,target:"_blank",className:"pp-hyperlink",children:[s.jsxs("div",{children:[t[0]==="image"&&s.jsx(U,{}),t[0]==="audio"&&s.jsx($,{}),t[0]==="video"&&s.jsx(q,{}),t[0]==="application"?t[1]==="pdf"?s.jsx(D,{}):s.jsx(G,{}):s.jsx(s.Fragment,{})]}),s.jsx("div",{className:"link-label",children:r.title})]})}case"WP-Audio":return s.jsx("audio",{controls:!0,children:s.jsx("source",{src:r.url,type:r.mime_type})});case"WP-Video":return s.jsx("video",{controls:!0,children:s.jsx("source",{src:r.url,type:r.mime_type})})}};return B?null:s.jsxs("fieldset",{className:`pp-fieldset media ${u}`,children:[s.jsx("legend",{children:c.formLabel}),s.jsxs(Q,{children:[s.jsxs(L,{fullWidth:!0,children:[d==null?void 0:d.map(r=>{try{if(r===null||r==="")return null;const t=JSON.parse(r);return t!=null&&t.url?s.jsx("div",{ref:A,className:"content",children:T(t)},t.url):null}catch(t){return console.error("Invalid media properties",t),s.jsx("span",{children:"Error reading media"})}}),s.jsx("input",{type:"hidden",value:W??""})]}),n.hint&&n.hint.trim()!==""&&s.jsx(L,{sx:{marginTop:"20px"},children:s.jsx(X,{sx:{marginLeft:0},children:H(n,"")})})]}),s.jsxs("div",{className:"actions",style:{...x===F.VIEW&&{display:"none"}},children:[s.jsx(S,{variant:"outlined",onClick:J,disabled:!y.appPlugin||x===F.VIEW||n.updatable===!1,children:N.select}),s.jsx(S,{variant:"outlined",onClick:M,disabled:!y.appPlugin||x===F.VIEW||n.updatable===!1,children:_.delete})]})]})};export{kr as default};
