(()=>{"use strict";var e={n:t=>{var r=t&&t.__esModule?()=>t.default:()=>t;return e.d(r,{a:r}),r},d:(t,r)=>{for(var a in r)e.o(r,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:r[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};const t=window.wp.apiFetch;var r=e.n(t);const a=window.wp.htmlEntities,o=window.wp.url,i=window.wp.i18n,s=window.wp.compose,n=window.wp.element,l=window.wp.blockEditor,d=window.wp.components,u=window.wp.hooks,c=window.ReactJSXRuntime,h="ambassadorhub/query-loop-hierarchy-filter",b=[{label:(0,i.__)("All items","ambassadorhub"),value:"all"},{label:(0,i.__)("Top-level only","ambassadorhub"),value:"parents_only"},{label:(0,i.__)("Children only","ambassadorhub"),value:"children_only"},{label:(0,i.__)("Children of current post","ambassadorhub"),value:"current"},{label:(0,i.__)("Parent of current post","ambassadorhub"),value:"parent_of_current"},{label:(0,i.__)("Children of specific post","ambassadorhub"),value:"specific"}],p={hierarchyFilter:{type:"string",default:"all"},hierarchyParentId:{type:"integer",default:0}};(0,u.addFilter)("blocks.registerBlockType",h,(e,t)=>"core/query"!==t?e:{...e,attributes:{...e.attributes,...p}});const _=({attributes:e,setAttributes:t})=>{const s=e.hierarchyFilter||"all",u=Number(e.hierarchyParentId||0),h=e?.query?.postType||"post",p="specific"===s&&!!h,[_,w]=(0,n.useState)([]),[y,m]=(0,n.useState)(!1),[f,g]=(0,n.useState)("");(0,n.useEffect)(()=>{if(!p)return void w([]);let e=!0;return m(!0),r()({path:(0,o.addQueryArgs)(`/wp/v2/${h}`,{per_page:50,orderby:"title",order:"asc",_fields:"id,title"})}).then(r=>{if(!e)return;const o=Array.isArray(r)?r.map(e=>({label:(0,a.decodeEntities)(e?.title?.rendered||String(e?.title||(0,i.__)("(no title)","ambassadorhub"))),value:String(e.id)})):[];w(o),u>0&&!o.some(e=>Number(e.value)===u)&&t({hierarchyParentId:0})}).catch(()=>{e&&w([])}).finally(()=>{e&&m(!1)}),()=>{e=!1}},[s,u,h,t,p]);const v=(0,n.useMemo)(()=>{if(!f)return _;const e=f.toLowerCase();return _.filter(t=>t.label.toLowerCase().includes(e))},[f,_]);return(0,c.jsx)(l.InspectorControls,{children:(0,c.jsxs)(d.PanelBody,{title:(0,i.__)("Hierarchy","ambassadorhub"),initialOpen:!0,children:[(0,c.jsx)(d.SelectControl,{label:(0,i.__)("Filter items by hierarchy","ambassadorhub"),value:s,options:b,onChange:e=>t({hierarchyFilter:e,hierarchyParentId:"specific"===e?u:0}),__nextHasNoMarginBottom:!0,__next40pxDefaultSize:!0}),"specific"===s&&(0,c.jsxs)(n.Fragment,{children:[y&&(0,c.jsx)(d.Spinner,{}),(0,c.jsx)(d.ComboboxControl,{label:(0,i.__)("Parent post","ambassadorhub"),value:u>0?String(u):"",options:v,onFilterValueChange:e=>g(e),onChange:e=>t({hierarchyParentId:parseInt(e,10)||0}),help:(0,i.__)("Choose the parent post whose direct children should be shown.","ambassadorhub"),__next40pxDefaultSize:!0,__nextHasNoMarginBottom:!0})]})]})})},w=(0,s.createHigherOrderComponent)(e=>t=>"core/query"!==t.name?(0,c.jsx)(e,{...t}):(0,c.jsxs)(n.Fragment,{children:[(0,c.jsx)(e,{...t}),(0,c.jsx)(_,{attributes:t.attributes,setAttributes:t.setAttributes})]}),"withHierarchyInspectorControls");(0,u.addFilter)("editor.BlockEdit",h,w)})();