(()=>{"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 s in a)e.o(a,s)&&!e.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:a[s]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};const t=window.wp.blocks,a=window.wp.blockEditor,s=window.wp.components,l=window.wp.i18n,i=window.wp.element,n=window.wp.apiFetch;var o=e.n(n);const r=JSON.parse('{"UU":"gateway/gts-data-source"}'),c=window.ReactJSXRuntime;(0,t.registerBlockType)(r.UU,{edit:({attributes:e,setAttributes:t})=>{const{collectionSlug:n,namespace:r,previewItems:d,availableFields:w,fieldDefinitions:g}=e,[p,u]=(0,i.useState)(!1),[h,y]=(0,i.useState)(null),m=(0,a.useBlockProps)({className:"gateway-data-source"});return(0,i.useEffect)(()=>{n?(async()=>{u(!0),y(null);try{const a=await o()({path:`/gateway/v1/collections/${n}`}),s=a.routes?.endpoint;if(!s)throw new Error("Collection endpoint not found");const l=a.fields||{},i=Object.keys(l),r=await o()({path:s.replace("/wp-json","")}),c=r.data?.items||r.items||r||[],d=c.slice(0,3),w=i.length>0?i: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:l})}catch(e){console.error("GTS Data Source: Error fetching preview data",e),y(e.message),t({previewItems:[],availableFields:[],fieldDefinitions:{}})}finally{u(!1)}var e})():t({previewItems:[],availableFields:[],fieldDefinitions:{}})},[n]),(0,c.jsxs)(c.Fragment,{children:[(0,c.jsxs)(a.InspectorControls,{children:[(0,c.jsxs)(s.PanelBody,{title:(0,l.__)("Data Source Settings","gateway"),children:[(0,c.jsx)(s.TextControl,{label:(0,l.__)("Collection Slug","gateway"),value:n,onChange:e=>t({collectionSlug:e}),help:(0,l.__)("Enter the slug of the Gateway collection to use as a data source","gateway")}),(0,c.jsx)(s.TextControl,{label:(0,l.__)("Store Namespace","gateway"),value:r,onChange:e=>t({namespace:e}),help:(0,l.__)("Unique namespace for the interactivity store (e.g., gateway/my-data)","gateway")})]}),w.length>0&&(0,c.jsxs)(s.PanelBody,{title:(0,l.__)("Available Fields","gateway"),initialOpen:!1,children:[(0,c.jsx)("p",{className:"components-base-control__help",children:Object.keys(g||{}).length>0?(0,l.__)("Fields from collection definition:","gateway"):(0,l.__)("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",{...m,children:(0,c.jsxs)("div",{className:"gateway-data-source-editor",children:[(0,c.jsxs)("div",{className:"gateway-data-source-header",children:[(0,c.jsx)("h3",{children:(0,l.__)("GTS Data Source","gateway")}),n?(0,c.jsxs)(c.Fragment,{children:[(0,c.jsxs)("p",{children:[(0,l.__)("Collection:","gateway")," ",(0,c.jsx)("strong",{children:n})]}),p&&(0,c.jsxs)("p",{className:"gateway-data-source-loading",children:[(0,c.jsx)(s.Spinner,{})," ",(0,l.__)("Loading preview...","gateway")]}),h&&(0,c.jsx)(s.Notice,{status:"error",isDismissible:!1,children:h}),!p&&!h&&d.length>0&&(0,c.jsxs)("p",{className:"gateway-data-source-preview-count",children:[(0,l.__)("Preview:","gateway")," ",d.length," ",(0,l.__)("items loaded","gateway")]})]}):(0,c.jsx)("p",{className:"gateway-data-source-notice",children:(0,l.__)("Configure collection slug in block settings →","gateway")})]}),(0,c.jsx)("div",{className:"gateway-data-source-content",children:(0,c.jsx)(a.InnerBlocks,{})})]})})]})},save:({attributes:e})=>{const{collectionSlug:t,namespace:s}=e,l=JSON.stringify({collectionSlug:t,items:[],loading:!1,error:null,searchQuery:"",searchFields:["title","slug"]});return(0,c.jsx)("div",{"data-wp-interactive":s,"data-wp-context":l,"data-wp-init":"callbacks.init",children:(0,c.jsx)(a.InnerBlocks.Content,{})})}})})();