(()=>{"use strict";var e={n:t=>{var s=t&&t.__esModule?()=>t.default:()=>t;return e.d(s,{a:s}),s},d:(t,s)=>{for(var r in s)e.o(s,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:s[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};const t=window.wp.element,s=window.wp.apiFetch;var r=e.n(s);(0,window.wp.data.register)({name:"pmpro/require-membership",instantiate:()=>{const e=new Set,t={restrictedLevels:[]};function s(){for(const t of e)t()}const i={getRestrictedLevels:()=>t.restrictedLevels},l={setRestrictedLevels(e){t.restrictedLevels=e,s()},fetchRestrictedLevels(){r()({path:"pmpro/v1/post_restrictions/?post_id="+pmpro_block_editor_sidebar.post_id}).then(e=>{l.setRestrictedLevels(e.map(e=>e.membership_id)),s()}).catch(e=>{console.error(e)})},saveRestrictedLevels(){r()({path:"pmpro/v1/post_restrictions/",method:"POST",data:{post_id:pmpro_block_editor_sidebar.post_id,level_ids:t.restrictedLevels}})}};return l.fetchRestrictedLevels(),{getSelectors:()=>i,getActions:()=>l,subscribe:function(t){return e.add(t),()=>e.delete(t)}}}}),function(e,s,i){const{__}=e.i18n,{registerPlugin:l}=e.plugins,n=null!==(s=e.editor?.PluginDocumentSettingPanel)&&void 0!==s?s:null!==(i=e.editPost?.PluginDocumentSettingPanel)&&void 0!==i?i:e.editSite?.PluginDocumentSettingPanel,{Component:o}=e.element,{Spinner:a,CheckboxControl:c}=e.components,{withSelect:d,withDispatch:p,dispatch:m}=e.data,{compose:v}=e.compose,u=v(p(function(e,t){return{setRestrictedLevelsValue:function(t){e("pmpro/require-membership").setRestrictedLevels(t),e("core/editor").editPost({meta:{pmpro_force_save_enable:"1"}})}}}),d(function(e,t){return{restrictedLevels:e("pmpro/require-membership").getRestrictedLevels()}}))(function(e){const s=e.levels.map(s=>(0,t.createElement)(c,{__nextHasNoMarginBottom:!0,key:s.id,label:s.name,checked:e.restrictedLevels.includes(s.id),onChange:()=>{let t=[...e.restrictedLevels];t.includes(s.id)?t=t.filter(e=>e!==s.id):t.push(s.id),e.setRestrictedLevelsValue(t)}}));return(0,t.createElement)(t.Fragment,null,s.length>1&&(0,t.createElement)("p",null," ",__("Select","paid-memberships-pro")+": ",(0,t.createElement)("button",{className:"button-link",onClick:()=>{e.setRestrictedLevelsValue(e.levels.map(e=>e.id))}},__("All","paid-memberships-pro"))," | ",(0,t.createElement)("button",{className:"button-link",onClick:()=>{e.setRestrictedLevelsValue([])}},__("None","paid-memberships-pro"))),s.length>6?(0,t.createElement)("div",{className:"pmpro-block-inspector-scrollable"},s):(0,t.createElement)("div",{className:"pmpro-block-inspector-membershiplevels"},s))});function h(){const t=e.data.select("core/editor").isSavingPost()||e.data.select("core/editor").isAutosavingPost(),s=e.data.select("core/editor").isEditedPostSaveable(),r=e.data.select("core/editor").isPostSavingLocked(),i=e.data.select("core/editor").hasNonPostEntityChanges();return!e.data.select("core/editor").isAutosavingPost()&&t&&(t||!s||r)&&!i}var b=h();e.data.subscribe(function(){let e=h(),t=b&&!e;b=e,t&&m("pmpro/require-membership").saveRestrictedLevels()}),l("pmpro-sidebar",{icon:"lock",render:class extends o{constructor(e){super(e),this.state={levelList:[],loadingLevels:!0}}componentDidMount(){this.fetchlevels()}fetchlevels(){r()({path:"pmpro/v1/membership_levels"}).then(e=>{"object"==typeof e&&(e=Object.keys(e).map(function(t){return e[t]})),this.setState({levelList:e,loadingLevels:!1})}).catch(e=>{this.setState({levelList:e,loadingLevels:!1})})}render(){var e=(0,t.createElement)(a,null);return this.state.loadingLevels||(e=Array.isArray(this.state.levelList)?0===this.state.levelList.length?(0,t.createElement)("p",null,__("No levels found. Please create a level to restrict content.","paid-memberships-pro")):(0,t.createElement)("div",null,(0,t.createElement)(u,{label:__("Membership Levels","paid-memberships-pro"),levels:this.state.levelList})):(0,t.createElement)("p",null,__("Error retrieving membership levels.","restrict-with-stripe")+" "+this.state.levelList)),(0,t.createElement)(n,{name:"pmpro-sidebar-panel",title:__("Require Membership","paid-memberships-pro")},e)}}})}(window.wp)})();