{t.onload=()=>{n(t.result)},t.readAsText(e)}))}(e);let n;try{n=JSON.parse(t)}catch(e){throw new Error("Invalid JSON file")}if("wp_block"!==n.__file||!n.title||!n.content||"string"!=typeof n.title||"string"!=typeof n.content||n.syncStatus&&"string"!=typeof n.syncStatus)throw new Error("Invalid pattern JSON file");const o=await u()({path:"/wp/v2/types/wp_block"});return await u()({path:`/wp/v2/${o.rest_base}`,data:{title:n.title,content:n.content,status:"publish",meta:"unsynced"===n.syncStatus?{wp_pattern_sync_status:n.syncStatus}:void 0},method:"POST"})};const v=(0,f.withInstanceId)((function({instanceId:e,onUpload:t}){const a="list-reusable-blocks-import-form-"+e,s=(0,o.useRef)(),[i,l]=(0,o.useState)(!1),[c,p]=(0,o.useState)(null),[d,u]=(0,o.useState)(null);return(0,n.createElement)("form",{className:"list-reusable-blocks-import-form",onSubmit:e=>{e.preventDefault(),d&&(l({isLoading:!0}),_(d).then((e=>{s&&(l(!1),t(e))})).catch((e=>{if(!s)return;let t;switch(e.message){case"Invalid JSON file":t=(0,r.__)("Invalid JSON file");break;case"Invalid pattern JSON file":t=(0,r.__)("Invalid pattern JSON file");break;default:t=(0,r.__)("Unknown error")}l(!1),p(t)})))},ref:s},c&&(0,n.createElement)(b.Notice,{status:"error",onRemove:()=>{p(null)}},c),(0,n.createElement)("label",{htmlFor:a,className:"list-reusable-blocks-import-form__label"},(0,r.__)("File")),(0,n.createElement)("input",{id:a,type:"file",onChange:e=>{u(e.target.files[0]),p(null)}}),(0,n.createElement)(b.Button,{type:"submit",isBusy:i,disabled:!d||i,variant:"secondary",className:"list-reusable-blocks-import-form__button"},(0,r._x)("Import","button label")))}));const y=function({onUpload:e}){return(0,n.createElement)(b.Dropdown,{popoverProps:{placement:"bottom-start"},contentClassName:"list-reusable-blocks-import-dropdown__content",renderToggle:({isOpen:e,onToggle:t})=>(0,n.createElement)(b.Button,{"aria-expanded":e,onClick:t,variant:"primary"},(0,r.__)("Import from JSON")),renderContent:({onClose:t})=>(0,n.createElement)(v,{onUpload:(0,f.pipe)(t,e)})})};document.body.addEventListener("click",(e=>{e.target.classList.contains("wp-list-reusable-blocks__export")&&(e.preventDefault(),m(e.target.dataset.id))})),document.addEventListener("DOMContentLoaded",(()=>{const e=document.querySelector(".page-title-action");if(!e)return;const t=document.createElement("div");t.className="list-reusable-blocks__container",e.parentNode.insertBefore(t,e),(0,o.createRoot)(t).render((0,n.createElement)(y,{onUpload:()=>{const e=document.createElement("div");e.className="notice notice-success is-dismissible",e.innerHTML=`${(0,r.__)("Pattern imported successfully!")}
`;const t=document.querySelector(".wp-header-end");t&&t.parentNode.insertBefore(e,t)}}))})),(window.wp=window.wp||{}).listReusableBlocks=t})();