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