!function(){"use strict";var e={n:function(o){var t=o&&o.__esModule?function(){return o.default}:function(){return o};return e.d(t,{a:t}),t},d:function(o,t){for(var r in t)e.o(t,r)&&!e.o(o,r)&&Object.defineProperty(o,r,{enumerable:!0,get:t[r]})},o:function(e,o){return Object.prototype.hasOwnProperty.call(e,o)}},o=window.wp.i18n,t=window.wp.blocks,r=window.wp.plugins,i=window.wp.primitives,n=window.ReactJSXRuntime,c=(0,n.jsx)(i.SVG,{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",children:(0,n.jsx)(i.Path,{d:"M19.5 4.5h-7V6h4.44l-5.97 5.97 1.06 1.06L18 7.06v4.44h1.5v-7Zm-13 1a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-3H17v3a.5.5 0 0 1-.5.5h-10a.5.5 0 0 1-.5-.5v-10a.5.5 0 0 1 .5-.5h3V5.5h-3Z"})}),l=window.lodash,s=window.wp.apiFetch,p=e.n(s),a=window.wp.components,d=window.wp.data,w=window.wp.blockEditor,m=window.wp.element,b=(0,a.withNotices)((({clientId:e,attributes:r,noticeOperations:i,noticeUI:c})=>{(0,m.useEffect)((()=>{r.file&&u(r.file)}),[]);const[l,s]=(0,m.useState)(!1),{replaceBlocks:b}=(0,d.useDispatch)("core/block-editor"),u=r=>{s(!0);const c=r[0];if("application/json"!==c.type){const e=[(0,n.jsx)("strong",{children:c.name},"filename"),": ",(0,o.__)("Sorry, only JSON files are supported here.","blocks-export-import")];return i.removeAllNotices(),i.createErrorNotice(e),void s(!1)}const l=new FileReader;l.onload=async()=>{let r;try{r=JSON.parse(l.result)}catch(e){return i.removeAllNotices(),i.createErrorNotice((0,o.__)("Invalid JSON file","blocks-export-import")),void s(!1)}if(r.__file&&r.content&&"wp_export"===r.__file&&(r=(0,t.parse)(r.content)),r.__file&&r.content&&"wp_block"===r.__file){const e=await p()({path:"/wp/v2/types/wp_block"}),n=await p()({path:`/wp/v2/${e.rest_base}`,data:{title:r.title||(0,o.__)("Untitled Reusable Block","blocks-export-import"),content:r.content,status:"publish"},method:"POST"});if(!n.id)return i.removeAllNotices(),i.createErrorNotice((0,o.__)("Invalid Reusable Block JSON file","blocks-export-import")),void s(!1);r=`\x3c!-- wp:block { "ref": ${n.id} } /--\x3e`,r=(0,t.parse)(r)}b(e,r),s(!1)},l.readAsText(c)},_=(0,w.useBlockProps)();return l?(0,n.jsx)("div",{..._,children:(0,n.jsx)(a.Placeholder,{children:(0,n.jsx)(a.Spinner,{})})}):(0,n.jsx)("div",{..._,children:(0,n.jsxs)(a.Placeholder,{label:(0,o.__)("Import Blocks from JSON","blocks-export-import"),instructions:(0,o.__)("Upload JSON file from your device.","blocks-export-import"),icon:"category",notices:c,children:[(0,n.jsx)(a.FormFileUpload,{accept:"text/json",onChange:e=>u(e.target.files),isSecondary:!0,children:(0,o.__)("Upload","blocks-export-import")}),(0,n.jsx)(a.DropZone,{label:(0,o.__)("Import from JSON","blocks-export-import"),onFilesDrop:u})]})})}));(0,t.registerBlockType)("themeisle-blocks/importer",{apiVersion:2,title:(0,o.__)("Import Blocks from JSON","blocks-export-import"),description:(0,o.__)("Allows you import blocks from a JSON file.","blocks-export-import"),icon:"category",category:"widgets",keywords:[(0,o.__)("JSON","blocks-export-import"),(0,o.__)("Importer","blocks-export-import"),(0,o.__)("Import","blocks-export-import")],attributes:{file:{type:"object"}},transforms:{from:[{type:"files",isMatch:e=>"application/json"===e[0].type,transform:e=>(0,t.createBlock)("themeisle-blocks/importer",{file:e})}]},edit:b,save:()=>null}),(0,r.registerPlugin)("blocks-export-import",{render:()=>{const{blocks:e,count:r}=(0,d.useSelect)((e=>{const{getSelectedBlockCount:o,getSelectedBlock:t,getMultiSelectedBlocks:r}=e("core/block-editor");return{blocks:1===o()?t():r(),count:o()}}),[]),{createNotice:i}=(0,d.useDispatch)("core/notices"),s=async()=>{if(!e)return;let n,c;if(1===r&&"core/block"===e.name){const o=e.attributes.ref,t=await p()({path:"/wp/v2/types/wp_block"});let r;try{r=await p()({path:`/wp/v2/${t.rest_base}/${o}?context=edit`})}catch(e){return void(e.message&&i("error",e.message,{type:"snackbar"}))}const s=r.title.raw,a=r.content.raw;c=(0,l.kebabCase)(s)+".json",n={__file:"wp_block",title:s,content:a}}else c="blocks-export.json",n={__file:"wp_export",version:2,content:(0,t.serialize)(e)};const s=JSON.stringify({...n},null,2);i("success",(0,o.__)("Blocks exported.","blocks-export-import"),{type:"snackbar"}),((e,o)=>{const t=new window.Blob([o],{type:"application/json"});if(window.navigator.msSaveOrOpenBlob)window.navigator.msSaveOrOpenBlob(t,e);else{const o=document.createElement("a");o.href=URL.createObjectURL(t),o.download=e,o.style.display="none",document.body.appendChild(o),o.click(),document.body.removeChild(o)}})(c,s)};return(0,n.jsx)(w.BlockSettingsMenuControls,{children:()=>(0,n.jsx)(a.MenuItem,{icon:c,onClick:s,children:(0,o.__)("Export as JSON","blocks-export-import")})})}})}();