(()=>{"use strict";const e=window.wp.blocks,t=window.wp.i18n,a=window.wp.blockEditor,l=window.wp.components,o=(window.wp.data,window.wp.element),c=JSON.parse('{"UU":"gateway/gts-collection-records"}'),s=window.ReactJSXRuntime;(0,e.registerBlockType)(c.UU,{edit:function({attributes:e,setAttributes:c,clientId:i}){const{collection:n,limit:r,orderBy:d,order:w}=e,[g,p]=(0,o.useState)([]),[_,y]=(0,o.useState)([]),[h,b]=(0,o.useState)(!0);(0,o.useEffect)(()=>{(async()=>{try{const e=await wp.apiFetch({path:"/gateway/v1/collections"});e&&Array.isArray(e)?p(e):e&&"object"==typeof e&&p(Object.values(e))}catch(e){if(console.error("Failed to fetch collections:",e),window.gatewayBindingSources){const e=Object.entries(window.gatewayBindingSources).map(([e,t])=>({key:t.collection_key,title:t.label.replace("Gateway: ",""),fields:t.fields||[]}));p(e)}}finally{b(!1)}})()},[]),(0,o.useEffect)(()=>{if(n&&g.length>0){const e=g.find(e=>e.key===n);if(e&&e.fields){const t=e.fields,a=Array.isArray(t)?t:Object.keys(t);y(a)}else y([])}else y([])},[n,g]);const u=(0,a.useBlockProps)({className:"wp-block-gateway-collection-records"}),x=[{label:(0,t.__)("Select a collection...","gateway"),value:""},...g.map(e=>({label:e.title||e.key,value:e.key}))],m=Array.isArray(_)?_:[],k=[{label:(0,t.__)("ID","gateway"),value:"id"},{label:(0,t.__)("Created Date","gateway"),value:"created_at"},{label:(0,t.__)("Updated Date","gateway"),value:"updated_at"},...m.filter(e=>!["id","created_at","updated_at"].includes(e)).map(e=>({label:e.replace(/_/g," ").replace(/\b\w/g,e=>e.toUpperCase()),value:e}))],f=[{label:(0,t.__)("Descending (newest first)","gateway"),value:"DESC"},{label:(0,t.__)("Ascending (oldest first)","gateway"),value:"ASC"}];return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(a.InspectorControls,{children:[(0,s.jsxs)(l.PanelBody,{title:(0,t.__)("Collection Settings","gateway"),children:[(0,s.jsx)(l.SelectControl,{label:(0,t.__)("Collection","gateway"),value:n,options:x,onChange:e=>c({collection:e}),help:(0,t.__)("Select the collection to loop over. Child blocks can use block bindings to display record data.","gateway"),__nextHasNoMarginBottom:!0}),n&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(l.RangeControl,{label:(0,t.__)("Records to display","gateway"),value:r,onChange:e=>c({limit:e}),min:1,max:100,__nextHasNoMarginBottom:!0}),(0,s.jsx)(l.SelectControl,{label:(0,t.__)("Order by","gateway"),value:d,options:k,onChange:e=>c({orderBy:e}),__nextHasNoMarginBottom:!0}),(0,s.jsx)(l.SelectControl,{label:(0,t.__)("Order","gateway"),value:w,options:f,onChange:e=>c({order:e}),__nextHasNoMarginBottom:!0})]})]}),n&&(0,s.jsxs)(l.PanelBody,{title:(0,t.__)("Block Bindings Info","gateway"),initialOpen:!1,children:[(0,s.jsx)("p",{children:(0,t.__)("Child blocks inside this loop can use block bindings with:","gateway")}),(0,s.jsxs)("code",{style:{display:"block",padding:"8px",background:"#f0f0f0",borderRadius:"4px",fontSize:"12px",marginBottom:"12px"},children:['source: "gateway/',n,'"']}),(0,s.jsx)("p",{style:{fontSize:"12px",color:"#666"},children:(0,t.__)("The record ID is automatically provided via context. No need to specify an ID in binding args.","gateway")})]})]}),(0,s.jsxs)("div",{...u,children:[(0,s.jsxs)("div",{className:"wp-block-gateway-collection-records__header",children:[(0,s.jsx)("span",{className:"wp-block-gateway-collection-records__icon",children:(0,s.jsx)("svg",{width:"20",height:"20",viewBox:"0 0 24 24",fill:"currentColor",children:(0,s.jsx)("path",{d:"M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 17.93c-3.95-.49-7-3.85-7-7.93 0-.62.08-1.21.21-1.79L9 15v1c0 1.1.9 2 2 2v1.93zm6.9-2.54c-.26-.81-1-1.39-1.9-1.39h-1v-3c0-.55-.45-1-1-1H8v-2h2c.55 0 1-.45 1-1V7h2c1.1 0 2-.9 2-2v-.41c2.93 1.19 5 4.06 5 7.41 0 2.08-.8 3.97-2.1 5.39z"})})}),(0,s.jsxs)("span",{className:"wp-block-gateway-collection-records__title",children:[(0,t.__)("Collection Records","gateway"),n&&(0,s.jsxs)("span",{className:"wp-block-gateway-collection-records__collection",children:[" ","— ",n]})]}),n&&(0,s.jsxs)("span",{className:"wp-block-gateway-collection-records__limit",children:[r," ",(0,t.__)("records","gateway")]})]}),n?(0,s.jsxs)("div",{className:"wp-block-gateway-collection-records__content",children:[(0,s.jsx)("div",{className:"wp-block-gateway-collection-records__template-notice",children:(0,s.jsx)("p",{children:(0,t.__)("Add blocks below. They will repeat for each record. Use block bindings to display record fields.","gateway")})}),(0,s.jsx)(a.InnerBlocks,{template:[["core/paragraph",{placeholder:(0,t.__)("Add blocks with bindings here...","gateway")}]],templateLock:!1})]}):(0,s.jsx)(l.Notice,{status:"info",isDismissible:!1,children:(0,t.__)("Select a collection in the block settings to get started.","gateway")})]})]})},save:function(){return(0,s.jsx)("div",{...a.useBlockProps.save(),children:(0,s.jsx)(a.InnerBlocks.Content,{})})}})})();