import{getContext as t,getElement as e,store as r}from"@wordpress/interactivity";function n(t,e){if(!t||!e)return;const r=e.split(".");let n=t;for(const t of r){if(null==n)return;n=n[t]}return n}r("gateway/context-select",{state:{get options(){const r=t(),{arrayProperty:a,valueProperty:o,labelProperty:l}=r,s=e(),c=s?.ref?.parentElement;if(!c)return[];let i=null,p=c;for(;p&&!i;){const t=p.getAttribute("data-wp-context");if(t)try{i=JSON.parse(t)}catch(t){console.error("GT Context Select: Error parsing parent context",t)}p=p.parentElement}if(!i||!a)return[];const y=n(i,a);return Array.isArray(y)?y.map((t,e)=>{const r=o?n(t,o):e,a=l?n(t,l):String(r);return{value:String(r),label:String(a)}}):(console.warn("GT Context Select: Array property does not point to an array",{arrayProperty:a,array:y}),[])}},actions:{handleChange:e=>{const r=t(),n=e.target.value;r.selectedValue=n;const a=new CustomEvent("gateway-context-select-change",{detail:{value:n,context:r},bubbles:!0});e.target.dispatchEvent(a)}},callbacks:{init:()=>{const e=t();e.arrayProperty&&e.valueProperty&&e.labelProperty||console.warn("GT Context Select: Missing required configuration. Set arrayProperty, valueProperty, and labelProperty in block settings.")}}});