import{j as s}from"./tanstack-1.0.74-aU0azVkW.js";import{r as T}from"./redux-1.0.74-D8bIc6ki.js";import{l as e,C as y,dh as C,al as v,bW as H,dk as _}from"./lib-1.0.74-B5IEqfHg.js";import{a as q}from"./hooks-1.0.74-NYCTWeI9.js";import{u as U}from"./useLocaleMrt-1.0.74-BUIBUKA0.js";import{l as $,m as z,n as D,o as G,p as K}from"./index-1.0.74-8lphm33F.js";import{B as Q}from"./Box-1.0.74-DEbuVvGV.js";import{F as L,b as X}from"./TextField-1.0.74-BW9Byijb.js";import{B as S}from"./ConfirmDialog-1.0.74-CXu4CqK2.js";import"./vendor-1.0.74-CN03Eozo.js";import"./utils-1.0.74-7xWhx9mc.js";import"./Languages-1.0.74-DaJcF5OJ.js";import"./useLocaleUd-1.0.74-DiRzsyAi.js";import"./RestApi-1.0.74-5XKxNzpR.js";import"./iconBase-1.0.74-Cs0vntJu.js";import"./CircularProgress-1.0.74-QfwfRJ7i.js";import"./ButtonBase-1.0.74-W6tAH7AI.js";import"./Typography-1.0.74-B4mhsevh.js";import"./useEnhancedEffect-1.0.74-B56M26dS.js";import"./Select-1.0.74-DHOnlEEH.js";import"./Menu-1.0.74-CdjCBJ4R.js";import"./ThemeProvider-1.0.74-4uzbYYLM.js";import"./index-1.0.74-DxwH6OqZ.js";import"./Portal-1.0.74-BrUPeMX9.js";import"./isHostComponent-1.0.74-DVu5iVWx.js";import"./Modal-1.0.74-BfLid1bk.js";import"./createSvgIcon-1.0.74-Cw_ZFbxi.js";import"./AdminTheme-1.0.74-BtTaJUu_.js";const Y="Upload or select %s from your WordPress media library",Er=({appId:c,columnName:a,columnValue:W,columnMetaData:f,onColumnChange:F,columnMedia:u,mediaType:m,locale:N,language:h,formMode:x,storeColumn:p,storeTable:w})=>{e.debug(c,a,W,f,u,m);const A=T.useRef(null),P=q(),{mrtLocale:V,mrtIsLoading:M}=U((h==null?void 0:h.code)??y.defaultLanguage.code),E=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}}},B=()=>{e.debug("update media");const r=H(f.data_type)!=="number";e.debug(r);let t="media",d="";switch(m){case"WP-Image":t="images(s)",d="image";break;case"WP-Audio":t="audio",d="audio";break;case"WP-Video":t="video(s)",d="video";break}e.debug(t,d);const n=window.wp.media({title:Y.replace("%s",t),button:{text:"Select"},library:{type:d},multiple:r});e.debug("frame",n),n.on("select",()=>{const l=[];if(r){const i=n.state().get("selection").toJSON();e.debug(i),i.map(I=>{e.debug(I);const k=E(I);k&&l.push(k)})}else{const i=E(n.state().get("selection").first().toJSON());i&&l.push(i)}e.debug("media",l);const o=[],g=[];for(let i=0;i<l.length;i++)o.push(l[i].value),g.push(JSON.stringify(l[i].media));e.debug(o,g),F(a,o.toString()),P(_({appId:c,columnName:a,columnValue:g}))}),n.open(),n.on("close",()=>{w.formModal&&window.document.body.classList.remove("pp-media-modal"),setTimeout(()=>{document.querySelectorAll('[id^="__wp-uploader-id-"]').forEach(o=>{o&&o.parentNode&&o.parentNode.removeChild(o)})},200)}),w.formModal&&window.document.body.classList.add("pp-media-modal")},J=()=>{e.debug("delete media"),F(a,null),P(_({appId:c,columnName:a,columnValue:[]}))},R=r=>{switch(m){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($,{}),t[0]==="audio"&&s.jsx(z,{}),t[0]==="video"&&s.jsx(D,{}),t[0]==="application"?t[1]==="pdf"?s.jsx(G,{}):s.jsx(K,{}):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 M?null:s.jsxs("fieldset",{className:`pp-fieldset media ${m}`,children:[s.jsx("legend",{children:f.formLabel}),s.jsxs(Q,{children:[s.jsxs(L,{fullWidth:!0,children:[u==null?void 0:u.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:R(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??""})]}),p.hint&&p.hint.trim()!==""&&s.jsx(L,{sx:{marginTop:"20px"},children:s.jsx(X,{sx:{marginLeft:0},children:C(p,"")})})]}),s.jsxs("div",{className:"actions",style:{...x===v.VIEW&&{display:"none"}},children:[s.jsx(S,{variant:"outlined",onClick:B,disabled:!y.appPlugin||x===v.VIEW||p.updatable===!1,children:V.select}),s.jsx(S,{variant:"outlined",onClick:J,disabled:!y.appPlugin||x===v.VIEW||p.updatable===!1,children:N.delete})]})]})};export{Er as default};
