(()=>{"use strict";var e={d:(t,n)=>{for(var l in n)e.o(n,l)&&!e.o(t,l)&&Object.defineProperty(t,l,{enumerable:!0,get:n[l]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{ReusableBlocksMenuItems:()=>h,store:()=>m});var n={};e.r(n),e.d(n,{__experimentalConvertBlockToStatic:()=>a,__experimentalConvertBlocksToReusable:()=>i,__experimentalDeleteReusableBlock:()=>u,__experimentalSetEditingReusableBlock:()=>d});var l={};e.r(l),e.d(l,{__experimentalIsEditingReusableBlock:()=>_});const c=window.wp.data,o=window.wp.blockEditor,r=window.wp.blocks,s=window.wp.i18n,a=e=>({registry:t})=>{const n=t.select(o.store).getBlock(e),l=t.select("core").getEditedEntityRecord("postType","wp_block",n.attributes.ref),c=(0,r.parse)("function"==typeof l.content?l.content(l):l.content);t.dispatch(o.store).replaceBlocks(n.clientId,c)},i=(e,t,n)=>async({registry:l,dispatch:c})=>{const a="unsynced"===n?{wp_pattern_sync_status:n}:void 0,i={title:t||(0,s.__)("Untitled Pattern block"),content:(0,r.serialize)(l.select(o.store).getBlocksByClientId(e)),status:"publish",meta:a},u=await l.dispatch("core").saveEntityRecord("postType","wp_block",i);if("unsynced"===n)return;const d=(0,r.createBlock)("core/block",{ref:u.id});l.dispatch(o.store).replaceBlocks(e,d),c.__experimentalSetEditingReusableBlock(d.clientId,!0)},u=e=>async({registry:t})=>{if(!t.select("core").getEditedEntityRecord("postType","wp_block",e))return;const n=t.select(o.store).getBlocks().filter((t=>(0,r.isReusableBlock)(t)&&t.attributes.ref===e)).map((e=>e.clientId));n.length&&t.dispatch(o.store).removeBlocks(n),await t.dispatch("core").deleteEntityRecord("postType","wp_block",e)};function d(e,t){return{type:"SET_EDITING_REUSABLE_BLOCK",clientId:e,isEditing:t}}const p=(0,c.combineReducers)({isEditingReusableBlock:function(e={},t){return"SET_EDITING_REUSABLE_BLOCK"===t?.type?{...e,[t.clientId]:t.isEditing}:e}});function _(e,t){return e.isEditingReusableBlock[t]}const m=(0,c.createReduxStore)("core/reusable-blocks",{actions:n,reducer:p,selectors:l});(0,c.register)(m);const k=window.wp.element,b=window.wp.components,y=window.wp.primitives,w=(0,k.createElement)(y.SVG,{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24"},(0,k.createElement)(y.Path,{d:"M21.3 10.8l-5.6-5.6c-.7-.7-1.8-.7-2.5 0l-5.6 5.6c-.7.7-.7 1.8 0 2.5l5.6 5.6c.3.3.8.5 1.2.5s.9-.2 1.2-.5l5.6-5.6c.8-.7.8-1.9.1-2.5zm-1 1.4l-5.6 5.6c-.1.1-.3.1-.4 0l-5.6-5.6c-.1-.1-.1-.3 0-.4l5.6-5.6s.1-.1.2-.1.1 0 .2.1l5.6 5.6c.1.1.1.3 0 .4zm-16.6-.4L10 5.5l-1-1-6.3 6.3c-.7.7-.7 1.8 0 2.5L9 19.5l1.1-1.1-6.3-6.3c-.2 0-.2-.2-.1-.3z"})),g=window.wp.notices,E=window.wp.coreData;function B({clientIds:e,rootClientId:t}){const[n,l]=(0,k.useState)("unsynced"),[a,i]=(0,k.useState)(!1),[u,d]=(0,k.useState)(""),p=(0,c.useSelect)((n=>{var l;const{canUser:c}=n(E.store),{getBlocksByClientId:s,canInsertBlockType:a}=n(o.store),i=null!==(l=s(e))&&void 0!==l?l:[];return!(1===i.length&&i[0]&&(0,r.isReusableBlock)(i[0])&&n(E.store).getEntityRecord("postType","wp_block",i[0].attributes.ref))&&a("core/block",t)&&i.every((e=>!!e&&e.isValid&&(0,r.hasBlockSupport)(e.name,"reusable",!0)))&&!!c("create","blocks")}),[e,t]),{__experimentalConvertBlocksToReusable:_}=(0,c.useDispatch)(m),{createSuccessNotice:y,createErrorNotice:B}=(0,c.useDispatch)(g.store),C=(0,k.useCallback)((async function(t){try{await _(e,t,n),y("fully"===n?(0,s.__)("Synced Pattern created."):(0,s.__)("Unsynced Pattern created."),{type:"snackbar"})}catch(e){B(e.message,{type:"snackbar"})}}),[_,e,n,y,B]);return p?(0,k.createElement)(o.BlockSettingsMenuControls,null,(({onClose:e})=>(0,k.createElement)(k.Fragment,null,(0,k.createElement)(b.MenuItem,{icon:w,onClick:()=>i(!0)},(0,s.__)("Create pattern/reusable block")),a&&(0,k.createElement)(b.Modal,{title:(0,s.__)("Create pattern"),onRequestClose:()=>{i(!1),d("")},overlayClassName:"reusable-blocks-menu-items__convert-modal"},(0,k.createElement)("form",{onSubmit:t=>{t.preventDefault(),C(u),i(!1),d(""),e()}},(0,k.createElement)(b.__experimentalVStack,{spacing:"5"},(0,k.createElement)(o.ReusableBlocksRenameHint,null),(0,k.createElement)(b.TextControl,{__nextHasNoMarginBottom:!0,label:(0,s.__)("Name"),value:u,onChange:d,placeholder:(0,s.__)("My pattern")}),(0,k.createElement)(b.ToggleControl,{label:(0,s.__)("Synced"),help:(0,s.__)("Editing the pattern will update it anywhere it is used."),checked:"fully"===n,onChange:()=>{l("fully"===n?"unsynced":"fully")}}),(0,k.createElement)(b.__experimentalHStack,{justify:"right"},(0,k.createElement)(b.Button,{variant:"tertiary",onClick:()=>{i(!1),d("")}},(0,s.__)("Cancel")),(0,k.createElement)(b.Button,{variant:"primary",type:"submit"},(0,s.__)("Create"))))))))):null}const C=window.wp.url,f=function({clientId:e}){const{canRemove:t,isVisible:n,innerBlockCount:l}=(0,c.useSelect)((t=>{const{getBlock:n,canRemoveBlock:l,getBlockCount:c}=t(o.store),{canUser:s}=t(E.store),a=n(e);return{canRemove:l(e),isVisible:!!a&&(0,r.isReusableBlock)(a)&&!!s("update","blocks",a.attributes.ref),innerBlockCount:c(e)}}),[e]),{__experimentalConvertBlockToStatic:a}=(0,c.useDispatch)(m);return n?(0,k.createElement)(o.BlockSettingsMenuControls,null,(0,k.createElement)(b.MenuItem,{href:(0,C.addQueryArgs)("edit.php",{post_type:"wp_block"})},(0,s.__)("Manage Patterns")),t&&(0,k.createElement)(b.MenuItem,{onClick:()=>a(e)},l>1?(0,s.__)("Detach patterns"):(0,s.__)("Detach pattern"))):null},h=(0,c.withSelect)((e=>{const{getSelectedBlockClientIds:t}=e(o.store);return{clientIds:t()}}))((function({clientIds:e,rootClientId:t}){return(0,k.createElement)(k.Fragment,null,(0,k.createElement)(B,{clientIds:e,rootClientId:t}),1===e.length&&(0,k.createElement)(f,{clientId:e[0]}))}));(window.wp=window.wp||{}).reusableBlocks=t})(); //# sourceMappingURL=index.min.js.map