1import{q as M,T as w,k as D,A as Z,h as N}from"./esnext-vendor.DDpcaSqU.js";import{T as ve,dH as Ee,bd as x,dI as Me,dJ as ee,ak as _e,b7 as De,dB as me,I as Q,dK as he,dL as Oe}from"./utilities-previous.Cnwy8G2v.js";import{s as Ue}from"./BillingAddressForm.B1Xpg2b3.js";import{u as Le}from"./FormLayout.UkTBTXMZ.js";import{cp as ke,e8 as Ne,e9 as $e,s as te,cU as Be}from"./hydrate.BcfGVx0L.js";import{A as Ge}from"./shared-permissions.B59tuyWB.js";function Ve(e){const{observability:t}=ve(),{geolocation:s}=ke();return M(async(o,d,u)=>{const n=await e(o,d,u);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)=>{Ne(e.approvalScopes,Ge.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 Qe(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:d,label:u,matchedSubstrings:n,formattedAddress:r}]of s.entries()){if(q(u)||t.push(`label is required and must be a non-empty string: received ${typeof u} in suggestions[${o}]`),d&&!q(d)&&t.push(`id must be a non-empty string: received ${typeof d} 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:d,company:u,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`),d&&!q(d)&&t.push(`city must be a non-empty string: received type ${typeof d} in formattedAddress`),u&&!q(u)&&t.push(`company must be a non-empty string: received type ${typeof u} 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"),d=w(()=>o.length>0,[o]);return M(u=>s?new Promise((n,r)=>{He(d).then(()=>we(s)).then(()=>{const{internalId:p,...a}=u;return t({target:{selectedSuggestion:a}})}).then(p=>{const a=p?.formattedAddress||{};n({formattedAddress:Qe(a)})}).catch(p=>{xe(p,s,r),n({formattedAddress:{}})})}):Promise.resolve({formattedAddress:{}}),[t,s,d])}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 Je(e){return e.map((t,s)=>{const{id:o,label:d,matchedSubstrings:u,formattedAddress:n}=t;return{internalId:`extension-suggestion-${s}`,matchedSubstrings:Ke(u),label:d,...o&&{id:o},...n&&{formattedAddress:n}}})}function Ke(e){return e?.length?e.sort(We):[{offset:0,length:0}]}function We(e,t){return e.offset-t.offset}function Ye(e){const t=$e(e),s=w(()=>e[0],[e]);return M((o,d)=>s?new Promise((u,n)=>{d.aborted&&n(new ee(d.reason)),we(s).then(()=>t({signal:d,target:{...o}})).then(r=>r?.suggestions||[]).then(r=>je(r)).then(r=>Je(r)).then(r=>u(r)).catch(r=>{xe(r,s,n),u([])}),d.addEventListener("abort",()=>{n(new ee(d.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 d=0,u=0,n=0,r=0,p,a,c=!1,B=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,++d,o()]},recordPredictionResponse([h,A,_],O){const T=A<u,P=u;u=Math.max(u,A),a=h,c=T,!(!T||B===0)&&(B-=1,C("prediction-race",{latestAppliedPredictionGeneration:P,predictionCount:O,queryChangedSinceRequest:h!==r,requestGeneration:A,responseAgeMs:Math.max(0,o()-_)}))},startAddressRequest(){const h=c||a!==void 0&&a!==r;return[r,++n,h,o()]},recordAddressResponse([h,A,_,O]){const T=A!==n,P=h!==r;!T&&!P&&!_||E===0||(E-=1,C("address-race",{latestRequestGeneration:n,newerSelectionStarted:T,queryChangedSinceSelection:P,requestGeneration:A,responseAgeMs:Math.max(0,o()-O),selectedFromStalePredictionResponse:_}))}}}function dt({addressType:e,countryCode:t,field:s,initialAddressQuery:o="",provider:d="autocomplete-service",allowAllCountries:u}){const{i18n:{locale:n},observability:r,userEvents:p,shop:a,source:c,checkout:{address:B}}=ve(),{id:E}=a,[C,h]=D(o),[A,_]=D(C),[O,T]=D(!1),[P,F]=D([]),[se,X]=D([]),H=Z(!1),[z,Ie]=D(Se(c.sourceId)
1),{geolocation:y}=ke(),{search:Re,fetchAddress:oe}=Le(),I=Be(),{details:ne}=_e(t),G=De(),re=B.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,de=te("purchase.address-autocomplete.suggest"),V=w(()=>de.map(me),[de]),qe=Ye(V),ue=w(()=>V.length>0,[V]),ae=te("purchase.address-autocomplete.format-suggestion"),J=w(()=>ae.map(me),[ae]),pe=Xe(J),le=w(()=>J.length>0,[J]),fe=M((l,g,$,m)=>{const S=G.value.defaultAttributes?.uniqToken||"";p.monorailEvent({schemaId:"checkout_autocomplete_suggestion/5.1",payload:{shopId:parseInt(Q(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,G.value.defaultAttributes?.uniqToken,p,y.coordinates]),K=w(()=>ue&&s&&j(e),[s,e,ue]),W=w(()=>K||u||Ue(t||y.country.code),[u,t,y.country.code,K]);N(()=>{const l=setTimeout(()=>{I&&_(C)},Ze);return()=>{clearTimeout(l)}},[I,C]),N(()=>{k(()=>b.recordQuery(o)),h(o)},[o,b]),N(()=>{W||F([])},[W]);const Y=Z(t);N(()=>{Y.current!==t&&Y.current!==void 0&&(F([]),X([]),H.current=!1),Y.current=t},[t]);const Ce=Ve(Re);N(()=>{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:Q(E),sourceId:c.sourceId||"",checkoutSessionIdentifier:c.checkoutSessionIdentifier||""},ie);I.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}`),I.current&&!l&&F([])}}async function $({query:m,field:S,signal:v}){function U(i){return i.map(({label:f,matchedSubstrings:R,internalId:ze})=>({addressId:ze,completionService:"AUTOCOMPLETE_EXTENSION",description:f,matchedSubstrings:R}))}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);I.current&&!l&&(i&&k(()=>b.recordPredictionResponse(i,f.length)),X(f),F(U(f)))}catch(i){if(i instanceof he&&r.error(i),V[0].type==="local")if(i instanceof ee)console.warn("AddressAutocompleteSignalAbortedError: ",i.message);else throw i}}if(W&&O)if(K&&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=G.value.defaultAttributes?.uniqToken,L=$.findIndex(i=>i.addressId===l)+1;p.monorailEvent({schemaId:"checkout_autocomplete_selection/6.2",payload:{shopId:parseInt(Q(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,G.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(R=>R.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),R=f?.formattedAddress||await U(f);return tt(R)}try{let i,f;if(g==="AUTOCOMPLETE_EXTENSION")i=await L(l);else{const R=z;i=await oe(l,{locale:n,requestToken:z,completionService:g,shopId:Q(E),sourceId:c.sourceId||"",checkoutSessionIdentifier:c.checkoutSessionIdentifier||"",extendedFields:!$&&re}
1),f=R,I.current&&(Ie(Se(c.sourceId)),j(e)&&ge(l,i,P,e,R))}I.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,I,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:d}}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{dt 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.