(()=>{"use strict";var t={d:(e,o)=>{for(var s in o)t.o(o,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:o[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{useCurrentPost:()=>u,useCurrentPostTaxonomies:()=>l,usePost:()=>p,usePostTypes:()=>c});const o=window.lodash,s=window.wp.data,r=window.wp.editor,n=window.wp.coreData,a=window.wp.blockEditor,i=window.wp.blocks,d=window.wp.element,c=()=>(0,s.useSelect)(t=>t(n.store).getPostTypes(),[])||[],p=(t,e)=>(0,s.useSelect)(o=>{const{getEditedEntityRecord:s,getEntityRecords:r,hasFinishedResolution:a}=o(n.store),i=["postType",e,t],d=s(...i),c=r("taxonomy","post_tag",{context:"view",per_page:100,include:d.tags}),p=c?c.map(t=>t.name):[];return p?.length&&(d.tagNames=p),{hasResolvedPost:a("getEditedEntityRecord",i),post:d}}),u=(t={})=>{const{context:e={}}=t,[c,p]=(0,d.useState)(null),{post:u,postType:l,postId:y,hasResolvedPost:m,hasResolvedPostTags:w}=(0,s.useSelect)(t=>{const{getEditedEntityRecord:o,hasFinishedResolution:s,getEntityRecords:a,getMedia:i}=t(n.store),{getCurrentPostId:d,getCurrentPostType:c}=t(r.store),{postType:p=c(),postId:u=d()}=e,l=["postType",p,u],y=o(...l)||{},m=i(y.featured_media);y.featured_image_url=m?m.source_url:"";const w=["taxonomy","post_tag",{context:"view",per_page:100,include:y.tags}],g=a(...w),h=g?g.map(t=>t.name):[];return h?.length&&(y.tagNames=h),{post:y,hasResolvedPost:s("getEditedEntityRecord",l),hasResolvedPostTags:s("getEntityRecords",w),postType:p,postId:u}},[e]);(0,d.useEffect)(()=>{u&&m&&w&&(c&&(0,o.isEqual)(c,u)||p(u))},[m,w,u]);const{editEntityRecord:g}=(0,s.useDispatch)(n.store),{resetEditorBlocks:h}=(0,s.useDispatch)(r.store),{insertBlocks:T,resetBlocks:E}=(0,s.useDispatch)(a.store),P=(0,d.useCallback)((t,e)=>{"content"===t&&c.content&&(E([]),h()),setTimeout(()=>{if("content"===t){const t=(0,i.rawHandler)({HTML:e});T(t)}else g("postType",l,y,{[t]:e})},50)},[c]);return{post:c,setPost:P}},l=()=>{const{post:t,setPost:e}=u(),{saveEntityRecord:o}=(0,s.useDispatch)(n.store),a=async(t,e)=>{try{return await o("taxonomy",t,e,{throwOnError:!0})}catch(t){if("term_exists"!==t.code)throw t;return{id:t.data.term_id,name:e.name}}},{terms:i,taxonomies:d}=(0,s.useSelect)(e=>{const{getEditedPostAttribute:o}=e(r.store),{getTaxonomy:s}=e(n.store),a=t?._links["wp:term"]?.map(t=>t.taxonomy),i=a?.reduce((t,e)=>{const r=s(e),n=r?o(r.rest_base):[];return t[e]=n,t},{});return{taxonomies:a,terms:i}});return{terms:i,taxonomies:d,setTaxonomyTerms:async(t,o)=>{const{select:s}=wp.data,{getTaxonomy:r}=s(n.store),{rest_base:d}=r(t)||{},c=i[t],p=[];for(let e=0;e<o.length;e++){const s=o[e];try{const e=await a(t,{name:s});c.includes(e.id)||p.push(e.id)}catch(t){}}e(d,p)}}};(window.aicp=window.aicp||{}).hooks=e})();