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