(()=>{"use strict";var e={n:t=>{var o=t&&t.__esModule?()=>t.default:()=>t;return e.d(o,{a:o}),o},d:(t,o)=>{for(var n in o)e.o(o,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:o[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};const t=window.wp.blocks,o=window.wp.blockEditor,n=window.wp.serverSideRender;var r=e.n(n);const c=window.wp.components,a=window.wp.element,u=window.wp.apiFetch;var l=e.n(u);const s=window.ReactJSXRuntime;function d({value:e,onChange:t,label:o}){const[n,r]=(0,a.useState)([]),[u,d]=(0,a.useState)(!1);function i(e){d(!0);const t=e?`/ggmc/v1/products/search?q=${encodeURIComponent(e)}`:"/ggmc/v1/products/search";l()({path:t}).then(e=>{r(e.map(e=>({value:e.id,label:e.sku?`${e.name} (${e.sku})`:e.name})))}).catch(()=>r([])).finally(()=>d(!1))}return(0,a.useEffect)(()=>{i("")},[]),(0,a.useEffect)(()=>{e&&!n.find(t=>t.value===e)&&l()({path:`/ggmc/v1/products/${e}`}).then(e=>{r(t=>t.find(t=>t.value===e.id)?t:[{value:e.id,label:e.sku?`${e.name} (${e.sku})`:e.name},...t])}).catch(()=>{})},[e]),(0,s.jsx)(c.ComboboxControl,{label:o||"Product",value:e,options:n,onChange:t,onFilterValueChange:e=>i(e),__nextHasNoMarginBottom:!0})}const i=JSON.parse('{"UU":"ggmc/product-image"}');(0,t.registerBlockType)(i.UU,{edit:({attributes:e,setAttributes:t})=>(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(o.InspectorControls,{children:(0,s.jsxs)(c.PanelBody,{title:"Content",children:[(0,s.jsx)(c.SelectControl,{label:"Product Source",value:e.productSource,options:[{label:"Current Product (Auto-detect)",value:"current"},{label:"Select Product",value:"custom"}],onChange:e=>t({productSource:e})}),"custom"===e.productSource&&(0,s.jsx)(d,{value:e.productId,onChange:e=>t({productId:e})})]})}),(0,s.jsx)(r(),{block:i.UU,attributes:e})]}),save:()=>null})})();