(()=>{"use strict";const e=window.wp.element;!function(t){const{addFilter:r}=t.hooks,{createHigherOrderComponent:l}=t.compose,{Fragment:o,useState:n,useEffect:a,useRef:c,createInterpolateElement:i}=t.element,{ToolbarButton:s,Modal:m,Button:u,Flex:d,FlexItem:p,Notice:h,ExternalLink:E}=t.components,{BlockControls:b}=t.blockEditor,{apiFetch:g}=t,w=["core/image","core/video","core/audio","core/file"],f=e=>{switch(e){case"core/image":default:return"url";case"core/audio":case"core/video":return"src";case"core/file":return"href"}};r("editor.BlockEdit","my-plugin/with-protect-image-toolbar",l((t=>r=>{const l=i(LLMS.l10n.translate("This media is not protected. If you select a product here, the media will be moved to the protected uploads directory and existing links to the media will no longer work. <link>Learn More</link>"),{link:(0,e.createElement)(E,{href:"https://lifterlms.com/docs/how-protected-media-files-work/?utm_source=LifterLMS%20Plugin&utm_medium=Media&utm_campaign=Backend%20Help%20Page"})});if(!r.attributes||!r.attributes.id)return(0,e.createElement)(t,r);if(!w.includes(r.name))return(0,e.createElement)(t,r);const[_,k]=n(!1),[L,y]=n(null),v=c(null);return a((()=>{_&&v.current&&jQuery(v.current).llmsPostsSelect2()}),[_]),a((()=>{_&&g({path:`/wp/v2/media/${r.attributes.id}?context=edit`}).then((e=>{const t=e._llms_media_protection_product_id;if(!t)return void y(null);const r=["courses","memberships"];(async()=>{for(const e of r)try{const r=await g({path:`/llms/v1/${e}/${t}?context=edit`});return void y(r.title.raw)}catch(e){}y("(unknown)")})()})).catch((e=>{console.error("Unable to read media meta",e),y(null)}))}),[_,r.attributes.id]),(0,e.createElement)(o,null,(0,e.createElement)(t,r),(0,e.createElement)(b,{group:"inline"},(0,e.createElement)(s,{icon:"lock",label:"Protect Image",onClick:()=>k(!0)})),_&&(0,e.createElement)(m,{title:LLMS.l10n.translate("Select Course or Membership"),onRequestClose:()=>k(!1)},(0,e.createElement)(d,{direction:"column",gap:4},(0,e.createElement)(p,null,(0,e.createElement)("label",{htmlFor:"llms-protect-image-select"},LLMS.l10n.translate("Select a Course or Membership to protect this image:"))),(0,e.createElement)(p,null,!r.attributes[f(r.name)].includes("llms_media_id")&&(0,e.createElement)(h,{status:"warning",isDismissible:!1},l),(0,e.createElement)("select",{id:"llms-protect-image-select",ref:v,className:"llms-block-protect llms-posts-select2","data-no-view-button":"true","data-allow_clear":"false","data-post-type":"course,llms_membership"})),L&&(0,e.createElement)(p,null,LLMS.l10n.translate("Currently protected by:")," ",(0,e.createElement)("strong",null,L)),(0,e.createElement)(p,null,(0,e.createElement)(u,{isPrimary:!0,onClick:()=>{(()=>{const e=jQuery(v.current).val();g({path:`/wp/v2/media/${r.attributes.id}`,method:"POST",data:{_llms_media_protection_product_id:e}}).then((e=>{const t=f(r.name);r.setAttributes({[t]:e.source_url}),y(null)})).catch((e=>{console.error("Error updating media meta:",e)})),k(!1)})()}},"Protect Image")))))}),"withProtectImageToolbar"))}(window.wp)})();