(()=>{"use strict";var e={n:t=>{var a=t&&t.__esModule?()=>t.default:()=>t;return e.d(a,{a}),a},d:(t,a)=>{for(var l in a)e.o(a,l)&&!e.o(t,l)&&Object.defineProperty(t,l,{enumerable:!0,get:a[l]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};const t=window.wp.blocks,a=window.wp.blockEditor,l=window.wp.components,s=window.wp.i18n,n=window.wp.element,i=window.wp.apiFetch;var o=e.n(i);const r=JSON.parse('{"UU":"gateway/store"}'),c=window.ReactJSXRuntime;(0,t.registerBlockType)(r.UU,{edit:({attributes:e,setAttributes:t})=>{const{collectionSlug:i,namespace:r,previewItems:d,availableFields:w,fieldDefinitions:g}=e,[p,y]=(0,n.useState)(!1),[h,u]=(0,n.useState)(null),[m,j]=(0,n.useState)([]),[f,x]=(0,n.useState)(!0),[_,v]=(0,n.useState)(null),b=(0,a.useBlockProps)({className:"gateway-store"});(0,n.useEffect)(()=>{(async()=>{x(!0),v(null);try{const e=await o()({path:"/gateway/v1/collections"}),t=e.data||e||[];j(t)}catch(e){console.error("Store Block: Error fetching collections",e),v(e.message)}finally{x(!1)}})()},[]),(0,n.useEffect)(()=>{if(!i)return t({previewItems:[],availableFields:[],fieldDefinitions:{}}),void u(null);(async()=>{y(!0),u(null);try{const a=await o()({path:`/gateway/v1/collections/${i}`});if(!a||!1===a.success&&a.message)throw new Error(a.message||"Collection not found");const l=a.routes?.endpoint;if(!l)throw new Error("Collection endpoint not found");const s=a.fields||{},n=Object.keys(s),r=await o()({path:l.replace("/wp-json","")}),c=r.data?.items||r.items||r||[],d=c.slice(0,3),w=n.length>0?n:c.length>0&&(e=c[0])&&"object"==typeof e?Object.keys(e).filter(t=>"object"!=typeof e[t]||null===e[t]):[];t({previewItems:d,availableFields:w,fieldDefinitions:s}),u(null)}catch(e){console.error("Store Block: Error fetching preview data",e),u(e.message||"Failed to load collection"),t({previewItems:[],availableFields:[],fieldDefinitions:{}})}finally{y(!1)}var e})()},[i]);const S=[{label:(0,s.__)("Select a collection...","gateway"),value:""},...m.map(e=>({label:e.label||e.key||e.slug,value:e.key||e.slug}))];return(0,c.jsxs)(c.Fragment,{children:[(0,c.jsxs)(a.InspectorControls,{children:[(0,c.jsxs)(l.PanelBody,{title:(0,s.__)("Store Settings","gateway"),children:[f?(0,c.jsxs)("p",{children:[(0,c.jsx)(l.Spinner,{})," ",(0,s.__)("Loading collections...","gateway")]}):_?(0,c.jsx)(l.Notice,{status:"error",isDismissible:!1,children:_}):(0,c.jsx)(l.SelectControl,{label:(0,s.__)("Collection","gateway"),value:i,options:S,onChange:e=>t({collectionSlug:e}),help:(0,s.__)("Select the Gateway collection to use as a data source","gateway")}),(0,c.jsx)(l.TextControl,{label:(0,s.__)("Store Namespace","gateway"),value:r,onChange:e=>t({namespace:e}),help:(0,s.__)("Unique namespace for the interactivity store (e.g., gateway/my-data)","gateway")})]}),w.length>0&&(0,c.jsxs)(l.PanelBody,{title:(0,s.__)("Available Fields","gateway"),initialOpen:!1,children:[(0,c.jsx)("p",{className:"components-base-control__help",children:Object.keys(g||{}).length>0?(0,s.__)("Fields from collection definition:","gateway"):(0,s.__)("Fields discovered from collection data:","gateway")}),(0,c.jsx)("ul",{style:{margin:0,paddingLeft:"20px"},children:w.map(e=>{const t=g?.[e],a=t?.type;return(0,c.jsxs)("li",{children:[(0,c.jsx)("code",{children:e}),a&&(0,c.jsxs)("span",{style:{color:"#666",marginLeft:"4px"},children:["(",a,")"]}),t?.label&&t.label!==e&&(0,c.jsxs)("span",{style:{color:"#888",marginLeft:"4px"},children:["— ",t.label]})]},e)})})]})]}),(0,c.jsx)("div",{...b,children:(0,c.jsxs)("div",{className:"gateway-store-editor",children:[(0,c.jsxs)("div",{className:"gateway-store-header",children:[(0,c.jsx)("h3",{children:(0,s.__)("Store","gateway")}),i?(0,c.jsxs)(c.Fragment,{children:[(0,c.jsxs)("p",{children:[(0,s.__)("Collection:","gateway")," ",(0,c.jsx)("strong",{children:i})]}),p&&(0,c.jsxs)("p",{className:"gateway-store-loading",children:[(0,c.jsx)(l.Spinner,{})," ",(0,s.__)("Loading preview...","gateway")]}),h&&(0,c.jsx)(l.Notice,{status:"error",isDismissible:!1,children:h}),!p&&!h&&d.length>0&&(0,c.jsxs)("p",{className:"gateway-store-preview-count",children:[(0,s.__)("Preview:","gateway")," ",d.length," ",(0,s.__)("items loaded","gateway")]})]}):(0,c.jsx)("p",{className:"gateway-store-notice",children:(0,s.__)("Select a collection in block settings →","gateway")})]}),(0,c.jsx)("div",{className:"gateway-store-content",children:(0,c.jsx)(a.InnerBlocks,{})})]})})]})},save:({attributes:e})=>{const{collectionSlug:t,namespace:l}=e,s=JSON.stringify({collectionSlug:t,items:[],loading:!1,error:null,searchQuery:"",searchFields:["title","slug"]});return(0,c.jsx)("div",{"data-wp-interactive":l,"data-wp-context":s,"data-wp-init":"callbacks.init",children:(0,c.jsx)(a.InnerBlocks.Content,{})})}})})();