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