(()=>{"use strict";var e,t={4(e,t,a){const o=window.wp.blocks,r=window.wp.blockEditor,n=window.wp.components,l=window.wp.i18n,s=window.wp.element,i=window.wp.apiFetch;var c=a.n(i);const d=JSON.parse('{"UU":"gateway/grid"}'),g=window.ReactJSXRuntime,w=["gateway/facet-group","gateway/heading-group","gateway/row-group"],u=[["gateway/facet-group",{}],["gateway/heading-group",{}],["gateway/row-group",{}]];(0,o.registerBlockType)(d.UU,{edit:({attributes:e,setAttributes:t})=>{const{collectionSlug:a}=e,[o,i]=(0,s.useState)([]),[d,p]=(0,s.useState)(!0),[y,h]=(0,s.useState)(null),[f,v]=(0,s.useState)([]),[b,_]=(0,s.useState)(!1),[x,m]=(0,s.useState)(null),j=(0,r.useBlockProps)({className:"gateway-grid"});(0,s.useEffect)(()=>{t({previewTotalCount:f.length,previewFilteredCount:f.length,previewIsConfigured:!!a&&!b&&!x})},[f.length,a,b,x]),(0,s.useEffect)(()=>{(async()=>{p(!0),h(null);try{const e=await c()({path:"/gateway/v1/collections"});i(e.data||e||[])}catch(e){console.error("[Gateway Grid] Failed to load collections:",e),h(e.message)}finally{p(!1)}})()},[]);const k=(0,s.useCallback)(async e=>{if(!e)return v([]),void m(null);_(!0),m(null);try{const t=await c()({path:`/gateway/v1/collections/${e}`});if(!t||!1===t.success)throw new Error(t?.message||"Collection not found");const a=t.routes?.endpoint;if(!a)throw new Error(`No REST endpoint found for "${e}"`);const o=await c()({path:a.replace(/^.*\/wp-json/,"")}),r=o.data?.items??o.items??o??[];v(r)}catch(e){console.error("[Gateway Grid] Failed to load records:",e),m(e.message),v([])}finally{_(!1)}},[]);(0,s.useEffect)(()=>{k(a)},[a,k]);const S=[{label:(0,l.__)("Select a collection…","gateway"),value:""},...o.map(e=>({label:e.titlePlural||e.title||e.key||e.slug,value:e.key||e.slug}))];return(0,g.jsxs)(g.Fragment,{children:[(0,g.jsx)(r.InspectorControls,{children:(0,g.jsxs)(n.PanelBody,{title:(0,l.__)("Grid Settings","gateway"),children:[d?(0,g.jsxs)("p",{children:[(0,g.jsx)(n.Spinner,{})," ",(0,l.__)("Loading collections…","gateway")]}):y?(0,g.jsx)("p",{style:{color:"#cc1818"},children:y}):(0,g.jsx)(n.SelectControl,{label:(0,l.__)("Collection","gateway"),value:a,options:S,onChange:e=>t({collectionSlug:e}),help:(0,l.__)("Changing the collection resets the filter and reloads all records.","gateway")}),a&&!d&&(0,g.jsx)(n.Button,{variant:"secondary",onClick:()=>k(a),isBusy:b,disabled:b,style:{marginTop:"8px"},children:b?(0,l.__)("Refreshing…","gateway"):(0,l.__)("Refresh Records","gateway")})]})}),(0,g.jsx)("div",{...j,children:(0,g.jsx)(r.InnerBlocks,{allowedBlocks:w,template:u})})]})},save:({attributes:e})=>{const{collectionSlug:t}=e,a=r.useBlockProps.save({className:"gateway-grid"}),o=JSON.stringify({collectionSlug:t,data:[],records:[],filters:{},loading:!0,error:null});return(0,g.jsxs)("div",{...a,"data-wp-interactive":"gateway/grid","data-wp-context":o,"data-wp-init":"callbacks.init",children:[(0,g.jsx)("p",{className:"gateway-grid__loading","data-wp-bind--hidden":"state.isNotLoading",children:(0,l.__)("Loading records…","gateway")}),(0,g.jsx)("p",{className:"gateway-grid__error","data-wp-bind--hidden":"state.hasNoError","data-wp-text":"state.error"}),(0,g.jsx)("div",{className:"gateway-grid__body","data-wp-bind--hidden":"state.loading",children:(0,g.jsx)(r.InnerBlocks.Content,{})})]})}})}},a={};function o(e){var r=a[e];if(void 0!==r)return r.exports;var n=a[e]={exports:{}};return t[e](n,n.exports,o),n.exports}o.m=t,e=[],o.O=(t,a,r,n)=>{if(!a){var l=1/0;for(d=0;d<e.length;d++){for(var[a,r,n]=e[d],s=!0,i=0;i<a.length;i++)(!1&n||l>=n)&&Object.keys(o.O).every(e=>o.O[e](a[i]))?a.splice(i--,1):(s=!1,n<l&&(l=n));if(s){e.splice(d--,1);var c=r();void 0!==c&&(t=c)}}return t}n=n||0;for(var d=e.length;d>0&&e[d-1][2]>n;d--)e[d]=e[d-1];e[d]=[a,r,n]},o.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return o.d(t,{a:t}),t},o.d=(e,t)=>{for(var a in t)o.o(t,a)&&!o.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e={85:0,849:0};o.O.j=t=>0===e[t];var t=(t,a)=>{var r,n,[l,s,i]=a,c=0;if(l.some(t=>0!==e[t])){for(r in s)o.o(s,r)&&(o.m[r]=s[r]);if(i)var d=i(o)}for(t&&t(a);c<l.length;c++)n=l[c],o.o(e,n)&&e[n]&&e[n][0](),e[n]=0;return o.O(d)},a=globalThis.webpackChunkgateway_block_types=globalThis.webpackChunkgateway_block_types||[];a.forEach(t.bind(null,0)),a.push=t.bind(null,a.push.bind(a))})();var r=o.O(void 0,[849],()=>o(4));r=o.O(r)})();