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