(()=>{"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){const{__}=e.i18n,{registerPlugin:s}=e.plugins,{PluginDocumentSettingPanel:i}=e.editPost,{Component:l}=e.element,{Spinner:o,CheckboxControl:n}=e.components,{withSelect:a,withDispatch:c,dispatch:d}=e.data,{compose:p}=e.compose,m=p(c((function(e,t){return{setRestrictedLevelsValue:function(t){e("pmpro/require-membership").setRestrictedLevels(t),e("core/editor").editPost({meta:{pmpro_force_save_enable:"1"}})}}})),a((function(e,t){return{restrictedLevels:e("pmpro/require-membership").getRestrictedLevels()}})))((function(e){const s=e.levels.map((s=>(0,t.createElement)(n,{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)("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):s)}));function v(){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 u=v();e.data.subscribe((function(){let e=v(),t=u&&!e;u=e,t&&d("pmpro/require-membership").saveRestrictedLevels()})),s("pmpro-sidebar",{icon:"lock",render:class extends l{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)(o,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)(m,{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)(i,{name:"pmpro-sidebar-panel",title:__("Require Membership","paid-memberships-pro")},e)}}})}(window.wp)})();