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