import{j as s,b as o}from"./BlockEditorTranslator.js?ver=1.5.3";import{f as D}from"./Icons.js?ver=1.5.3";import{a as w}from"./index5.js?ver=1.5.3";import{I as C,L as A,E}from"./ProductListItem.js?ver=1.5.3";import"./index6.js?ver=1.5.3";import{a as _}from"./add-query-args.js?ver=1.5.3";const{useEffect:S,useState:h}=wp.element,v=window.fluentCartRestVars.rest,F=({prevSelectedProduct:i,onProductSelectionUpdated:a,isMultiple:c=!1})=>{const d=v.url+"/products",[x,u]=h(!1),[p,f]=h({}),[n,j]=h(c?null:i||null),[y,k]=h(c?i||[]:[]),I=(e,t)=>{if(c){k(l=>{const m=Array.isArray(l)?l:[],N=t?[...m,e]:m.filter(P=>P.ID!==e.ID);return a==null||a(N),N});return}const r=t?e:null;j(r),a==null||a(r)},b=(e=null)=>{u(!0),f({}),w({path:_(d,{...{with:["detail","variants"],active_view:"publish",per_page:10,page:1,order_by:"ID",order_type:"DESC",search:e}}),headers:{"X-WP-Nonce":v.nonce}}).then(r=>{let l=r.products.data;f(l)}).finally(()=>{u(!1)})};S(()=>{b()},[]);const g=e=>{b(e)};return s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"fct-popup-search-wrap",children:s.jsx(C,{icon:!0,name:"search",placeholder:o("Type to search for products, then hit Enter"),onKeyDown:e=>{const t=e.target.value;e.key==="Enter"&&g(t)}})}),s.jsx("div",{className:"fct-popup-body-inner",children:x?s.jsx("div",{className:"fct-skeleton-container",children:[...Array(9)].map((e,t)=>s.jsx("div",{className:"skeleton skeleton-item"},t))}):Object.keys(p).length>0?s.jsx("div",{className:"fct-popup-product-list-container",children:s.jsx("ul",{className:"fct-collapsible-list",children:p.map((e,t)=>{var r,l;return s.jsx("li",{className:"fct-collapsible-list-item",children:s.jsx("div",{className:"fct-collapsible-list-item-inner",children:s.jsx(A,{variant:e,title:e==null?void 0:e.post_title,media:(l=(r=e==null?void 0:e.detail)==null?void 0:r.featured_media)==null?void 0:l.url,isMultiple:c,checked:c?Array.isArray(y)&&y.some(m=>m.ID===e.ID):(n==null?void 0:n.ID)===e.ID,updateSelectedVariations:I})})},t)})})}):s.jsx(E,{text:o("We couldn't find any products matching your search")})})]})},{useState:L}=wp.element,K=({onModalClosed:i,buttonLabel:a=o("Add Product"),selectedProduct:c=null,setSelectedProduct:d=()=>{},isMultiple:x=!1})=>{const[u,p]=L(!1),f=()=>{p(!0)},n=()=>{p(!1),typeof i=="function"&&i(c)};return s.jsxs(s.Fragment,{children:[s.jsx("button",{className:"fct-button fct-button-secondary",onClick:f,children:o("Select Product")}),u&&s.jsxs("div",{className:"fct-popup-container",children:[s.jsx("div",{className:"fct-popup-overlay",onClick:n}),s.jsxs("div",{className:"fct-popup-inner-container",children:[s.jsxs("div",{className:"fct-popup-head",children:[s.jsx("h4",{className:"fct-popup-head-title",children:o("Select Product")}),s.jsx("button",{type:"button",className:"fct-popup-close",onClick:n,children:s.jsx(D,{})})]}),s.jsx("div",{className:"fct-popup-body",children:s.jsx(F,{prevSelectedProduct:c,onProductSelectionUpdated:j=>{d(j)},isMultiple:x})}),s.jsxs("div",{className:"fct-popup-footer",children:[s.jsx("button",{className:"fct-button fct-button-info-soft",onClick:n,children:o("Cancel")}),s.jsx("button",{className:"fct-button fct-button-primary",onClick:()=>{n()},children:a})]})]})]})]})};export{K as S};
