1import{q as M,T as w,k as _,A as Z,h as G}from"./esnext-vendor.7EcvH7tF.js";import{Q as ve,dC as Ee,b9 as x,dD as Me,dE as ee,ah as De,c6 as _e,dw as me,H as V,dF as he,dG as Oe}from"./utilities-previous.BNOUhCqt.js";import{s as Ue}from"./BillingAddressForm.BLCoRu_E.js";import{u as Le}from"./components-RedirectionNotice.module.wDj6pdp6.js";import{ck as ke,e3 as Ge,e4 as $e,p as te,cQ as Ne}from"./hydrate.CvWSYK3c.js";import{A as Be}from"./shared-permissions.FyWsJaGm.js";function Qe(e){const{observability:t}=ve(),{geolocation:s}=ke();return M(async(o,u,d)=>{const n=await e(o,u,d);return t.histogram({name:"prediction_duration",value:n.duration.end-n.duration.start,attributes:{buyerCountry:s.country.code}}),n.data},[e,t,s.country.code])}function we(e){return new Promise((t,s)=>{Ge(e.approvalScopes,Be.CustomerPersonalData)||s(new Ee(`Extension ${e.id} at target \`${e.extensionPoint.target}\` is missing required access to read customer personal data.`)),t()})}function xe(e,t,s){switch(!0){case e instanceof Ee:Ae(e,t);break;case e instanceof x:if(t.type==="persisted")Ae(e,t);else{const{extensionPoint:{target:o}}=t;s(new x(`[${o}] ${e.message}`))}break;default:s(e)}}function Ae(e,t){const s=`[${t.extensionPoint.target}] ${e.name}: ${e.message}`;console.error(s)}function Ve(e){if(typeof e!="object")throw new x(`formattedAddress must be an object: received type ${typeof e}`);const t=Pe(e);if(t.length)throw new x(t.join("; "));return e}function je(e){const t=[];if(!Array.isArray(e))throw new x("An array of address suggestions must be defined");const s=e.slice(0,5);for(const[o,{id:u,label:d,matchedSubstrings:n,formattedAddress:r}]of s.entries()){if(q(d)||t.push(`label is required and must be a non-empty string: received ${typeof d} in suggestions[${o}]`),u&&!q(u)&&t.push(`id must be a non-empty string: received ${typeof u} in suggestions[${o}]`),n)if(Array.isArray(n))for(const[p,{offset:a,length:c}]of n.entries())typeof a!="number"&&t.push(`offset must be a number: received type ${typeof a} in matchedSubstrings[${p}] for suggestions[${o}]`),typeof c!="number"&&t.push(`length must be a number: received type ${typeof c} in matchedSubstrings[${p}] for suggestions[${o}]`);else t.push(`matchedSubstrings must be an array: received type ${typeof n} in suggestions[${o}]`);if(r)if(typeof r=="object"){const p=Pe(r);for(const a of p)t.push(`${a} for suggestions[${o}]`)}else t.push(`formattedAddress must be an object: received type ${typeof r} in suggestions[${o}]`);if(t.length)throw new x(t.join("; "))}return s}function Pe(e){const t=[],{address1:s,address2:o,city:u,company:d,provinceCode:n,zip:r,countryCode:p,latitude:a,longitude:c}=e;return s&&!q(s)&&t.push(`address1 must be a non-empty string: received type ${typeof s} in formattedAddress`),o&&!q(o)&&t.push(`address2 must be a non-empty string: received type ${typeof o} in formattedAddress`),u&&!q(u)&&t.push(`city must be a non-empty string: received type ${typeof u} in formattedAddress`),d&&!q(d)&&t.push(`company must be a non-empty string: received type ${typeof d} in formattedAddress`),n&&!q(n)&&t.push(`provinceCode must be a non-empty string: received type ${typeof n} in formattedAddress`),r&&!q(r)&&t.push(`zip must be a non-empty string: received type ${typeof r} in formattedAddress`),p&&!Me(p)&&t.push(`countryCode must be a valid alpha-2 country code: received "${p}" in formattedAddress`),a&&(typeof a!="number"?t.push(`latitude must be a number: received type ${typeof a} in formattedAddress`):(a<-90||a>90)&&t.push(`latitude must be between -90 and 90: received ${a} in formattedAddress`)),c&&(typeof c!="number"?t.push(`longitude must be a number: received type ${typeof c} in formattedAddress`):(c<-180||c>180)&&t.push(`longitude must be between -180 and 180: received ${c} in formattedAddress`)),t}function q(e){return typeof e=="string"||e instanceof String?e.trim().length>0:!1}function Xe(e){const t=$e(e),s=w(()=>e[0],[e]),o=te("purchase.address-autocomplete.suggest"),u=w(()=>o.length>0,[o]);return M(d=>s?new Promise((n,r)=>{He(u).then(()=>we(s)).then(()=>{const{internalId:p,...a}=d;return t({target:{selectedSuggestion:a}})}).then(p=>{const a=p?.formattedAddress||{};n({formattedAddress:Ve(a)})}).catch(p=>{xe(p,s,r),n({formattedAddress:{}})})}):Promise.resolve({formattedAddress:{}}),[t,s,u])}function He(e){return new Promise((t,s)=>{if(e)t();else{const o="A purchase.address-autocomplete.suggest extension is required for a purchase.address-autocomplete.format-suggestion extension";s(new x(o))}})}function We(e){return e.map((t,s)=>{const{id:o,label:u,matchedSubstrings:d,formattedAddress:n}=t;return{internalId:`extension-suggestion-${s}`,matchedSubstrings:Ye(d),label:u,...o&&{id:o},...n&&{formattedAddress:n}}})}function Ye(e){return e?.length?e.sort(Je):[{offset:0,length:0}]}function Je(e,t){return e.offset-t.offset}function Ke(e){const t=$e(e),s=w(()=>e[0],[e]);return M((o,u)=>s?new Promise((d,n)=>{u.aborted&&n(new ee(u.reason)),we(s).then(()=>t({signal:u,target:{...o}})).then(r=>r?.suggestions||[]).then(r=>je(r)).then(r=>We(r)).then(r=>d(r)).catch(r=>{xe(r,s,n),d([])}),u.addEventListener("abort",()=>{n(new ee(u.reason))})}):Promise.resolve([]),[t,s])}const Ze=300,ye="address_clobber",be=20;function k(e){try{return e()}catch{return}}function et({addressType:e,field:t,log:s,now:o=Date.now}){let u=0,d=0,n=0,r=0,p,a,c=!1,N=be,E=be;const C=(h,A)=>s(ye,`${ye}: ${h}`,{kind:h,addressType:e??"unknown",field:t??"unknown",...A});return{recordQuery(h){h!==p&&(p=h,r+=1)},resetPredictionContext(){a=void 0,c=!1},startPredictionRequest(){return[r,++u,o()]},recordPredictionResponse([h,A,D],O){const T=A<d,P=d;d=Math.max(d,A),a=h,c=T,!(!T||N===0)&&(N-=1,C("prediction-race",{latestAppliedPredictionGeneration:P,predictionCount:O,queryChangedSinceRequest:h!==r,requestGeneration:A,responseAgeMs:Math.max(0,o()-D)}))},startAddressRequest(){const h=c||a!==void 0&&a!==r;return[r,++n,h,o()]},recordAddressResponse([h,A,D,O]){const T=A!==n,P=h!==r;!T&&!P&&!D||E===0||(E-=1,C("address-race",{latestRequestGeneration:n,newerSelectionStarted:T,queryChangedSinceSelection:P,requestGeneration:A,responseAgeMs:Math.max(0,o()-O),selectedFromStalePredictionResponse:D}))}}}function ut({addressType:e,countryCode:t,field:s,initialAddressQuery:o="",provider:u="autocomplete-service",allowAllCountries:d}){const{i18n:{locale:n},observability:r,userEvents:p,shop:a,source:c,checkout:{address:N}}=ve(),{id:E}=a,[C,h]=_(o),[A,D]=_(C),[O,T]=_(!1),[P,F]=_([]),[se,X]=_([]),H=Z(!1),[z,Re]=_(Se(c.sourceId)
1),{geolocation:y}=ke(),{search:Ie,fetchAddress:oe}=Le(),R=Ne(),{details:ne}=De(t),B=_e(),re=N.extendedAddressMode.get(t)!==null,ie=e==="pickup"?"google":void 0,ce=Z(void 0);ce.current??=et({addressType:e,field:s,log:(l,g,$)=>r.log(l,g,$)});const b=ce.current,ue=te("purchase.address-autocomplete.suggest"),Q=w(()=>ue.map(me),[ue]),qe=Ke(Q),de=w(()=>Q.length>0,[Q]),ae=te("purchase.address-autocomplete.format-suggestion"),W=w(()=>ae.map(me),[ae]),pe=Xe(W),le=w(()=>W.length>0,[W]),fe=M((l,g,$,m)=>{const S=B.value.defaultAttributes?.uniqToken||"";p.monorailEvent({schemaId:"checkout_autocomplete_suggestion/5.1",payload:{shopId:parseInt(V(E),10),checkoutToken:c.checkoutSessionIdentifier||"",uniqueToken:S,provider:g[0]?.completionService??"",context:l==="shipping"?"Shipping address":"Billing address",territoryCode:t??y.country.code,locale:n,query:$,sessionToken:z,requestProvider:m,requestLatitude:y.coordinates?.latitude,requestLongitude:y.coordinates?.longitude,addressIds:g.map(v=>v.addressId)}})},[t,y.country.code,n,z,E,c.checkoutSessionIdentifier,B.value.defaultAttributes?.uniqToken,p,y.coordinates]),Y=w(()=>de&&s&&j(e),[s,e,de]),J=w(()=>Y||d||Ue(t||y.country.code),[d,t,y.country.code,Y]);G(()=>{const l=setTimeout(()=>{R&&D(C)},Ze);return()=>{clearTimeout(l)}},[R,C]),G(()=>{k(()=>b.recordQuery(o)),h(o)},[o,b]),G(()=>{J||F([])},[J]);const K=Z(t);G(()=>{K.current!==t&&K.current!==void 0&&(F([]),X([]),H.current=!1),K.current=t},[t]);const Ce=Qe(Ie);G(()=>{let l=!1;async function g(m){try{if(!m||typeof m!="string"||m.length<=1){k(()=>b.resetPredictionContext()),F([]);return}const S=k(()=>b.startPredictionRequest()),v=await Ce(m,{locale:n,countryCode:t??y.country.code,location:y.coordinates,requestToken:z,shopId:V(E),sourceId:c.sourceId||"",checkoutSessionIdentifier:c.checkoutSessionIdentifier||""},ie);R.current&&!l&&(S&&k(()=>b.recordPredictionResponse(S,v.length)),F(v),!H.current&&j(e)&&(fe(e,v,m,ie),H.current=!0))}catch(S){console.warn(`Unable to fetch predictions: ${S}`),R.current&&!l&&F([])}}async function $({query:m,field:S,signal:v}){function U(i){return i.map(({label:f,matchedSubstrings:I,internalId:ze})=>({addressId:ze,completionService:"AUTOCOMPLETE_EXTENSION",description:f,matchedSubstrings:I}))}function L(i){switch(i){case"address1":case"streetName":return"address1";case"postalCode":return"zip";default:{const f=i;throw new he(`Unknown Address Autocomplete field: ${f}`)}}}if(!m||typeof m!="string"||m.length<=1){k(()=>b.resetPredictionContext()),X([]),F([]);return}try{const i=k(()=>b.startPredictionRequest()),f=await qe({value:m,field:L(S),selectedCountryCode:t},v);R.current&&!l&&(i&&k(()=>b.recordPredictionResponse(i,f.length)),X(f),F(U(f)))}catch(i){if(i instanceof he&&r.error(i),Q[0].type==="local")if(i instanceof ee)console.warn("AddressAutocompleteSignalAbortedError: ",i.message);else throw i}}if(J&&O)if(Y&&j(e)){const m=new AbortController;return $({query:A,field:s,signal:m.signal}),()=>{l=!0,m.abort("The query was debounced")}}else g(A);return()=>{l=!0}},[t,A,n,z,fe]);const ge=M((l,g,$,m,S)=>{const v=ne?.zones.find(i=>i.code===g.zoneCode)?.name||void 0,U=B.value.defaultAttributes?.uniqToken,L=$.findIndex(i=>i.addressId===l)+1;p.monorailEvent({schemaId:"checkout_autocomplete_selection/6.2",payload:{shopId:parseInt(V(E),10),checkoutToken:c.checkoutSessionIdentifier||"",uniqueToken:U,address1:g.address1,address2:g.address2,city:g.city,zone:v,latitude:g.coordinates?.latitude,longitude:g.coordinates?.longitude,zip:g.postalCode,territoryCode:t??y.country.code,position:L,query:A,context:m==="shipping"?"Shipping address":"Billing address",locale:n,sessionToken:S,requestLatitude:y.coordinates?.latitude,requestLongitude:y.coordinates?.longitude}})},[ne,B.value.defaultAttributes?.uniqToken,E,c.checkoutSessionIdentifier,t,y.country.code,y.coordinates,A,n,p]),Te=M(async(l,g,$,m)=>{const S=k(()=>b.startAddressRequest());function v(i){const f=se.find(I=>I.internalId===i);return f||r.error(new Oe("Could not find selected suggestion in extension suggestions.")),f}async function U(i){if(le&&i)return(await pe(i)).formattedAddress;const f=new x("The address fields could not be auto-populated. Please provide a `formattedAddress` for the selected suggestion or implement the `purchase.address-autocomplete.format-suggestion` extension.");console.error(`${f.name}: ${f.message}`)}async function L(i){const f=v(i),I=f?.formattedAddress||await U(f);return tt(I)}try{let i,f;if(g==="AUTOCOMPLETE_EXTENSION")i=await L(l);else{const I=z;i=await oe(l,{locale:n,requestToken:z,completionService:g,shopId:V(E),sourceId:c.sourceId||"",checkoutSessionIdentifier:c.checkoutSessionIdentifier||"",extendedFields:!$&&re}
1),f=I,R.current&&(Re(Se(c.sourceId)),j(e)&&ge(l,i,P,e,I))}R.current&&(S&&k(()=>b.recordAddressResponse(S)),m(i,f))}catch(i){if(i instanceof x)throw i;console.warn(`Unable to select prediction: ${i}`)}},[n,z,E,c.sourceId,c.checkoutSessionIdentifier,oe,R,e,ge,P,le,se,pe,r,re,b]),Fe=M((l,g=!0)=>{k(()=>b.recordQuery(l)),h(l),T(g)},[b]);return{value:C,debouncedValue:A,setValue:Fe,predictions:P,selectPrediction:Te,provider:u}}function Se(e){return`${e}-${Date.now()}`}function tt(e){return{address1:e?.address1,address2:e?.address2,city:e?.city,company:e?.company,zoneCode:e?.provinceCode,postalCode:e?.zip,coordinates:e?.latitude&&e?.longitude?{latitude:e.latitude,longitude:e.longitude}:void 0}}function j(e){return e==="billing"||e==="shipping"}export{ut as u};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.