(window.webpackWcBlocksJsonp=window.webpackWcBlocksJsonp||[]).push([[85],{442:function(e){e.exports=JSON.parse('{"name":"woocommerce/stock-filter","version":"1.0.0","title":"Filter by Stock Controls","description":"Enable customers to filter the product grid by stock status.","category":"woocommerce","keywords":["WooCommerce"],"supports":{"html":false,"multiple":false,"color":true,"inserter":false,"lock":false},"attributes":{"className":{"type":"string","default":""},"headingLevel":{"type":"number","default":3},"showCounts":{"type":"boolean","default":true},"showFilterButton":{"type":"boolean","default":false},"displayStyle":{"type":"string","default":"list"},"selectType":{"type":"string","default":"multiple"},"isPreview":{"type":"boolean","default":false}},"textdomain":"woocommerce","apiVersion":2,"$schema":"https://schemas.wp.org/trunk/block.json"}')},443:function(e,t){},494:function(e,t,c){"use strict";c.r(t);var s=c(0),l=c(6),o=c.n(l),n=c(264),a=c(35),r=c(1),i=c(27),u=c(84),d=c(247),b=c(44),f=c(67),m=c(62),O=c(246),g=c(2),j=c(122),p=c(117),k=c(116),v=c(115),w=c(332),h=c(121),y=c(18),_=c.n(y),E=c(20),S=c(96),C=c(25),L=c(19),N=c(97);const A=[{value:"preview-1",name:"In Stock",label:Object(s.createElement)(w.a,{name:"In Stock",count:3}),textLabel:"In Stock (3)"},{value:"preview-2",name:"Out of stock",label:Object(s.createElement)(w.a,{name:"Out of stock",count:3}),textLabel:"Out of stock (3)"},{value:"preview-3",name:"On backorder",label:Object(s.createElement)(w.a,{name:"On backorder",count:2}),textLabel:"On backorder (2)"}];c(443);var F=c(442);function x(){return Math.floor(Math.random()*Date.now())}const B=e=>e.trim().replace(/\s/g,"").replace(/_/g,"-").replace(/-+/g,"-").replace(/[^a-zA-Z0-9-]/g,"");var I=c(61);const T=N.a+"stock_status";var P=e=>{let{attributes:t,isEditor:c=!1}=e;const l=Object(I.b)(),n=Object(g.getSettingWithCoercion)("is_rendering_php_template",!1,S.a),[y,F]=Object(s.useState)(!1),{outofstock:P,...R}=Object(g.getSetting)("stockStatusOptions",{}),W=c?[]:Object(g.getSettingWithCoercion)("product_ids",[],Array.isArray),M=Object(s.useRef)(Object(g.getSetting)("hideOutOfStockItems",!1)?R:{outofstock:P,...R}),J=Object(s.useMemo)(()=>function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"filter_stock_status";const c=Object(N.d)(t);if(!c)return[];const s=Object(a.a)(c)?c.split(","):c,l=Object.keys(e);return s.filter(e=>l.includes(e))}(M.current,T),[]),[$,q]=Object(s.useState)(J),[z,D]=Object(s.useState)(t.isPreview?A:[]),[Q]=Object(s.useState)(Object.entries(M.current).map(e=>{let[t,c]=e;return{slug:t,name:c}}).filter(e=>!!e.name).sort((e,t)=>e.slug.localeCompare(t.slug))),[V]=Object(m.a)(),[Z,G]=Object(m.b)("stock_status",J),{results:H,isLoading:K}=Object(O.a)({queryStock:!0,queryState:V,productIds:W,isEditor:c}),U=Object(s.useCallback)(e=>Object(C.b)(H,"stock_status_counts")&&Array.isArray(H.stock_status_counts)?H.stock_status_counts.find(t=>{let{status:c,count:s}=t;return c===e&&0!==Number(s)}):null,[H]),[X,Y]=Object(s.useState)(x());Object(s.useEffect)(()=>{if(K||t.isPreview)return;const e=Q.map(e=>{const c=U(e.slug);if(!(c||$.includes(e.slug)||(l=e.slug,null!=V&&V.stock_status&&V.stock_status.some(e=>{let{status:t=[]}=e;return t.includes(l)}))))return null;var l;const o=c?Number(c.count):0;return{value:e.slug,name:Object(E.decodeEntities)(e.name),label:Object(s.createElement)(w.a,{name:Object(E.decodeEntities)(e.name),count:t.showCounts?o:null}),textLabel:t.showCounts?`${Object(E.decodeEntities)(e.name)} (${o})`:Object(E.decodeEntities)(e.name)}}).filter(e=>!!e);D(e),Y(x())},[t.showCounts,t.isPreview,K,U,$,V.stock_status,Q]);const ee="single"!==t.selectType,te=Object(s.useCallback)(e=>{c||(e&&!n&&G(e),(e=>{if(!window)return;if(0===e.length){const e=Object(L.removeQueryArgs)(window.location.href,T);return void(e!==Object(N.e)(window.location.href)&&Object(N.c)(e))}const t=Object(L.addQueryArgs)(window.location.href,{[T]:e.join(",")});t!==Object(N.e)(window.location.href)&&Object(N.c)(t)})(e))},[c,G,n]);Object(s.useEffect)(()=>{t.showFilterButton||te($)},[t.showFilterButton,$,te]);const ce=Object(s.useMemo)(()=>Z,[Z]),se=Object(b.a)(ce),le=Object(f.a)(se);Object(s.useEffect)(()=>{_()(le,se)||_()($,se)||q(se)},[$,se,le]),Object(s.useEffect)(()=>{y||(G(J),F(!0))},[G,y,F,J]);const oe=Object(s.useCallback)(e=>{const t=e=>{const t=z.find(t=>t.value===e);return t?t.name:null},c=e=>{let{filterAdded:c,filterRemoved:s}=e;const l=c?t(c):null,o=s?t(s):null;l?Object(i.speak)(Object(r.sprintf)(
/* translators: %s stock statuses (for example: 'instock'...) */
Object(r.__)("%s filter added.","woocommerce"),l)):o&&Object(i.speak)(Object(r.sprintf)(
/* translators: %s stock statuses (for example:'instock'...) */
Object(r.__)("%s filter removed.","woocommerce"),o))},s=$.includes(e);if(!ee){const t=s?[]:[e];return c(s?{filterRemoved:e}:{filterAdded:e}),void q(t)}if(s){const t=$.filter(t=>t!==e);return c({filterRemoved:e}),void q(t)}const l=[...$,e].sort();c({filterAdded:e}),q(l)},[$,ee,z]);if(!K&&0===z.length)return l(!1),null;const ne="h"+t.headingLevel,ae=!t.isPreview&&!M.current||0===z.length,re=!t.isPreview&&K;if(!Object(g.getSettingWithCoercion)("has_filterable_products",!1,S.a))return l(!1),null;const ie=ee?!ae&&$.length<z.length:!ae&&0===$.length,ue=Object(s.createElement)(ne,{className:"wc-block-stock-filter__title"},t.heading),de=ae?Object(s.createElement)(v.a,null,ue):ue;return l(!0),Object(s.createElement)(s.Fragment,null,!c&&t.heading&&de,Object(s.createElement)("div",{className:o()("wc-block-stock-filter","style-"+t.displayStyle,{"is-loading":ae})},"dropdown"===t.displayStyle?Object(s.createElement)(s.Fragment,null,Object(s.createElement)(h.a,{key:X,className:o()({"single-selection":!ee,"is-loading":ae}),suggestions:z.filter(e=>!$.includes(e.value)).map(e=>e.value),disabled:ae,placeholder:Object(r.__)("Select stock status","woocommerce"),onChange:e=>{!ee&&e.length>1&&(e=e.slice(-1));const t=[e=e.map(e=>{const t=z.find(t=>t.value===e);return t?t.value:e}),$].reduce((e,t)=>e.filter(e=>!t.includes(e)));if(1===t.length)return oe(t[0]);const c=[$,e].reduce((e,t)=>e.filter(e=>!t.includes(e)));1===c.length&&oe(c[0])},value:$,displayTransform:e=>{const t=z.find(t=>t.value===e);return t?t.textLabel:e},saveTransform:B,messages:{added:Object(r.__)("Stock filter added.","woocommerce"),removed:Object(r.__)("Stock filter removed.","woocommerce"),remove:Object(r.__)("Remove stock filter.","woocommerce"),__experimentalInvalid:Object(r.__)("Invalid stock filter.","woocommerce")}}),ie&&Object(s.createElement)(u.a,{icon:d.a,size:30})):Object(s.createElement)(j.a,{className:"wc-block-stock-filter-list",options:z,checked:$,onChange:oe,isLoading:ae,isDisabled:re})),Object(s.createElement)("div",{className:"wc-block-stock-filter__actions"},($.length>0||c)&&!ae&&Object(s.createElement)(k.a,{onClick:()=>{q([]),te([])},screenReaderLabel:Object(r.__)("Reset stock filter","woocommerce")}),t.showFilterButton&&Object(s.createElement)(p.a,{className:"wc-block-stock-filter__button",isLoading:ae,disabled:ae||re,onClick:()=>te($)})))};t.default=e=>{const t=Object(n.a)(e),c=(l=e,{heading:Object(a.a)(null==l?void 0:l.heading)?l.heading:"",headingLevel:Object(a.a)(null==l?void 0:l.headingLevel)&&parseInt(l.headingLevel,10)||F.attributes.headingLevel.default,showFilterButton:"true"===(null==l?void 0:l.showFilterButton),showCounts:"false"!==(null==l?void 0:l.showCounts),isPreview:!1,displayStyle:Object(a.a)(null==l?void 0:l.displayStyle)&&l.displayStyle||F.attributes.displayStyle.default,selectType:Object(a.a)(null==l?void 0:l.selectType)&&l.selectType||F.attributes.selectType.default});var l;return Object(s.createElement)("div",{className:o()(Object(a.a)(e.className)?e.className:"",t.className),style:t.style},Object(s.createElement)(P,{isEditor:!1,attributes:c}))}}}]);