PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.5
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.5
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
← All changes | assets/RelatedProductBlockEditor.js +1 -1 1.6.2 → 1.6.5 View file →
@@ -1 +1 @@
1 -import{b as o,j as t}from"./chunks/BlockEditorTranslator.js?ver=1.6.2";import{L as z,R as j}from"./chunks/Icons.js?ver=1.6.2";import{a as N}from"./chunks/index5.js?ver=1.6.2";import{a as P}from"./chunks/add-query-args.js?ver=1.6.2";import{S as H}from"./chunks/SingleProductContext.js?ver=1.6.2";const{Button:K,TextControl:J,Spinner:Y,Icon:A}=wp.components,{useState:b,useEffect:I,useRef:B}=wp.element,T=window.fluentCartRestVars.rest;var F;const X=(F=window.fluent_cart_block_editor_asset)==null?void 0:F.placeholder_image,ee=({onSelect:e})=>{const[l,d]=b(""),[p,c]=b([]),[r,i]=b(!1),u=B(0),h=(n="")=>{const s=++u.current;i(!0),N({path:P(T.url+"/products",{with:["block_picker"],active_view:"publish",per_page:10,page:1,order_by:"ID",order_type:"DESC",search:n||void 0}),headers:{"X-WP-Nonce":T.nonce}}).then(a=>{var f;s===u.current&&c(((f=a.products)==null?void 0:f.data)||[])}).catch(()=>{s===u.current&&c([])}).finally(()=>{s===u.current&&i(!1)})};I(()=>{h()},[]);const _=B(null),m=n=>{d(n),_.current&&clearTimeout(_.current),_.current=setTimeout(()=>{h(n)},300)};return I(()=>()=>{_.current&&clearTimeout(_.current)},[]),t.jsxs("div",{className:"fct-product-picker-dropdown",children:[t.jsx(J,{placeholder:o("Search products..."),value:l,onChange:m,className:"fct-product-picker-search"}),t.jsx("div",{className:"fct-product-picker-list",children:r?t.jsx("div",{className:"fct-product-picker-loading",children:t.jsx(Y,{})}):p.length>0?p.map(n=>{var s,a;return t.jsxs("button",{className:"fct-product-picker-item",onClick:()=>e(n),type:"button",children:[t.jsx("img",{className:"fct-product-picker-item-img",src:((a=(s=n==null?void 0:n.detail)==null?void 0:s.featured_media)==null?void 0:a.url)||X,alt:n.post_title}),t.jsx("span",{className:"fct-product-picker-item-title",children:n.post_title})]},n.ID)}):t.jsx("p",{className:"fct-product-picker-empty",children:o("No products found")})})]})},M=({selectedProduct:e=null,onSelect:l,buttonLabel:d=null,changeLabel:p=null})=>{var n,s;const[c,r]=b(!1),i=B(null),u=d||o("Select Product"),h=p||o("Change Product");I(()=>{const a=f=>{i.current&&!i.current.contains(f.target)&&r(!1)};return document.addEventListener("mousedown",a),()=>document.removeEventListener("mousedown",a)},[]);const _=a=>{l(a),r(!1)},m=((s=(n=e==null?void 0:e.detail)==null?void 0:n.featured_media)==null?void 0:s.url)||(e==null?void 0:e.thumbnail)||X;return t.jsxs("div",{children:[(e==null?void 0:e.post_title)&&t.jsxs("div",{className:"fct-product-picker-selected",children:[t.jsx("img",{className:"fct-product-picker-selected-img",src:m,alt:e.post_title,width:40,height:40,style:{width:"40px",height:"40px",maxWidth:"40px",objectFit:"cover"}}),t.jsx("span",{className:"fct-product-picker-selected-title",children:e.post_title})]}),t.jsxs("div",{className:"fct-product-picker-button-wrap",ref:i,children:[t.jsxs(K,{variant:"secondary",onClick:()=>r(!c),className:"fct-product-picker-btn",children:[t.jsx(A,{icon:e!=null&&e.post_title?"edit":"search",size:16}),e!=null&&e.post_title?h:u]}),c&&t.jsx(ee,{onSelect:_})]})]})},{InspectorControls:te}=wp.blockEditor,{PanelBody:k,CheckboxControl:D,SelectControl:oe,RangeControl:R,ToggleControl:y,__experimentalToolsPanel:re,__experimentalToolsPanelItem:v,__experimentalToggleGroupControl:le,__experimentalToggleGroupControlOption:q}=wp.components,E=({attributes:e,setAttributes:l,selectedProduct:d,setSelectedProduct:p})=>{const c=r=>{l({product_id:r.ID||""}),p(r)};return t.jsx(t.Fragment,{children:t.jsxs(te,{children:[t.jsxs(k,{title:o("Product Settings"),initialOpen:!0,className:"fct-related-product-inspector-controls",children:[t.jsxs(le,{label:o("Query type"),value:e.query_type,onChange:r=>l({query_type:r}),help:e.query_type==="custom"?o("Select a specific product to display related items."):o("Automatically use the current page's product context."),isBlock:!0,children:[t.jsx(q,{value:"default",label:o("Default")}),t.jsx(q,{value:"custom",label:o("Custom")})]}),e.query_type==="custom"&&t.jsx(M,{selectedProduct:d,onSelect:c})]}),t.jsxs(k,{title:o("Related by"),initialOpen:!0,children:[t.jsx(D,{label:o("Categories"),checked:e.related_by_categories,onChange:r=>l({related_by_categories:r})}),t.jsx(D,{label:o("Brands"),checked:e.related_by_brands,onChange:r=>l({related_by_brands:r})})]}),t.jsxs(re,{label:o("Settings"),resetAll:()=>l({query_type:"custom",order_by:"title_asc",columns:4,posts_per_page:6}),children:[t.jsx(v,{hasValue:()=>e.order_by!=="title_asc",label:o("Order by"),onDeselect:()=>l({order_by:"title_asc"}),isShownByDefault:!0,children:t.jsx(oe,{label:o("Order by"),value:e.order_by,options:[{label:o("A → Z"),value:"title_asc"},{label:o("Z → A"),value:"title_desc"},{label:o("Newest to oldest"),value:"date_desc"},{label:o("Oldest to newest"),value:"date_asc"},{label:o("Price, high to low"),value:"price_desc"},{label:o("Price, low to high"),value:"price_asc"},{label:o("Random"),value:"rand"}],onChange:r=>l({order_by:r})})}),t.jsx(v,{hasValue:()=>e.columns!==4,label:o("Columns"),onDeselect:()=>l({columns:4}),isShownByDefault:!0,children:t.jsx(R,{label:o("Columns"),value:e.columns,onChange:r=>l({columns:r}),min:1,max:6})}),t.jsx(v,{hasValue:()=>e.posts_per_page!==6,label:o("Products per page"),onDeselect:()=>l({posts_per_page:6}),isShownByDefault:!0,children:t.jsx(R,{label:o("Products per page"),value:e.posts_per_page,onChange:r=>l({posts_per_page:r}),min:1,max:12})})]}),t.jsxs(k,{title:o("Display Elements"),initialOpen:!1,children:[t.jsx(y,{label:o("Image"),checked:e.show_image,onChange:r=>l({show_image:r})}),t.jsx(y,{label:o("Title"),checked:e.show_title,onChange:r=>l({show_title:r})}),t.jsx(y,{label:o("Price"),checked:e.show_price,onChange:r=>l({show_price:r})}),t.jsx(y,{label:o("Button"),checked:e.show_button,onChange:r=>l({show_button:r})})]})]})})},{useBlockProps:L,InnerBlocks:ce,BlockContextProvider:se,useInnerBlocksProps:ae,__experimentalUseBlockPreview:ne}=wp.blockEditor,{registerBlockType:de}=wp.blocks,{useState:pe,memo:ie}=wp.element,{useSelect:ue}=wp.data,_e=ne||(()=>({})),O=window.fluent_cart_related_product_data,he=O.slug+"/"+O.name,fe=[["fluent-cart/shopapp-product-image"],["fluent-cart/shopapp-product-title"],["fluent-cart/shopapp-product-price"],["fluent-cart/shopapp-product-buttons"]],me=()=>{const e=ae({className:"fct-related-product-card"},{template:fe,templateLock:!1,allowedBlocks:["fluent-cart/shopapp-product-image","fluent-cart/shopapp-product-title","fluent-cart/shopapp-product-price","fluent-cart/shopapp-product-buttons"]});return t.jsx("div",{...e})},ge=ie(({blocks:e,blockContextId:l,isHidden:d,setActiveBlockContextId:p})=>{const c=_e({blocks:e,props:{className:"fct-related-product-card"}}),r=()=>{p(l)},i={display:d?"none":void 0};return t.jsx("div",{...c,tabIndex:0,role:"button",onClick:r,onKeyPress:r,style:i})}),ye=({blocks:e,blockContext:l,displayTemplate:d,setActiveBlockContextId:p})=>{const c={...l,detail:l.detail||{}};return t.jsx(se,{value:c,children:t.jsxs(H,{value:{product:c},children:[d?t.jsx(me,{}):null,t.jsx(ge,{blocks:e,blockContextId:c.ID,setActiveBlockContextId:p,isHidden:d})]})},c.ID)};de("fluent-cart/product-template",{apiVersion:3,title:o("Product Template"),description:o("Contains the block elements used to render a product."),icon:{src:z},category:"fluent-cart",parent:[he],usesContext:["fluent-cart/related_product_ids","fluent-cart/related_products","fluent-cart/related_by_categories","fluent-cart/related_by_brands","fluent-cart/product_id","fluent-cart/query_type","fluent-cart/columns","fluent-cart/posts_per_page","fluent-cart/show_image","fluent-cart/show_title","fluent-cart/show_price","fluent-cart/show_button"],supports:{reusable:!1,html:!1},edit:({context:e,clientId:l})=>{const d=e["fluent-cart/related_products"]||[],p=e["fluent-cart/query_type"]||"custom",c=e["fluent-cart/product_id"]||"",r=e["fluent-cart/columns"]||4,[i,u]=pe(),h={gridTemplateColumns:`repeat(${r}, 1fr)`},_=[e["fluent-cart/show_image"]===!1&&"fct-hide-image",e["fluent-cart/show_title"]===!1&&"fct-hide-title",e["fluent-cart/show_price"]===!1&&"fct-hide-price",e["fluent-cart/show_button"]===!1&&"fct-hide-button"].filter(Boolean).join(" "),m=L({className:`fct-product-template-block ${_}`.trim()}),n=ue(a=>a("core/block-editor").getBlock(l),[l]),s=(n==null?void 0:n.innerBlocks)||[];if(d.length===0){if(p==="custom"&&c)return t.jsx("div",{...m,children:t.jsx("div",{className:"fct-no-related-products",children:t.jsx("p",{children:o("No products to display. Try adjusting the filters in the block settings panel or add categories or brands to this product.")})})});const a=e["fluent-cart/show_image"]!==!1,f=e["fluent-cart/show_title"]!==!1,g=e["fluent-cart/show_price"]!==!1,Q=e["fluent-cart/show_button"]!==!1,U=Array.from({length:r},(S,Z)=>Z+1);return t.jsx("div",{...m,children:t.jsx("div",{className:"fct-related-products-grid",style:h,children:U.map(S=>t.jsxs("div",{className:"fct-product-card-placeholder",children:[a&&t.jsx("div",{className:"fct-placeholder-image"}),f&&t.jsx("div",{className:"fct-placeholder-line fct-placeholder-title"}),g&&t.jsx("div",{className:"fct-placeholder-line fct-placeholder-price"}),Q&&t.jsx("div",{className:"fct-placeholder-line fct-placeholder-button"})]},S))})})}return t.jsx("div",{...m,children:t.jsx("div",{className:"fct-related-products-grid",style:h,children:d.map(a=>{var g;const f=a.ID===(i||((g=d[0])==null?void 0:g.ID));return t.jsx("div",{className:"fct-product-card",children:t.jsx(ye,{blocks:s,blockContext:a,displayTemplate:f,setActiveBlockContextId:u},a.ID)},a.ID)})})})},save:()=>{const e=L.save();return t.jsx("div",{...e,className:"fluent-cart-related-product-template",children:t.jsx(ce.Content,{})})}});const{useBlockProps:$,InnerBlocks:V}=wp.blockEditor,{registerBlockType:we}=wp.blocks,{Placeholder:G}=wp.components,{useEffect:W,useState:C}=wp.element,w=window.fluent_cart_related_product_data,x=window.fluentCartRestVars.rest;we(w.slug+"/"+w.name,{apiVersion:3,title:w.title,description:w.description,icon:{src:j},category:"fluent-cart",attributes:{product_id:{type:["string","number"],default:""},related_product_ids:{type:"array",default:[]},related_products:{type:"array",default:[]},related_by_categories:{type:"boolean",default:!0},related_by_brands:{type:"boolean",default:!0},query_type:{type:"string",default:"custom"},order_by:{type:"string",default:"title_asc"},columns:{type:"number",default:4},posts_per_page:{type:"number",default:6},show_image:{type:"boolean",default:!0},show_title:{type:"boolean",default:!0},show_price:{type:"boolean",default:!0},show_button:{type:"boolean",default:!0}},providesContext:{"fluent-cart/related_product_ids":"related_product_ids","fluent-cart/related_products":"related_products","fluent-cart/product_id":"product_id","fluent-cart/related_by_categories":"related_by_categories","fluent-cart/related_by_brands":"related_by_brands","fluent-cart/query_type":"query_type","fluent-cart/order_by":"order_by","fluent-cart/columns":"columns","fluent-cart/posts_per_page":"posts_per_page","fluent-cart/show_image":"show_image","fluent-cart/show_title":"show_title","fluent-cart/show_price":"show_price","fluent-cart/show_button":"show_button"},supports:{innerBlocks:!0},edit:({attributes:e,setAttributes:l})=>{const d=$({className:"fluent-cart-related-product-block"}),[p,c]=C({}),[r,i]=C(e.related_products||[]),[u,h]=C(!1),_=[["core/heading",{level:2,content:"Related Products"}],["fluent-cart/product-template"]],m=()=>{N({path:P(x.url+"/products/"+e.product_id,{with:["block_product_detail"]}),headers:{"X-WP-Nonce":x.nonce}}).then(s=>{c(s.product||{})}).catch(s=>{console.error("Error fetching product:",s)})},n=()=>{h(!0),N({path:P(x.url+"/products/"+e.product_id+"/related-products",{related_by_categories:e.related_by_categories?1:0,related_by_brands:e.related_by_brands?1:0,order_by:e.order_by,posts_per_page:e.posts_per_page}),headers:{"X-WP-Nonce":x.nonce}}).then(s=>{const a=s.products||[];i(a);const f=a.map(g=>g.ID);l({related_product_ids:f,related_products:a})}).catch(s=>{console.error("Error fetching related products:",s)}).finally(()=>{h(!1)})};return W(()=>{e.query_type==="custom"&&e.product_id&&(m(),n())},[e.product_id,e.related_by_categories,e.related_by_brands,e.order_by,e.posts_per_page,e.query_type]),W(()=>{e.query_type==="default"&&(i([]),c({}),l({product_id:"",related_product_ids:[],related_products:[]}))},[e.query_type]),e.query_type==="custom"&&!e.product_id?t.jsxs("div",{...d,children:[t.jsx(E,{attributes:e,setAttributes:l,selectedProduct:p,setSelectedProduct:c}),t.jsx(G,{icon:j,label:o("Related Products"),instructions:o("Related products requires a product to be selected in order to display associated items."),className:"fct-related-products-placeholder",children:t.jsx(M,{selectedProduct:p,onSelect:s=>{l({product_id:s.ID||""}),c(s)}})})]}):t.jsxs("div",{...d,children:[t.jsx(E,{attributes:e,setAttributes:l,selectedProduct:p,setSelectedProduct:c}),u&&t.jsx(G,{icon:j,label:o("Loading Related Products...")}),!u&&t.jsx(V,{template:_,templateLock:!1,allowedBlocks:["core/heading","fluent-cart/product-template"]})]})},save:()=>{const e=$.save({className:"fluent-cart-related-products"});return t.jsx("div",{...e,children:t.jsx(V.Content,{})})}});
1 +import{j as t}from"./chunks/jsx-runtime.js?ver=1.6.5";import{L as z,R as j}from"./chunks/Icons.js?ver=1.6.5";import{a as N}from"./chunks/index5.js?ver=1.6.5";import{b as o}from"./chunks/BlockEditorTranslator.js?ver=1.6.5";import{a as P}from"./chunks/add-query-args.js?ver=1.6.5";import{S as H}from"./chunks/SingleProductContext.js?ver=1.6.5";const{Button:K,TextControl:J,Spinner:Y,Icon:A}=wp.components,{useState:b,useEffect:I,useRef:B}=wp.element,T=window.fluentCartRestVars.rest;var F;const X=(F=window.fluent_cart_block_editor_asset)==null?void 0:F.placeholder_image,ee=({onSelect:e})=>{const[l,d]=b(""),[p,c]=b([]),[r,i]=b(!1),u=B(0),h=(n="")=>{const s=++u.current;i(!0),N({path:P(T.url+"/products",{with:["block_picker"],active_view:"publish",per_page:10,page:1,order_by:"ID",order_type:"DESC",search:n||void 0}),headers:{"X-WP-Nonce":T.nonce}}).then(a=>{var f;s===u.current&&c(((f=a.products)==null?void 0:f.data)||[])}).catch(()=>{s===u.current&&c([])}).finally(()=>{s===u.current&&i(!1)})};I(()=>{h()},[]);const _=B(null),m=n=>{d(n),_.current&&clearTimeout(_.current),_.current=setTimeout(()=>{h(n)},300)};return I(()=>()=>{_.current&&clearTimeout(_.current)},[]),t.jsxs("div",{className:"fct-product-picker-dropdown",children:[t.jsx(J,{placeholder:o("Search products..."),value:l,onChange:m,className:"fct-product-picker-search"}),t.jsx("div",{className:"fct-product-picker-list",children:r?t.jsx("div",{className:"fct-product-picker-loading",children:t.jsx(Y,{})}):p.length>0?p.map(n=>{var s,a;return t.jsxs("button",{className:"fct-product-picker-item",onClick:()=>e(n),type:"button",children:[t.jsx("img",{className:"fct-product-picker-item-img",src:((a=(s=n==null?void 0:n.detail)==null?void 0:s.featured_media)==null?void 0:a.url)||X,alt:n.post_title}),t.jsx("span",{className:"fct-product-picker-item-title",children:n.post_title})]},n.ID)}):t.jsx("p",{className:"fct-product-picker-empty",children:o("No products found")})})]})},M=({selectedProduct:e=null,onSelect:l,buttonLabel:d=null,changeLabel:p=null})=>{var n,s;const[c,r]=b(!1),i=B(null),u=d||o("Select Product"),h=p||o("Change Product");I(()=>{const a=f=>{i.current&&!i.current.contains(f.target)&&r(!1)};return document.addEventListener("mousedown",a),()=>document.removeEventListener("mousedown",a)},[]);const _=a=>{l(a),r(!1)},m=((s=(n=e==null?void 0:e.detail)==null?void 0:n.featured_media)==null?void 0:s.url)||(e==null?void 0:e.thumbnail)||X;return t.jsxs("div",{children:[(e==null?void 0:e.post_title)&&t.jsxs("div",{className:"fct-product-picker-selected",children:[t.jsx("img",{className:"fct-product-picker-selected-img",src:m,alt:e.post_title,width:40,height:40,style:{width:"40px",height:"40px",maxWidth:"40px",objectFit:"cover"}}),t.jsx("span",{className:"fct-product-picker-selected-title",children:e.post_title})]}),t.jsxs("div",{className:"fct-product-picker-button-wrap",ref:i,children:[t.jsxs(K,{variant:"secondary",onClick:()=>r(!c),className:"fct-product-picker-btn",children:[t.jsx(A,{icon:e!=null&&e.post_title?"edit":"search",size:16}),e!=null&&e.post_title?h:u]}),c&&t.jsx(ee,{onSelect:_})]})]})},{InspectorControls:te}=wp.blockEditor,{PanelBody:k,CheckboxControl:D,SelectControl:oe,RangeControl:R,ToggleControl:y,__experimentalToolsPanel:re,__experimentalToolsPanelItem:v,__experimentalToggleGroupControl:le,__experimentalToggleGroupControlOption:q}=wp.components,E=({attributes:e,setAttributes:l,selectedProduct:d,setSelectedProduct:p})=>{const c=r=>{l({product_id:r.ID||""}),p(r)};return t.jsx(t.Fragment,{children:t.jsxs(te,{children:[t.jsxs(k,{title:o("Product Settings"),initialOpen:!0,className:"fct-related-product-inspector-controls",children:[t.jsxs(le,{label:o("Query type"),value:e.query_type,onChange:r=>l({query_type:r}),help:e.query_type==="custom"?o("Select a specific product to display related items."):o("Automatically use the current page's product context."),isBlock:!0,children:[t.jsx(q,{value:"default",label:o("Default")}),t.jsx(q,{value:"custom",label:o("Custom")})]}),e.query_type==="custom"&&t.jsx(M,{selectedProduct:d,onSelect:c})]}),t.jsxs(k,{title:o("Related by"),initialOpen:!0,children:[t.jsx(D,{label:o("Categories"),checked:e.related_by_categories,onChange:r=>l({related_by_categories:r})}),t.jsx(D,{label:o("Brands"),checked:e.related_by_brands,onChange:r=>l({related_by_brands:r})})]}),t.jsxs(re,{label:o("Settings"),resetAll:()=>l({query_type:"custom",order_by:"title_asc",columns:4,posts_per_page:6}),children:[t.jsx(v,{hasValue:()=>e.order_by!=="title_asc",label:o("Order by"),onDeselect:()=>l({order_by:"title_asc"}),isShownByDefault:!0,children:t.jsx(oe,{label:o("Order by"),value:e.order_by,options:[{label:o("A → Z"),value:"title_asc"},{label:o("Z → A"),value:"title_desc"},{label:o("Newest to oldest"),value:"date_desc"},{label:o("Oldest to newest"),value:"date_asc"},{label:o("Price, high to low"),value:"price_desc"},{label:o("Price, low to high"),value:"price_asc"},{label:o("Random"),value:"rand"}],onChange:r=>l({order_by:r})})}),t.jsx(v,{hasValue:()=>e.columns!==4,label:o("Columns"),onDeselect:()=>l({columns:4}),isShownByDefault:!0,children:t.jsx(R,{label:o("Columns"),value:e.columns,onChange:r=>l({columns:r}),min:1,max:6})}),t.jsx(v,{hasValue:()=>e.posts_per_page!==6,label:o("Products per page"),onDeselect:()=>l({posts_per_page:6}),isShownByDefault:!0,children:t.jsx(R,{label:o("Products per page"),value:e.posts_per_page,onChange:r=>l({posts_per_page:r}),min:1,max:12})})]}),t.jsxs(k,{title:o("Display Elements"),initialOpen:!1,children:[t.jsx(y,{label:o("Image"),checked:e.show_image,onChange:r=>l({show_image:r})}),t.jsx(y,{label:o("Title"),checked:e.show_title,onChange:r=>l({show_title:r})}),t.jsx(y,{label:o("Price"),checked:e.show_price,onChange:r=>l({show_price:r})}),t.jsx(y,{label:o("Button"),checked:e.show_button,onChange:r=>l({show_button:r})})]})]})})},{useBlockProps:L,InnerBlocks:ce,BlockContextProvider:se,useInnerBlocksProps:ae,__experimentalUseBlockPreview:ne}=wp.blockEditor,{registerBlockType:de}=wp.blocks,{useState:pe,memo:ie}=wp.element,{useSelect:ue}=wp.data,_e=ne||(()=>({})),O=window.fluent_cart_related_product_data,he=O.slug+"/"+O.name,fe=[["fluent-cart/shopapp-product-image"],["fluent-cart/shopapp-product-title"],["fluent-cart/shopapp-product-price"],["fluent-cart/shopapp-product-buttons"]],me=()=>{const e=ae({className:"fct-related-product-card"},{template:fe,templateLock:!1,allowedBlocks:["fluent-cart/shopapp-product-image","fluent-cart/shopapp-product-title","fluent-cart/shopapp-product-price","fluent-cart/shopapp-product-buttons"]});return t.jsx("div",{...e})},ge=ie(({blocks:e,blockContextId:l,isHidden:d,setActiveBlockContextId:p})=>{const c=_e({blocks:e,props:{className:"fct-related-product-card"}}),r=()=>{p(l)},i={display:d?"none":void 0};return t.jsx("div",{...c,tabIndex:0,role:"button",onClick:r,onKeyPress:r,style:i})}),ye=({blocks:e,blockContext:l,displayTemplate:d,setActiveBlockContextId:p})=>{const c={...l,detail:l.detail||{}};return t.jsx(se,{value:c,children:t.jsxs(H,{value:{product:c},children:[d?t.jsx(me,{}):null,t.jsx(ge,{blocks:e,blockContextId:c.ID,setActiveBlockContextId:p,isHidden:d})]})},c.ID)};de("fluent-cart/product-template",{apiVersion:3,title:o("Product Template"),description:o("Contains the block elements used to render a product."),icon:{src:z},category:"fluent-cart",parent:[he],usesContext:["fluent-cart/related_product_ids","fluent-cart/related_products","fluent-cart/related_by_categories","fluent-cart/related_by_brands","fluent-cart/product_id","fluent-cart/query_type","fluent-cart/columns","fluent-cart/posts_per_page","fluent-cart/show_image","fluent-cart/show_title","fluent-cart/show_price","fluent-cart/show_button"],supports:{reusable:!1,html:!1},edit:({context:e,clientId:l})=>{const d=e["fluent-cart/related_products"]||[],p=e["fluent-cart/query_type"]||"custom",c=e["fluent-cart/product_id"]||"",r=e["fluent-cart/columns"]||4,[i,u]=pe(),h={gridTemplateColumns:`repeat(${r}, 1fr)`},_=[e["fluent-cart/show_image"]===!1&&"fct-hide-image",e["fluent-cart/show_title"]===!1&&"fct-hide-title",e["fluent-cart/show_price"]===!1&&"fct-hide-price",e["fluent-cart/show_button"]===!1&&"fct-hide-button"].filter(Boolean).join(" "),m=L({className:`fct-product-template-block ${_}`.trim()}),n=ue(a=>a("core/block-editor").getBlock(l),[l]),s=(n==null?void 0:n.innerBlocks)||[];if(d.length===0){if(p==="custom"&&c)return t.jsx("div",{...m,children:t.jsx("div",{className:"fct-no-related-products",children:t.jsx("p",{children:o("No products to display. Try adjusting the filters in the block settings panel or add categories or brands to this product.")})})});const a=e["fluent-cart/show_image"]!==!1,f=e["fluent-cart/show_title"]!==!1,g=e["fluent-cart/show_price"]!==!1,Q=e["fluent-cart/show_button"]!==!1,U=Array.from({length:r},(S,Z)=>Z+1);return t.jsx("div",{...m,children:t.jsx("div",{className:"fct-related-products-grid",style:h,children:U.map(S=>t.jsxs("div",{className:"fct-product-card-placeholder",children:[a&&t.jsx("div",{className:"fct-placeholder-image"}),f&&t.jsx("div",{className:"fct-placeholder-line fct-placeholder-title"}),g&&t.jsx("div",{className:"fct-placeholder-line fct-placeholder-price"}),Q&&t.jsx("div",{className:"fct-placeholder-line fct-placeholder-button"})]},S))})})}return t.jsx("div",{...m,children:t.jsx("div",{className:"fct-related-products-grid",style:h,children:d.map(a=>{var g;const f=a.ID===(i||((g=d[0])==null?void 0:g.ID));return t.jsx("div",{className:"fct-product-card",children:t.jsx(ye,{blocks:s,blockContext:a,displayTemplate:f,setActiveBlockContextId:u},a.ID)},a.ID)})})})},save:()=>{const e=L.save();return t.jsx("div",{...e,className:"fluent-cart-related-product-template",children:t.jsx(ce.Content,{})})}});const{useBlockProps:$,InnerBlocks:V}=wp.blockEditor,{registerBlockType:we}=wp.blocks,{Placeholder:G}=wp.components,{useEffect:W,useState:C}=wp.element,w=window.fluent_cart_related_product_data,x=window.fluentCartRestVars.rest;we(w.slug+"/"+w.name,{apiVersion:3,title:w.title,description:w.description,icon:{src:j},category:"fluent-cart",attributes:{product_id:{type:["string","number"],default:""},related_product_ids:{type:"array",default:[]},related_products:{type:"array",default:[]},related_by_categories:{type:"boolean",default:!0},related_by_brands:{type:"boolean",default:!0},query_type:{type:"string",default:"custom"},order_by:{type:"string",default:"title_asc"},columns:{type:"number",default:4},posts_per_page:{type:"number",default:6},show_image:{type:"boolean",default:!0},show_title:{type:"boolean",default:!0},show_price:{type:"boolean",default:!0},show_button:{type:"boolean",default:!0}},providesContext:{"fluent-cart/related_product_ids":"related_product_ids","fluent-cart/related_products":"related_products","fluent-cart/product_id":"product_id","fluent-cart/related_by_categories":"related_by_categories","fluent-cart/related_by_brands":"related_by_brands","fluent-cart/query_type":"query_type","fluent-cart/order_by":"order_by","fluent-cart/columns":"columns","fluent-cart/posts_per_page":"posts_per_page","fluent-cart/show_image":"show_image","fluent-cart/show_title":"show_title","fluent-cart/show_price":"show_price","fluent-cart/show_button":"show_button"},supports:{innerBlocks:!0},edit:({attributes:e,setAttributes:l})=>{const d=$({className:"fluent-cart-related-product-block"}),[p,c]=C({}),[r,i]=C(e.related_products||[]),[u,h]=C(!1),_=[["core/heading",{level:2,content:"Related Products"}],["fluent-cart/product-template"]],m=()=>{N({path:P(x.url+"/products/"+e.product_id,{with:["block_product_detail"]}),headers:{"X-WP-Nonce":x.nonce}}).then(s=>{c(s.product||{})}).catch(s=>{console.error("Error fetching product:",s)})},n=()=>{h(!0),N({path:P(x.url+"/products/"+e.product_id+"/related-products",{related_by_categories:e.related_by_categories?1:0,related_by_brands:e.related_by_brands?1:0,order_by:e.order_by,posts_per_page:e.posts_per_page}),headers:{"X-WP-Nonce":x.nonce}}).then(s=>{const a=s.products||[];i(a);const f=a.map(g=>g.ID);l({related_product_ids:f,related_products:a})}).catch(s=>{console.error("Error fetching related products:",s)}).finally(()=>{h(!1)})};return W(()=>{e.query_type==="custom"&&e.product_id&&(m(),n())},[e.product_id,e.related_by_categories,e.related_by_brands,e.order_by,e.posts_per_page,e.query_type]),W(()=>{e.query_type==="default"&&(i([]),c({}),l({product_id:"",related_product_ids:[],related_products:[]}))},[e.query_type]),e.query_type==="custom"&&!e.product_id?t.jsxs("div",{...d,children:[t.jsx(E,{attributes:e,setAttributes:l,selectedProduct:p,setSelectedProduct:c}),t.jsx(G,{icon:j,label:o("Related Products"),instructions:o("Related products requires a product to be selected in order to display associated items."),className:"fct-related-products-placeholder",children:t.jsx(M,{selectedProduct:p,onSelect:s=>{l({product_id:s.ID||""}),c(s)}})})]}):t.jsxs("div",{...d,children:[t.jsx(E,{attributes:e,setAttributes:l,selectedProduct:p,setSelectedProduct:c}),u&&t.jsx(G,{icon:j,label:o("Loading Related Products...")}),!u&&t.jsx(V,{template:_,templateLock:!1,allowedBlocks:["core/heading","fluent-cart/product-template"]})]})},save:()=>{const e=$.save({className:"fluent-cart-related-products"});return t.jsx("div",{...e,children:t.jsx(V.Content,{})})}});