@pro6pp/infer-js 0.0.2-beta.13 → 0.0.2-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var m=Object.defineProperty,A=Object.defineProperties,k=Object.getOwnPropertyDescriptor,_=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,C=Object.getOwnPropertySymbols;var E=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var I=(o,e,t)=>e in o?m(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,c=(o,e)=>{for(var t in e||(e={}))E.call(e,t)&&I(o,t,e[t]);if(C)for(var t of C(e))D.call(e,t)&&I(o,t,e[t]);return o},b=(o,e)=>A(o,_(e));var F=(o,e)=>{for(var t in e)m(o,t,{get:e[t],enumerable:!0})},R=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of B(e))!E.call(o,n)&&n!==t&&m(o,n,{get:()=>e[n],enumerable:!(i=k(e,n))||i.enumerable});return o};var $=o=>R(m({},"__esModule",{value:!0}),o);var U={};F(U,{InferJS:()=>y,attach:()=>N});module.exports=$(U);var f={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},L={DIGITS_1_3:/^[0-9]{1,3}$/},T={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},x=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||f.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:f.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,s)=>fetch(r,s)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=c({},T);let i=e.debounceMs!==void 0?e.debounceMs:f.DEBOUNCE_MS,n=Math.max(i,f.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),n)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let s=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];s&&(this.selectItem(s),this.updateState({selectedSuggestionIndex:-1}));return}}let n=t.value;if(e.key===" "&&this.shouldAutoInsertComma(n)){e.preventDefault();let r=`${n.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let n=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!n&&Object.keys(n).length>0;if(this.state.stage==="final"||r){let d=t;if(n&&Object.keys(n).length>0){let{street:l,street_number:a,city:h,addition:p}=n;if(l&&a&&h){let g=p?` ${p}`:"";d=`${l} ${a}${g}, ${h}`}}return this.finishSelection(d,n),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&L.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return L.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:n}=this.state,r=n;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(n);!a||!a.includes(t)?r=a?`${a} ${e}, ${t}, `:`${e}, ${t}, `:r=a?`${a} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!n.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(n,e),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,h;let i=(e||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let n=(h=this.abortController)==null?void 0:h.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${f.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey);let d=r.includes("?")?"&":"?",l=`${r}${d}${s.toString()}`;this.fetcher(l,{signal:n}).then(p=>{if(!p.ok){if(t<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(e,t,n);throw new Error("Network error")}return p.json()}).then(p=>{p&&this.mapResponseToState(p)}).catch(p=>{if(p.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,n);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let n=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},n)}mapResponseToState(e){var d,l;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],n=[],r=new Set;for(let a of i){let h=`${a.label}|${a.subtitle||""}|${JSON.stringify(a.value||{})}`;r.has(h)||(r.add(h),n.push(a))}let s=n.length+(((d=e.cities)==null?void 0:d.length)||0)+(((l=e.streets)==null?void 0:l.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],t.suggestions=[]):(t.suggestions=n,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&n.length===1&&this.selectItem(n[0])}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(b(c({},T),{query:this.state.query}))}updateState(e){this.state=c(c({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,n=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return n.cancel=()=>{i&&(clearTimeout(i),i=void 0)},n}};function M(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],i=o.toLowerCase(),n=e.toLowerCase(),r=0,s=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&i[a]===n[r]))continue;a>s&&t.push({text:o.slice(s,a),match:!1}),t.push({text:o[a],match:!0}),r++,s=a+1}return s<o.length&&t.push({text:o.slice(s),match:!1}),r===e.length?t:[{text:o,match:!1}]}var S=`
1
+ "use strict";var m=Object.defineProperty,_=Object.defineProperties,k=Object.getOwnPropertyDescriptor,R=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,I=Object.getOwnPropertySymbols;var L=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable;var T=(o,e,t)=>e in o?m(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))L.call(e,t)&&T(o,t,e[t]);if(I)for(var t of I(e))F.call(e,t)&&T(o,t,e[t]);return o},b=(o,e)=>_(o,R(e));var D=(o,e)=>{for(var t in e)m(o,t,{get:e[t],enumerable:!0})},N=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of B(e))!L.call(o,s)&&s!==t&&m(o,s,{get:()=>e[s],enumerable:!(i=k(e,s))||i.enumerable});return o};var $=o=>N(m({},"__esModule",{value:!0}),o);var H={};D(H,{InferJS:()=>y,attach:()=>U});module.exports=$(H);var g={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},S={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},M={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},x=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||g.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:g.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,n)=>fetch(r,n)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},M);let i=e.debounceMs!==void 0?e.debounceMs:g.DEBOUNCE_MS,s=Math.max(i,g.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),s)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:t?null:this.state.stage}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let n=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];n&&(this.selectItem(n),this.updateState({selectedSuggestionIndex:-1}));return}}let s=t.value;if(e.key===" "&&this.shouldAutoInsertComma(s)){e.preventDefault();let r=`${s.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let s=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!s&&Object.keys(s).length>0;if(this.state.stage==="final"||r){let d=t;if(s&&Object.keys(s).length>0){let{street:p,street_number:a,city:h,addition:l}=s;if(p&&a&&h){let c=l?` ${l}`:"";d=`${p} ${a}${c}, ${h}`}}return this.finishSelection(d,s),!0}let n=typeof e!="string"?e.subtitle:null;return this.processSelection(i,n),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&S.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return S.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:s}=this.state,r=s;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(s),h=!a||!a.includes(t),l=a;if(a&&t){let c=a.match(S.STREET_NUMBER_PREFIX);if(c){let C=c[1];t.startsWith(C)&&(l="")}}h?r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `:r=l?`${l} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!s.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(s,e),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,h;let i=(e||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let s=(h=this.abortController)==null?void 0:h.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${g.API_URL}/infer/${this.country.toLowerCase()}`,n=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&n.append("country",this.country.toLowerCase()),this.authKey&&n.set("authKey",this.authKey);let d=r.includes("?")?"&":"?",p=`${r}${d}${n.toString()}`;this.fetcher(p,{signal:s}).then(l=>{if(!l.ok){if(t<this.maxRetries&&(l.status>=500||l.status===429))return this.retry(e,t,s);throw new Error("Network error")}return l.json()}).then(l=>{l&&this.mapResponseToState(l)}).catch(l=>{if(l.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,s);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let s=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},s)}mapResponseToState(e){var d,p,a,h;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],s=[],r=new Set;for(let l of i){let c=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;r.has(c)||(r.add(c),s.push(l))}let n=s.length+(((d=e.cities)==null?void 0:d.length)||0)+(((p=e.streets)==null?void 0:p.length)||0);t.hasMore=n>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((a=t.cities)==null?void 0:a.length)===0&&((h=t.streets)==null?void 0:h.length)===0?t.suggestions=s:t.suggestions=[]):(t.suggestions=s,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&s.length===1&&this.selectItem(s[0])}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(b(f({},M),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,s=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return s.cancel=()=>{i&&(clearTimeout(i),i=void 0)},s}};function A(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],i=o.toLowerCase(),s=e.toLowerCase(),r=0,n=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&i[a]===s[r]))continue;a>n&&t.push({text:o.slice(n,a),match:!1}),t.push({text:o[a],match:!0}),r++,n=a+1}return n<o.length&&t.push({text:o.slice(n),match:!1}),r===e.length?t:[{text:o,match:!1}]}var w=`
2
2
  .pro6pp-wrapper {
3
3
  position: relative;
4
4
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
@@ -208,13 +208,13 @@
208
208
  @keyframes pro6pp-spin {
209
209
  to { transform: rotate(360deg); }
210
210
  }
211
- `;var y=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadMoreText=t.loadMoreText||"Show more results...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let s=t.inputClass.split(" ");this.input.classList.add(...s)}let n=document.createElement("div");n.className="pro6pp-input-addons",this.wrapper.appendChild(n),this.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",n.appendChild(this.loader),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
211
+ `;var y=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadMoreText=t.loadMoreText||"Show more results...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let n=t.inputClass.split(" ");this.input.classList.add(...n)}let s=document.createElement("div");s.className="pro6pp-input-addons",this.wrapper.appendChild(s),this.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",s.appendChild(this.loader),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
212
212
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
213
213
  <line x1="18" y1="6" x2="6" y2="18"></line>
214
214
  <line x1="6" y1="6" x2="18" y2="18"></line>
215
215
  </svg>
216
- `,n.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.loadMoreButton=document.createElement("button"),this.loadMoreButton.type="button",this.loadMoreButton.className="pro6pp-load-more",this.loadMoreButton.textContent=this.loadMoreText,this.loadMoreButton.style.display="none",this.dropdown.appendChild(this.loadMoreButton),this.core=new x(b(c({},t),{onStateChange:s=>{this.render(s),t.onStateChange&&t.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(s)}})),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=`${e.street} ${e.street_number}, ${e.city}`;this.core.selectItem({label:t,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=S,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),this.loadMoreButton.addEventListener("click",e=>{e.preventDefault(),this.core.loadMore()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.loader.style.display=e.isLoading?"block":"none",this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,n=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||n))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",this.loadMoreButton.style.display=e.hasMore?"block":"none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Loading suggestions...",this.list.appendChild(s);return}if(n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}t.forEach((s,d)=>{if(!s.label)return;let l=document.createElement("li");l.className="pro6pp-item",d===e.selectedSuggestionIndex&&l.classList.add("pro6pp-item--active"),l.setAttribute("role","option"),l.setAttribute("aria-selected",d===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",M(s.label,e.query).forEach(({text:u,match:w})=>{if(w){let v=document.createElement("strong");v.className="pro6pp-item__label--match",v.textContent=u,a.appendChild(v)}else a.appendChild(document.createTextNode(u))}),l.appendChild(a);let p=s.count!==void 0&&s.count!==null?s.count:"",g=s.subtitle||p;if(g!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${g}`,l.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
216
+ `,s.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.loadMoreButton=document.createElement("button"),this.loadMoreButton.type="button",this.loadMoreButton.className="pro6pp-load-more",this.loadMoreButton.textContent=this.loadMoreText,this.loadMoreButton.style.display="none",this.dropdown.appendChild(this.loadMoreButton),this.core=new x(b(f({},t),{onStateChange:n=>{this.render(n),t.onStateChange&&t.onStateChange(n)},onSelect:n=>{typeof n=="string"?this.input.value=n:n&&typeof n=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(n)}})),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=`${e.street} ${e.street_number}, ${e.city}`;this.core.selectItem({label:t,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=w,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),this.loadMoreButton.addEventListener("click",e=>{e.preventDefault(),this.core.loadMore()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.loader.style.display=e.isLoading?"block":"none",this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,s=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||s))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",this.loadMoreButton.style.display=e.hasMore?"block":"none",e.isLoading&&!i){let n=document.createElement("li");n.className="pro6pp-no-results",n.textContent="Loading suggestions...",this.list.appendChild(n);return}if(s){let n=document.createElement("li");n.className="pro6pp-no-results",n.textContent=this.noResultsText,this.list.appendChild(n);return}t.forEach((n,d)=>{if(!n.label)return;let p=document.createElement("li");p.className="pro6pp-item",d===e.selectedSuggestionIndex&&p.classList.add("pro6pp-item--active"),p.setAttribute("role","option"),p.setAttribute("aria-selected",d===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",A(n.label,e.query).forEach(({text:u,match:E})=>{if(E){let v=document.createElement("strong");v.className="pro6pp-item__label--match",v.textContent=u,a.appendChild(v)}else a.appendChild(document.createTextNode(u))}),p.appendChild(a);let l=n.count!==void 0&&n.count!==null?n.count:"",c=n.subtitle||l;if(c!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${c}`,p.appendChild(u)}if(n.value===void 0||n.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
217
217
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
218
218
  <polyline points="9 18 15 12 9 6"></polyline>
219
219
  </svg>
220
- `,l.appendChild(u)}l.onmousedown=u=>u.preventDefault(),l.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(l)})}};function N(o,e){return new y(o,e)}0&&(module.exports={InferJS,attach});
220
+ `,p.appendChild(u)}p.onmousedown=u=>u.preventDefault(),p.onclick=u=>{u.stopPropagation(),this.core.selectItem(n)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(p)})}};function U(o,e){return new y(o,e)}0&&(module.exports={InferJS,attach});
@@ -1,4 +1,4 @@
1
- "use strict";var Pro6PP=(()=>{var m=Object.defineProperty,A=Object.defineProperties,k=Object.getOwnPropertyDescriptor,_=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,C=Object.getOwnPropertySymbols;var E=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var I=(o,e,t)=>e in o?m(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,c=(o,e)=>{for(var t in e||(e={}))E.call(e,t)&&I(o,t,e[t]);if(C)for(var t of C(e))D.call(e,t)&&I(o,t,e[t]);return o},b=(o,e)=>A(o,_(e));var F=(o,e)=>{for(var t in e)m(o,t,{get:e[t],enumerable:!0})},R=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of B(e))!E.call(o,n)&&n!==t&&m(o,n,{get:()=>e[n],enumerable:!(i=k(e,n))||i.enumerable});return o};var $=o=>R(m({},"__esModule",{value:!0}),o);var U={};F(U,{InferJS:()=>y,attach:()=>N});var f={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},L={DIGITS_1_3:/^[0-9]{1,3}$/},T={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},x=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||f.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:f.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,s)=>fetch(r,s)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=c({},T);let i=e.debounceMs!==void 0?e.debounceMs:f.DEBOUNCE_MS,n=Math.max(i,f.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),n)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let s=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];s&&(this.selectItem(s),this.updateState({selectedSuggestionIndex:-1}));return}}let n=t.value;if(e.key===" "&&this.shouldAutoInsertComma(n)){e.preventDefault();let r=`${n.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let n=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!n&&Object.keys(n).length>0;if(this.state.stage==="final"||r){let d=t;if(n&&Object.keys(n).length>0){let{street:l,street_number:a,city:h,addition:p}=n;if(l&&a&&h){let g=p?` ${p}`:"";d=`${l} ${a}${g}, ${h}`}}return this.finishSelection(d,n),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&L.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return L.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:n}=this.state,r=n;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(n);!a||!a.includes(t)?r=a?`${a} ${e}, ${t}, `:`${e}, ${t}, `:r=a?`${a} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!n.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(n,e),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,h;let i=(e||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let n=(h=this.abortController)==null?void 0:h.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${f.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey);let d=r.includes("?")?"&":"?",l=`${r}${d}${s.toString()}`;this.fetcher(l,{signal:n}).then(p=>{if(!p.ok){if(t<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(e,t,n);throw new Error("Network error")}return p.json()}).then(p=>{p&&this.mapResponseToState(p)}).catch(p=>{if(p.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,n);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let n=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},n)}mapResponseToState(e){var d,l;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],n=[],r=new Set;for(let a of i){let h=`${a.label}|${a.subtitle||""}|${JSON.stringify(a.value||{})}`;r.has(h)||(r.add(h),n.push(a))}let s=n.length+(((d=e.cities)==null?void 0:d.length)||0)+(((l=e.streets)==null?void 0:l.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],t.suggestions=[]):(t.suggestions=n,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&n.length===1&&this.selectItem(n[0])}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(b(c({},T),{query:this.state.query}))}updateState(e){this.state=c(c({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,n=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return n.cancel=()=>{i&&(clearTimeout(i),i=void 0)},n}};function M(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],i=o.toLowerCase(),n=e.toLowerCase(),r=0,s=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&i[a]===n[r]))continue;a>s&&t.push({text:o.slice(s,a),match:!1}),t.push({text:o[a],match:!0}),r++,s=a+1}return s<o.length&&t.push({text:o.slice(s),match:!1}),r===e.length?t:[{text:o,match:!1}]}var S=`
1
+ "use strict";var Pro6PP=(()=>{var m=Object.defineProperty,_=Object.defineProperties,k=Object.getOwnPropertyDescriptor,R=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,I=Object.getOwnPropertySymbols;var L=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable;var T=(o,e,t)=>e in o?m(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))L.call(e,t)&&T(o,t,e[t]);if(I)for(var t of I(e))F.call(e,t)&&T(o,t,e[t]);return o},b=(o,e)=>_(o,R(e));var D=(o,e)=>{for(var t in e)m(o,t,{get:e[t],enumerable:!0})},N=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of B(e))!L.call(o,s)&&s!==t&&m(o,s,{get:()=>e[s],enumerable:!(i=k(e,s))||i.enumerable});return o};var $=o=>N(m({},"__esModule",{value:!0}),o);var H={};D(H,{InferJS:()=>y,attach:()=>U});var g={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},S={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},M={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},x=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||g.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:g.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,n)=>fetch(r,n)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},M);let i=e.debounceMs!==void 0?e.debounceMs:g.DEBOUNCE_MS,s=Math.max(i,g.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),s)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:t?null:this.state.stage}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let n=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];n&&(this.selectItem(n),this.updateState({selectedSuggestionIndex:-1}));return}}let s=t.value;if(e.key===" "&&this.shouldAutoInsertComma(s)){e.preventDefault();let r=`${s.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let s=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!s&&Object.keys(s).length>0;if(this.state.stage==="final"||r){let d=t;if(s&&Object.keys(s).length>0){let{street:p,street_number:a,city:h,addition:l}=s;if(p&&a&&h){let c=l?` ${l}`:"";d=`${p} ${a}${c}, ${h}`}}return this.finishSelection(d,s),!0}let n=typeof e!="string"?e.subtitle:null;return this.processSelection(i,n),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&S.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return S.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:s}=this.state,r=s;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(s),h=!a||!a.includes(t),l=a;if(a&&t){let c=a.match(S.STREET_NUMBER_PREFIX);if(c){let C=c[1];t.startsWith(C)&&(l="")}}h?r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `:r=l?`${l} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!s.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(s,e),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,h;let i=(e||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let s=(h=this.abortController)==null?void 0:h.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${g.API_URL}/infer/${this.country.toLowerCase()}`,n=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&n.append("country",this.country.toLowerCase()),this.authKey&&n.set("authKey",this.authKey);let d=r.includes("?")?"&":"?",p=`${r}${d}${n.toString()}`;this.fetcher(p,{signal:s}).then(l=>{if(!l.ok){if(t<this.maxRetries&&(l.status>=500||l.status===429))return this.retry(e,t,s);throw new Error("Network error")}return l.json()}).then(l=>{l&&this.mapResponseToState(l)}).catch(l=>{if(l.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,s);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let s=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},s)}mapResponseToState(e){var d,p,a,h;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],s=[],r=new Set;for(let l of i){let c=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;r.has(c)||(r.add(c),s.push(l))}let n=s.length+(((d=e.cities)==null?void 0:d.length)||0)+(((p=e.streets)==null?void 0:p.length)||0);t.hasMore=n>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((a=t.cities)==null?void 0:a.length)===0&&((h=t.streets)==null?void 0:h.length)===0?t.suggestions=s:t.suggestions=[]):(t.suggestions=s,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&s.length===1&&this.selectItem(s[0])}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(b(f({},M),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,s=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return s.cancel=()=>{i&&(clearTimeout(i),i=void 0)},s}};function A(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],i=o.toLowerCase(),s=e.toLowerCase(),r=0,n=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&i[a]===s[r]))continue;a>n&&t.push({text:o.slice(n,a),match:!1}),t.push({text:o[a],match:!0}),r++,n=a+1}return n<o.length&&t.push({text:o.slice(n),match:!1}),r===e.length?t:[{text:o,match:!1}]}var w=`
2
2
  .pro6pp-wrapper {
3
3
  position: relative;
4
4
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
@@ -208,13 +208,13 @@
208
208
  @keyframes pro6pp-spin {
209
209
  to { transform: rotate(360deg); }
210
210
  }
211
- `;var y=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadMoreText=t.loadMoreText||"Show more results...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let s=t.inputClass.split(" ");this.input.classList.add(...s)}let n=document.createElement("div");n.className="pro6pp-input-addons",this.wrapper.appendChild(n),this.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",n.appendChild(this.loader),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
211
+ `;var y=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadMoreText=t.loadMoreText||"Show more results...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let n=t.inputClass.split(" ");this.input.classList.add(...n)}let s=document.createElement("div");s.className="pro6pp-input-addons",this.wrapper.appendChild(s),this.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",s.appendChild(this.loader),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
212
212
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
213
213
  <line x1="18" y1="6" x2="6" y2="18"></line>
214
214
  <line x1="6" y1="6" x2="18" y2="18"></line>
215
215
  </svg>
216
- `,n.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.loadMoreButton=document.createElement("button"),this.loadMoreButton.type="button",this.loadMoreButton.className="pro6pp-load-more",this.loadMoreButton.textContent=this.loadMoreText,this.loadMoreButton.style.display="none",this.dropdown.appendChild(this.loadMoreButton),this.core=new x(b(c({},t),{onStateChange:s=>{this.render(s),t.onStateChange&&t.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(s)}})),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=`${e.street} ${e.street_number}, ${e.city}`;this.core.selectItem({label:t,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=S,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),this.loadMoreButton.addEventListener("click",e=>{e.preventDefault(),this.core.loadMore()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.loader.style.display=e.isLoading?"block":"none",this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,n=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||n))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",this.loadMoreButton.style.display=e.hasMore?"block":"none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Loading suggestions...",this.list.appendChild(s);return}if(n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}t.forEach((s,d)=>{if(!s.label)return;let l=document.createElement("li");l.className="pro6pp-item",d===e.selectedSuggestionIndex&&l.classList.add("pro6pp-item--active"),l.setAttribute("role","option"),l.setAttribute("aria-selected",d===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",M(s.label,e.query).forEach(({text:u,match:w})=>{if(w){let v=document.createElement("strong");v.className="pro6pp-item__label--match",v.textContent=u,a.appendChild(v)}else a.appendChild(document.createTextNode(u))}),l.appendChild(a);let p=s.count!==void 0&&s.count!==null?s.count:"",g=s.subtitle||p;if(g!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${g}`,l.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
216
+ `,s.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.loadMoreButton=document.createElement("button"),this.loadMoreButton.type="button",this.loadMoreButton.className="pro6pp-load-more",this.loadMoreButton.textContent=this.loadMoreText,this.loadMoreButton.style.display="none",this.dropdown.appendChild(this.loadMoreButton),this.core=new x(b(f({},t),{onStateChange:n=>{this.render(n),t.onStateChange&&t.onStateChange(n)},onSelect:n=>{typeof n=="string"?this.input.value=n:n&&typeof n=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(n)}})),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=`${e.street} ${e.street_number}, ${e.city}`;this.core.selectItem({label:t,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=w,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),this.loadMoreButton.addEventListener("click",e=>{e.preventDefault(),this.core.loadMore()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.loader.style.display=e.isLoading?"block":"none",this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,s=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||s))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",this.loadMoreButton.style.display=e.hasMore?"block":"none",e.isLoading&&!i){let n=document.createElement("li");n.className="pro6pp-no-results",n.textContent="Loading suggestions...",this.list.appendChild(n);return}if(s){let n=document.createElement("li");n.className="pro6pp-no-results",n.textContent=this.noResultsText,this.list.appendChild(n);return}t.forEach((n,d)=>{if(!n.label)return;let p=document.createElement("li");p.className="pro6pp-item",d===e.selectedSuggestionIndex&&p.classList.add("pro6pp-item--active"),p.setAttribute("role","option"),p.setAttribute("aria-selected",d===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",A(n.label,e.query).forEach(({text:u,match:E})=>{if(E){let v=document.createElement("strong");v.className="pro6pp-item__label--match",v.textContent=u,a.appendChild(v)}else a.appendChild(document.createTextNode(u))}),p.appendChild(a);let l=n.count!==void 0&&n.count!==null?n.count:"",c=n.subtitle||l;if(c!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${c}`,p.appendChild(u)}if(n.value===void 0||n.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
217
217
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
218
218
  <polyline points="9 18 15 12 9 6"></polyline>
219
219
  </svg>
220
- `,l.appendChild(u)}l.onmousedown=u=>u.preventDefault(),l.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(l)})}};function N(o,e){return new y(o,e)}return $(U);})();
220
+ `,p.appendChild(u)}p.onmousedown=u=>u.preventDefault(),p.onclick=u=>{u.stopPropagation(),this.core.selectItem(n)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(p)})}};function U(o,e){return new y(o,e)}return $(H);})();
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropertyDescriptors;var w=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,_=Object.prototype.propertyIsEnumerable;var C=(a,e,t)=>e in a?T(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t,c=(a,e)=>{for(var t in e||(e={}))k.call(e,t)&&C(a,t,e[t]);if(w)for(var t of w(e))_.call(e,t)&&C(a,t,e[t]);return a},m=(a,e)=>M(a,A(e));var f={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},I={DIGITS_1_3:/^[0-9]{1,3}$/},E={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},b=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||f.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:f.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((n,s)=>fetch(n,s)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=c({},E);let i=e.debounceMs!==void 0?e.debounceMs:f.DEBOUNCE_MS,r=Math.max(i,f.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(n=>this.executeFetch(n),r)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let n=this.state.selectedSuggestionIndex+1;n>=i&&(n=0),this.updateState({selectedSuggestionIndex:n});return}if(e.key==="ArrowUp"){e.preventDefault();let n=this.state.selectedSuggestionIndex-1;n<0&&(n=i-1),this.updateState({selectedSuggestionIndex:n});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let s=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];s&&(this.selectItem(s),this.updateState({selectedSuggestionIndex:-1}));return}}let r=t.value;if(e.key===" "&&this.shouldAutoInsertComma(r)){e.preventDefault();let n=`${r.trim()}, `;this.updateQueryAndFetch(n)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let r=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,n=!!r&&Object.keys(r).length>0;if(this.state.stage==="final"||n){let d=t;if(r&&Object.keys(r).length>0){let{street:l,street_number:o,city:h,addition:p}=r;if(l&&o&&h){let g=p?` ${p}`:"";d=`${l} ${o}${g}, ${h}`}}return this.finishSelection(d,r),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&I.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return I.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:r}=this.state,n=r;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")n=`${t}, ${e}, `;else{let o=this.getQueryPrefix(r);!o||!o.includes(t)?n=o?`${o} ${e}, ${t}, `:`${e}, ${t}, `:n=o?`${o} ${e}, `:`${e}, `}this.updateQueryAndFetch(n);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!r.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?n=`${e}, `:(n=this.replaceLastSegment(r,e),i!=="street_number"&&(n+=", ")),this.updateQueryAndFetch(n)}executeFetch(e,t=0){var o,h;let i=(e||"").toString();if(!i.trim()){(o=this.abortController)==null||o.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let r=(h=this.abortController)==null?void 0:h.signal,n=this.explicitApiUrl?this.explicitApiUrl:`${f.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey);let d=n.includes("?")?"&":"?",l=`${n}${d}${s.toString()}`;this.fetcher(l,{signal:r}).then(p=>{if(!p.ok){if(t<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(e,t,r);throw new Error("Network error")}return p.json()}).then(p=>{p&&this.mapResponseToState(p)}).catch(p=>{if(p.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,r);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let r=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},r)}mapResponseToState(e){var d,l;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],r=[],n=new Set;for(let o of i){let h=`${o.label}|${o.subtitle||""}|${JSON.stringify(o.value||{})}`;n.has(h)||(n.add(h),r.push(o))}let s=r.length+(((d=e.cities)==null?void 0:d.length)||0)+(((l=e.streets)==null?void 0:l.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],t.suggestions=[]):(t.suggestions=r,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&r.length===1&&this.selectItem(r[0])}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(m(c({},E),{query:this.state.query}))}updateState(e){this.state=c(c({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,r=(...n)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,n),t)};return r.cancel=()=>{i&&(clearTimeout(i),i=void 0)},r}};function L(a,e){if(!e||!a)return[{text:a,match:!1}];let t=[],i=a.toLowerCase(),r=e.toLowerCase(),n=0,s=0;for(let o=0;o<a.length;o++){if(!(n<e.length&&i[o]===r[n]))continue;o>s&&t.push({text:a.slice(s,o),match:!1}),t.push({text:a[o],match:!0}),n++,s=o+1}return s<a.length&&t.push({text:a.slice(s),match:!1}),n===e.length?t:[{text:a,match:!1}]}var y=`
1
+ var M=Object.defineProperty,A=Object.defineProperties;var _=Object.getOwnPropertyDescriptors;var E=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,R=Object.prototype.propertyIsEnumerable;var I=(l,e,t)=>e in l?M(l,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):l[e]=t,f=(l,e)=>{for(var t in e||(e={}))k.call(e,t)&&I(l,t,e[t]);if(E)for(var t of E(e))R.call(e,t)&&I(l,t,e[t]);return l},m=(l,e)=>A(l,_(e));var g={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},y={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},T={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},b=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||g.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:g.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,s)=>fetch(r,s)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},T);let i=e.debounceMs!==void 0?e.debounceMs:g.DEBOUNCE_MS,n=Math.max(i,g.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),n)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:t?null:this.state.stage}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let s=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];s&&(this.selectItem(s),this.updateState({selectedSuggestionIndex:-1}));return}}let n=t.value;if(e.key===" "&&this.shouldAutoInsertComma(n)){e.preventDefault();let r=`${n.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let n=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!n&&Object.keys(n).length>0;if(this.state.stage==="final"||r){let d=t;if(n&&Object.keys(n).length>0){let{street:p,street_number:o,city:h,addition:a}=n;if(p&&o&&h){let c=a?` ${a}`:"";d=`${p} ${o}${c}, ${h}`}}return this.finishSelection(d,n),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&y.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return y.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:n}=this.state,r=n;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let o=this.getQueryPrefix(n),h=!o||!o.includes(t),a=o;if(o&&t){let c=o.match(y.STREET_NUMBER_PREFIX);if(c){let w=c[1];t.startsWith(w)&&(a="")}}h?r=a?`${a} ${e}, ${t}, `:`${e}, ${t}, `:r=a?`${a} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!n.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(n,e),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var o,h;let i=(e||"").toString();if(!i.trim()){(o=this.abortController)==null||o.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let n=(h=this.abortController)==null?void 0:h.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${g.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey);let d=r.includes("?")?"&":"?",p=`${r}${d}${s.toString()}`;this.fetcher(p,{signal:n}).then(a=>{if(!a.ok){if(t<this.maxRetries&&(a.status>=500||a.status===429))return this.retry(e,t,n);throw new Error("Network error")}return a.json()}).then(a=>{a&&this.mapResponseToState(a)}).catch(a=>{if(a.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,n);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let n=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},n)}mapResponseToState(e){var d,p,o,h;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],n=[],r=new Set;for(let a of i){let c=`${a.label}|${a.subtitle||""}|${JSON.stringify(a.value||{})}`;r.has(c)||(r.add(c),n.push(a))}let s=n.length+(((d=e.cities)==null?void 0:d.length)||0)+(((p=e.streets)==null?void 0:p.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((o=t.cities)==null?void 0:o.length)===0&&((h=t.streets)==null?void 0:h.length)===0?t.suggestions=n:t.suggestions=[]):(t.suggestions=n,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&n.length===1&&this.selectItem(n[0])}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(m(f({},T),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,n=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return n.cancel=()=>{i&&(clearTimeout(i),i=void 0)},n}};function L(l,e){if(!e||!l)return[{text:l,match:!1}];let t=[],i=l.toLowerCase(),n=e.toLowerCase(),r=0,s=0;for(let o=0;o<l.length;o++){if(!(r<e.length&&i[o]===n[r]))continue;o>s&&t.push({text:l.slice(s,o),match:!1}),t.push({text:l[o],match:!0}),r++,s=o+1}return s<l.length&&t.push({text:l.slice(s),match:!1}),r===e.length?t:[{text:l,match:!1}]}var v=`
2
2
  .pro6pp-wrapper {
3
3
  position: relative;
4
4
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
@@ -208,13 +208,13 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
208
208
  @keyframes pro6pp-spin {
209
209
  to { transform: rotate(360deg); }
210
210
  }
211
- `;var v=class{constructor(e,t){this.isOpen=!1;var n;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadMoreText=t.loadMoreText||"Show more results...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(n=this.input.parentNode)==null||n.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let s=t.inputClass.split(" ");this.input.classList.add(...s)}let r=document.createElement("div");r.className="pro6pp-input-addons",this.wrapper.appendChild(r),this.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",r.appendChild(this.loader),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
211
+ `;var S=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadMoreText=t.loadMoreText||"Show more results...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let s=t.inputClass.split(" ");this.input.classList.add(...s)}let n=document.createElement("div");n.className="pro6pp-input-addons",this.wrapper.appendChild(n),this.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",n.appendChild(this.loader),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
212
212
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
213
213
  <line x1="18" y1="6" x2="6" y2="18"></line>
214
214
  <line x1="6" y1="6" x2="18" y2="18"></line>
215
215
  </svg>
216
- `,r.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.loadMoreButton=document.createElement("button"),this.loadMoreButton.type="button",this.loadMoreButton.className="pro6pp-load-more",this.loadMoreButton.textContent=this.loadMoreText,this.loadMoreButton.style.display="none",this.dropdown.appendChild(this.loadMoreButton),this.core=new b(m(c({},t),{onStateChange:s=>{this.render(s),t.onStateChange&&t.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(s)}})),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=`${e.street} ${e.street_number}, ${e.city}`;this.core.selectItem({label:t,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=y,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),this.loadMoreButton.addEventListener("click",e=>{e.preventDefault(),this.core.loadMore()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.loader.style.display=e.isLoading?"block":"none",this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,r=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||r))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",this.loadMoreButton.style.display=e.hasMore?"block":"none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Loading suggestions...",this.list.appendChild(s);return}if(r){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}t.forEach((s,d)=>{if(!s.label)return;let l=document.createElement("li");l.className="pro6pp-item",d===e.selectedSuggestionIndex&&l.classList.add("pro6pp-item--active"),l.setAttribute("role","option"),l.setAttribute("aria-selected",d===e.selectedSuggestionIndex?"true":"false");let o=document.createElement("span");o.className="pro6pp-item__label",L(s.label,e.query).forEach(({text:u,match:S})=>{if(S){let x=document.createElement("strong");x.className="pro6pp-item__label--match",x.textContent=u,o.appendChild(x)}else o.appendChild(document.createTextNode(u))}),l.appendChild(o);let p=s.count!==void 0&&s.count!==null?s.count:"",g=s.subtitle||p;if(g!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${g}`,l.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
216
+ `,n.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.loadMoreButton=document.createElement("button"),this.loadMoreButton.type="button",this.loadMoreButton.className="pro6pp-load-more",this.loadMoreButton.textContent=this.loadMoreText,this.loadMoreButton.style.display="none",this.dropdown.appendChild(this.loadMoreButton),this.core=new b(m(f({},t),{onStateChange:s=>{this.render(s),t.onStateChange&&t.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(s)}})),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=`${e.street} ${e.street_number}, ${e.city}`;this.core.selectItem({label:t,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=v,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),this.loadMoreButton.addEventListener("click",e=>{e.preventDefault(),this.core.loadMore()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.loader.style.display=e.isLoading?"block":"none",this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,n=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||n))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",this.loadMoreButton.style.display=e.hasMore?"block":"none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Loading suggestions...",this.list.appendChild(s);return}if(n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}t.forEach((s,d)=>{if(!s.label)return;let p=document.createElement("li");p.className="pro6pp-item",d===e.selectedSuggestionIndex&&p.classList.add("pro6pp-item--active"),p.setAttribute("role","option"),p.setAttribute("aria-selected",d===e.selectedSuggestionIndex?"true":"false");let o=document.createElement("span");o.className="pro6pp-item__label",L(s.label,e.query).forEach(({text:u,match:C})=>{if(C){let x=document.createElement("strong");x.className="pro6pp-item__label--match",x.textContent=u,o.appendChild(x)}else o.appendChild(document.createTextNode(u))}),p.appendChild(o);let a=s.count!==void 0&&s.count!==null?s.count:"",c=s.subtitle||a;if(c!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${c}`,p.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
217
217
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
218
218
  <polyline points="9 18 15 12 9 6"></polyline>
219
219
  </svg>
220
- `,l.appendChild(u)}l.onmousedown=u=>u.preventDefault(),l.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(l)})}};function Q(a,e){return new v(a,e)}export{v as InferJS,Q as attach};
220
+ `,p.appendChild(u)}p.onmousedown=u=>u.preventDefault(),p.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(p)})}};function Q(l,e){return new S(l,e)}export{S as InferJS,Q as attach};
package/package.json CHANGED
@@ -18,7 +18,7 @@
18
18
  "sideEffects": [
19
19
  "*.css"
20
20
  ],
21
- "version": "0.0.2-beta.13",
21
+ "version": "0.0.2-beta.14",
22
22
  "main": "./dist/index.js",
23
23
  "module": "./dist/index.mjs",
24
24
  "types": "./dist/index.d.ts",
@@ -43,7 +43,7 @@
43
43
  "test:coverage": "vitest run --coverage"
44
44
  },
45
45
  "dependencies": {
46
- "@pro6pp/infer-core": "0.0.2-beta.12"
46
+ "@pro6pp/infer-core": "0.0.2-beta.13"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@testing-library/dom": "^10.4.1",