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