(()=>{"use strict";const e={n:r=>{const t=r&&r.__esModule?()=>r.default:()=>r;return e.d(t,{a:t}),t},d:(r,t)=>{if(Array.isArray(t))for(var n=0;n<t.length;){var o=t[n++],i=t[n++];e.o(r,o)?0===i&&n++:0===i?Object.defineProperty(r,o,{enumerable:!0,value:t[n++]}):Object.defineProperty(r,o,{enumerable:!0,get:i})}else for(var o in t)e.o(t,o)&&!e.o(r,o)&&Object.defineProperty(r,o,{enumerable:!0,get:t[o]})},o:(e,r)=>Object.hasOwn(e,r)},r=window.wp.element,t=window.wp.i18n,n=window.wp.a11y,o=window.wp.apiFetch;var i=e.n(o);const s=window.wp.components,a=window.ReactJSXRuntime;function u({fieldRef:e,label:t,hideLabelFromVision:n,options:o,value:i,onChange:u,onFilterValueChange:c}){const l=(0,r.useRef)(null);return(0,r.useEffect)(()=>{const e=l.current&&l.current.querySelector('input[role="combobox"]');if(!e)return;const r=()=>{const r=e.getAttribute("aria-owns");r?r!==e.getAttribute("aria-controls")&&e.setAttribute("aria-controls",r):e.hasAttribute("aria-controls")&&e.removeAttribute("aria-controls")};r();const t=new window.MutationObserver(r);return t.observe(e,{attributes:!0,attributeFilter:["aria-owns"]}),()=>t.disconnect()},[]),(0,a.jsx)("div",{className:"wrenchpress-modal__field",ref:r=>{l.current=r,e&&(e.current=r)},onKeyDownCapture:e=>{if("Escape"!==e.key)return;const r=e.currentTarget.querySelector('input[role="combobox"]');r&&"true"===r.getAttribute("aria-expanded")||e.stopPropagation()},children:(0,a.jsx)(s.ComboboxControl,{__next40pxDefaultSize:!0,label:t,hideLabelFromVision:!!n,options:o,value:i||null,onChange:e=>u(e||""),onFilterValueChange:c})})}function c(e){return String(e??"").normalize("NFKD").replace(/[̀-ͯ]/g,"").normalize("NFKC").toLocaleLowerCase().replace(/[‐-―−﹘﹣－]/g,"-")}function l(e,r){return c(e).indexOf(r)>=0}function d(e){return String(e??"").replace(/\D+/g,"")}function p({label:e,query:r,identifiers:n}){const o=String(e??""),i=c(r);if(""===i)return o;if(l(o,i))return o;const s=(n||[]).filter(e=>""!==String(e[1]||"").trim());let a=s.find(e=>l(e[1],i));if(!a){const e=d(r);""!==e&&(a=s.find(r=>{const t=d(r[1]);return""!==t&&t.indexOf(e)>=0}))}const u=a?`${o} - ${a[0]} ${a[1]}`:o;return l(u,i)?u:(0,t.sprintf)(
// translators: 1: the option label, possibly with its matched identifier. 2: the text the user typed.
// translators: 1: the option label, possibly with its matched identifier. 2: the text the user typed.
(0,t.__)('%1$s (matched "%2$s")',"pitdesk-repair-crm"),u,String(r))}function m(e){return[[(0,t.__)("Phone","pitdesk-repair-crm"),e.phone],[(0,t.__)("Email","pitdesk-repair-crm"),e.email]]}const w={customers:"search/customers",vehicles:"search/vehicles"};function f(e,r){e&&window.requestAnimationFrame(()=>{window.setTimeout(()=>(0,n.speak)(e,r),0)})}const g=window.wrenchpressCustomerPicker||{},b=g.i18n||{},h="wrenchpress-customer-picker-error";function v(e){return""!==e.value&&"0"!==e.value}function S({select:e}){const o=g.selectedCustomer||null,[s,c]=(0,r.useState)(v(e)?e.value:""),[l,d]=(0,r.useState)(""),[S,y]=(0,r.useState)(""),[_,k]=(0,r.useState)(o),A=(0,r.useRef)(null),{options:C,rows:x,status:R}=function({restBase:e,resource:n,query:o,customerId:s,perPage:a,pinned:u,identifiersOf:c,i18n:l}){const[d,m]=(0,r.useState)({loading:!1,error:"",rows:[],total:0,appliedQuery:""}),g=(0,r.useRef)(0),b=(0,r.useRef)(null),h=l||{},v=String(o||""),S=s?String(s):"",y=Number(a)||20;(0,r.useEffect)(()=>{const r=""===v?0:250,o=window.setTimeout(()=>{const r=g.current+1;g.current=r,b.current&&b.current.abort();const o="function"==typeof window.AbortController?new window.AbortController:null;b.current=o,m(e=>({...e,loading:!0,error:""}));const s=window.setTimeout(()=>{r===g.current&&f(h.searching||(0,t.__)("Searching...","pitdesk-repair-crm"),"polite")},600),a=new window.URLSearchParams;a.set("search",v),a.set("per_page",String(y)),S&&a.set("customer_id",S),i()({url:`${e}/${w[n]||""}?${a.toString()}`,signal:o?o.signal:void 0}).then(e=>{if(window.clearTimeout(s),r!==g.current)return;const n=e&&e.items||[];m({loading:!1,error:"",rows:n,total:Number(e&&e.total)||0,appliedQuery:v}),0===n.length&&f(h.noResults||(0,t.__)("No results found.","pitdesk-repair-crm"),"polite")}).catch(e=>{if(window.clearTimeout(s),e&&"AbortError"===e.name)return;if(r!==g.current)return;const n=h.searchError||(0,t.__)("The search could not be completed. Try again.","pitdesk-repair-crm");m(e=>({...e,loading:!1,error:n})),f(n,"assertive")})},r);return()=>window.clearTimeout(o)},[e,n,v,S,y]);const _=u&&u.value?String(u.value):"",k=u&&u.label?String(u.label):"",A=(0,r.useMemo)(()=>{const e=d.rows.map(e=>({value:String(e.id),label:p({label:e.label,query:d.appliedQuery,identifiers:c?c(e):[]})}));return _&&!e.some(e=>e.value===_)&&e.unshift({value:_,label:k}),e},[d.rows,d.appliedQuery,_,k,c]);let C="";return d.loading?C=h.searching||(0,t.__)("Searching...","pitdesk-repair-crm"):d.error?C=d.error:0===d.rows.length&&""!==d.appliedQuery?C=h.noResults||(0,t.__)("No results found.","pitdesk-repair-crm"):d.total>d.rows.length&&(C=(h.showingOf||"").replace("%1$s",String(d.rows.length)).replace("%2$s",String(d.total))),{options:A,rows:d.rows,total:d.total,loading:d.loading,error:d.error,status:C}}({restBase:g.restBase||"",resource:"customers",query:l,perPage:g.perPage||20,pinned:_,identifiersOf:m,i18n:b}),q=()=>A.current&&A.current.querySelector('input[role="combobox"]');return(0,r.useEffect)(()=>{const e=q();e&&(S?(e.setAttribute("aria-invalid","true"),e.setAttribute("aria-describedby",h)):(e.removeAttribute("aria-invalid"),e.removeAttribute("aria-describedby")))},[S]),(0,r.useEffect)(()=>{const r=e.form;if(!r)return;const o=r=>{if(v(e))return;r.preventDefault();const o=b.chooseCustomer||(0,t.__)("Choose the customer who owns this vehicle.","pitdesk-repair-crm");y(o);const i=q();i&&i.focus(),window.requestAnimationFrame(()=>{window.setTimeout(()=>(0,n.speak)(o,"assertive"),0)})};return r.addEventListener("submit",o,!0),()=>r.removeEventListener("submit",o,!0)},[e]),(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(u,{fieldRef:A,hideLabelFromVision:!0,label:b.customer||(0,t.__)("Customer","pitdesk-repair-crm"),options:C,value:s,onChange:r=>{c(r),d("");const t=x.find(e=>String(e.id)===String(r));t&&k({value:String(t.id),label:t.label}),r&&function(e,r,t){if(!r)return;const n=Array.from(e.options).some(e=>e.value===String(r));if(n)return;const o=document.createElement("option");o.value=String(r),o.text=String(t||r),e.appendChild(o)}(e,r,t?t.label:""),e.value=r||"",r&&y("")},onFilterValueChange:d}),R?(0,a.jsx)("p",{id:"wrenchpress-customer-picker-status",className:"wrenchpress-picker-status description",style:{margin:"0.5em 0 0"},children:R}):null,S?(0,a.jsx)("p",{id:h,className:"wrenchpress-picker-error",style:{color:"#8a2424",margin:"0.5em 0 0"},children:S}):null]})}function y(){const e=document.querySelector('select[data-wrenchpress-picker="customer"]');if(!e)return;const t=document.querySelector(".wrenchpress-picker-mount");t&&(document.querySelectorAll(".wrenchpress-picker-cap-notice").forEach(e=>e.remove()),e.removeAttribute("required"),e.hidden=!0,(0,r.createRoot)(t).render((0,a.jsx)(S,{select:e})))}g.nonce&&i().use(i().createNonceMiddleware(g.nonce)),i().use(i().createRootURLMiddleware("/wp-json/")),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",y):y()})();