import{getContext as t,store as e}from"@wordpress/interactivity";const o={};async function r(t){if(o[t])return o[t];try{const e=await fetch(`/wp-json/gateway/v1/collections/${t}`);if(!e.ok)throw new Error(`Failed to fetch collection info: ${e.statusText}`);const r=await e.json();return o[t]=r,r}catch(t){throw console.error("GT Data Source: Error fetching collection info",t),t}}e("gateway/data-source",{state:{get items(){return t().items||[]},get loading(){return t().loading||!1},get error(){return t().error||null},get hasItems(){const e=t();return e.items&&e.items.length>0},get filteredItems(){const e=t(),o=e.items||[],r=e.searchQuery||"",s=e.searchFields||["title","slug"];if(!r)return o;const n=r.toLowerCase();return o.filter(t=>s.some(e=>{const o=t[e];return o&&String(o).toLowerCase().includes(n)}))},get totalItems(){return(t().items||[]).length},get filteredCount(){const e=t(),o=e.items||[],r=e.searchQuery||"";if(!r)return o.length;const s=e.searchFields||["title","slug"],n=r.toLowerCase();return o.filter(t=>s.some(e=>{const o=t[e];return o&&String(o).toLowerCase().includes(n)})).length}},actions:{updateSearch:e=>{t().searchQuery=e.target.value},clearSearch:()=>{t().searchQuery=""},async refresh(){const e=t(),o=e.collectionSlug;if(o){e.loading=!0,e.error=null;try{const t=await r(o),s=t.routes?.endpoint;if(!s)throw new Error("Collection endpoint not found in collection info");const n=await fetch(s);if(!n.ok)throw new Error(`Failed to fetch items: ${n.statusText}`);const i=await n.json();e.items=i.data?.items||i.items||i||[],e.loading=!1}catch(t){console.error("GT Data Source: Error fetching items",t),e.error=t.message,e.loading=!1}}else console.error("GT Data Source: No collection slug specified")},sortBy:e=>{const o=t(),r=e.target.dataset.field,s=e.target.dataset.direction||"asc";if(!r)return;const n=[...o.items||[]];n.sort((t,e)=>{const o=t[r],n=e[r];return o<n?"asc"===s?-1:1:o>n?"asc"===s?1:-1:0}),o.items=n},filterBy:e=>{const o=t(),r=e.target.dataset.field,s=e.target.value;r&&(o.filters||(o.filters={}),s?o.filters[r]=s:delete o.filters[r])}},callbacks:{init:async()=>{const e=t(),o=e.collectionSlug;if(console.log("[GT Data Source DEBUG] init() called"),console.log("[GT Data Source DEBUG] Initial context:",JSON.stringify(e,null,2)),o){console.log("[GT Data Source DEBUG] Collection slug:",o),e.items=e.items||[],e.loading=void 0===e.loading||e.loading,e.error=null,e.searchQuery=e.searchQuery||"",e.searchFields=e.searchFields||["title","slug"];try{console.log("[GT Data Source DEBUG] Fetching collection info...");const t=await r(o);console.log("[GT Data Source DEBUG] Collection info received:",JSON.stringify(t,null,2));const s=t.routes?.endpoint;if(console.log("[GT Data Source DEBUG] Endpoint:",s),!s)throw new Error("Collection endpoint not found in collection info");console.log("[GT Data Source DEBUG] Fetching items from endpoint...");const n=await fetch(s);if(console.log("[GT Data Source DEBUG] Response status:",n.status,n.statusText),!n.ok)throw new Error(`Failed to fetch items: ${n.statusText}`);const i=await n.json();console.log("[GT Data Source DEBUG] Raw API result:",JSON.stringify(i,null,2)),e.items=i.data?.items||i.items||i||[],console.log("[GT Data Source DEBUG] Items extracted:",e.items.length,"items"),console.log("[GT Data Source DEBUG] First item sample:",e.items[0]?JSON.stringify(e.items[0],null,2):"No items"),console.log("[GT Data Source DEBUG] Context after update:",JSON.stringify(e,null,2)),e.loading=!1}catch(t){console.error("GT Data Source: Error fetching items",t),console.error("[GT Data Source DEBUG] Full error:",t),e.error=t.message,e.loading=!1}}else console.warn("GT Data Source: No collection slug specified. Set collectionSlug in block attributes.")}}});