@pro6pp/infer-js 0.1.0-beta.19 → 0.1.0-beta.20

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 y=Object.defineProperty,k=Object.defineProperties,$=Object.getOwnPropertyDescriptor,R=Object.getOwnPropertyDescriptors,F=Object.getOwnPropertyNames,L=Object.getOwnPropertySymbols;var T=Object.prototype.hasOwnProperty,H=Object.prototype.propertyIsEnumerable;var I=(o,e,t)=>e in o?y(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))T.call(e,t)&&I(o,t,e[t]);if(L)for(var t of L(e))H.call(e,t)&&I(o,t,e[t]);return o},m=(o,e)=>k(o,R(e));var N=(o,e)=>{for(var t in e)y(o,t,{get:e[t],enumerable:!0})},B=(o,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of F(e))!T.call(o,s)&&s!==t&&y(o,s,{get:()=>e[s],enumerable:!(n=$(e,s))||n.enumerable});return o};var O=o=>B(y({},"__esModule",{value:!0}),o);var K={};N(K,{InferJS:()=>v,attach:()=>q});module.exports=O(K);function _(o){return o.toLowerCase().trim()}function U(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function V(o,e){let t=_(o),n=_(e);if(n.includes(" "))return t.indexOf(n);let r=new RegExp(`(?:^|[,\\s])${U(n)}(?:$|[,\\s])`,"g").exec(t);if(r){let i=r.index,p=t[i];return p===","||p===" "?i+1:i}return-1}function P(o,e){let t=[],n=[];e.street&&n.push({value:e.street,type:"street"}),e.city&&n.push({value:e.city,type:"city"}),e.postcode&&n.push({value:e.postcode,type:"postcode"}),e.street_number!==void 0&&e.street_number!==null&&n.push({value:String(e.street_number),type:"street_number"}),e.addition&&n.push({value:e.addition,type:"addition"});for(let s of n){let r=V(o,s.value);r!==-1&&t.push({type:s.type,value:s.value,position:r})}return t.sort((s,r)=>s.position-r.position),t}function M(o,e){if(!e||!o)return"";let t=P(o,e),n=new Set(t.map(i=>i.type)),s=[];for(let i of t)s.push(i.value);let r=["street","street_number","addition","postcode","city"];for(let i of r){if(n.has(i))continue;let p;switch(i){case"street":p=e.street;break;case"city":p=e.city;break;case"street_number":p=e.street_number!==void 0?String(e.street_number):void 0;break;case"postcode":p=e.postcode;break;case"addition":p=e.addition;break}p&&s.push(p)}return s.join(", ")}var b={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},w={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},A={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||b.LIMIT,this.currentLimit=this.baseLimit,this.language=e.language;let t=e.maxRetries!==void 0?e.maxRetries:b.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,i)=>fetch(r,i)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},A);let n=e.debounceMs!==void 0?e.debounceMs:b.DEBOUNCE_MS,s=Math.max(n,b.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 n=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(n>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=n&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=n-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let i=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];i&&(this.selectItem(i),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,n=t;typeof e!="string"&&typeof e.value=="string"&&(n=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 p=t;if(s&&Object.keys(s).length>0){let{street:u,street_number:a,postcode:c,city:l,addition:h}=s;if(u&&a&&l){let g=h?` ${h}`:"",d=c?`${c}, `:"";p=`${u}, ${a}${g}, ${d}${l}`}}return this.finishSelection(p,s),!0}let i=typeof e!="string"?e.subtitle:null;return this.processSelection(n,i),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&w.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let n=this.getCurrentFragment(e);return w.DIGITS_1_3.test(n)}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:n,query:s}=this.state,r=s;if(t&&(n==="city"||n==="street"||n==="mixed")){if(n==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(s),c=!a||!a.includes(t),l=a;if(a&&t){let h=a.match(w.STREET_NUMBER_PREFIX);if(h){let g=h[1];t.startsWith(g)&&(l="")}}c?r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `:r=l?`${l} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(n==="direct"||n==="addition"){this.finishSelection(e);return}!s.includes(",")&&(n==="city"||n==="street"||n==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(s,e),n!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,c;let n=(e||"").toString();if(!n.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=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${b.API_URL}/infer/${this.country.toLowerCase()}`,i=new URLSearchParams({query:n,limit:this.currentLimit.toString()});this.explicitApiUrl&&i.append("country",this.country.toLowerCase()),this.authKey&&i.set("authKey",this.authKey),this.language&&i.set("language",this.language);let p=r.includes("?")?"&":"?",u=`${r}${p}${i.toString()}`;this.fetcher(u,{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,n){if(n!=null&&n.aborted)return;let s=Math.pow(2,t)*200;setTimeout(()=>{n!=null&&n.aborted||this.executeFetch(e,t+1)},s)}mapResponseToState(e){var p,u,a,c;let t={stage:e.stage,isLoading:!1},n=e.suggestions||[],s=[],r=new Set;for(let l of n){let h=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;if(!r.has(h)){r.add(h);let g=this.reformatSuggestionLabel(l);s.push(g)}}let i=s.length+(((p=e.cities)==null?void 0:p.length)||0)+(((u=e.streets)==null?void 0:u.length)||0);t.hasMore=i>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((a=t.cities)==null?void 0:a.length)===0&&((c=t.streets)==null?void 0:c.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])}reformatSuggestionLabel(e){if(!e.value||typeof e.value=="string")return e;let t=e.value;if(!t.street||!t.city)return e;let n=M(this.state.query,t);return n?m(f({},e),{label:n}):e}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let n=e.lastIndexOf(",");return n===-1?t:`${e.slice(0,n+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({},A),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let n,s=(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>e.apply(this,r),t)};return s.cancel=()=>{n&&(clearTimeout(n),n=void 0)},s}};function z(o){if(o.length===0)return o;let e=[];for(let t of o){let n=e[e.length-1];n&&n.match===t.match?n.text+=t.text:e.push({text:t.text,match:t.match})}return e}function D(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],n=o.toLowerCase(),s=e.toLowerCase(),r=0,i=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&n[a]===s[r]))continue;a>i&&t.push({text:o.slice(i,a),match:!1}),t.push({text:o[a],match:!0}),r++,i=a+1}return i<o.length&&t.push({text:o.slice(i),match:!1}),r===e.length?z(t):[{text:o,match:!1}]}var C=`
1
+ "use strict";var y=Object.defineProperty,H=Object.defineProperties,$=Object.getOwnPropertyDescriptor,F=Object.getOwnPropertyDescriptors,R=Object.getOwnPropertyNames,L=Object.getOwnPropertySymbols;var D=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var T=(o,e,t)=>e in o?y(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))D.call(e,t)&&T(o,t,e[t]);if(L)for(var t of L(e))N.call(e,t)&&T(o,t,e[t]);return o},m=(o,e)=>H(o,F(e));var B=(o,e)=>{for(var t in e)y(o,t,{get:e[t],enumerable:!0})},O=(o,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of R(e))!D.call(o,i)&&i!==t&&y(o,i,{get:()=>e[i],enumerable:!(n=$(e,i))||n.enumerable});return o};var U=o=>O(y({},"__esModule",{value:!0}),o);var J={};B(J,{InferJS:()=>S,attach:()=>Q});module.exports=U(J);function M(o){return o.toLowerCase().trim()}function V(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function P(o,e){let t=M(o),n=M(e);if(n.includes(" "))return t.indexOf(n);let r=new RegExp(`(?:^|[,\\s])${V(n)}(?:$|[,\\s])`,"g").exec(t);if(r){let s=r.index,a=t[s];return a===","||a===" "?s+1:s}return-1}function z(o,e){let t=[],n=[];e.street&&n.push({value:e.street,type:"street"}),e.city&&n.push({value:e.city,type:"city"}),e.postcode&&n.push({value:e.postcode,type:"postcode"}),e.street_number!==void 0&&e.street_number!==null&&n.push({value:String(e.street_number),type:"street_number"}),e.addition&&n.push({value:e.addition,type:"addition"});for(let i of n){let r=P(o,i.value);r!==-1&&t.push({type:i.type,value:i.value,position:r})}return t.sort((i,r)=>i.position-r.position),t}function _(o,e){if(!e||!o)return"";let t=z(o,e),n=new Set(t.map(s=>s.type)),i=[];for(let s of t)i.push(s.value);let r=["street","street_number","addition","postcode","city"];for(let s of r){if(n.has(s))continue;let a;switch(s){case"street":a=e.street;break;case"city":a=e.city;break;case"street_number":a=e.street_number!==void 0?String(e.street_number):void 0;break;case"postcode":a=e.postcode;break;case"addition":a=e.addition;break}a&&i.push(a)}return i.join(", ")}var b={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},C={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},A={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},v=class{constructor(e){this.abortController=null;this.isDestroyed=!1;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||b.LIMIT,this.currentLimit=this.baseLimit,this.language=e.language;let t=e.maxRetries!==void 0?e.maxRetries:b.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({},A);let n=e.debounceMs!==void 0?e.debounceMs:b.DEBOUNCE_MS,i=Math.max(n,b.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),i)}handleInput(e){if(this.isDestroyed)return;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.isDestroyed||this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){if(this.isDestroyed)return;let t=e.target;if(!t)return;let n=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(n>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=n&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=n-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 i=t.value;if(e.key===" "&&this.shouldAutoInsertComma(i)){e.preventDefault();let r=`${i.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){if(this.isDestroyed)return!1;this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,n=t;typeof e!="string"&&typeof e.value=="string"&&(n=e.value);let i=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!i&&Object.keys(i).length>0;if(this.state.stage==="final"||r){let a=t;if(i&&Object.keys(i).length>0){let{street:d,street_number:l,postcode:c,city:p,addition:h}=i;if(d&&l&&p){let g=h?` ${h}`:"",u=c?`${c}, `:"";a=`${d}, ${l}${g}, ${u}${p}`}}return this.finishSelection(a,i),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(n,s),!1}destroy(){var e;this.isDestroyed||(this.isDestroyed=!0,this.debouncedFetch.cancel(),(e=this.abortController)==null||e.abort(),this.abortController=null)}shouldAutoInsertComma(e){if(!e.includes(",")&&C.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let n=this.getCurrentFragment(e);return C.DIGITS_1_3.test(n)}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:n,query:i}=this.state,r=i;if(t&&(n==="city"||n==="street"||n==="mixed")){if(n==="city")r=`${t}, ${e}, `;else{let l=this.getQueryPrefix(i),c=!l||!l.includes(t),p=l;if(l&&t){let h=l.match(C.STREET_NUMBER_PREFIX);if(h){let g=h[1];t.startsWith(g)&&(p="")}}c?r=p?`${p} ${e}, ${t}, `:`${e}, ${t}, `:r=p?`${p} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(n==="direct"||n==="addition"){this.finishSelection(e);return}!i.includes(",")&&(n==="city"||n==="street"||n==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(i,e),n!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var l,c;if(this.isDestroyed)return;let n=(e||"").toString();if(!n.trim()){(l=this.abortController)==null||l.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let i=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${b.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:n,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey),this.language&&s.set("language",this.language);let a=r.includes("?")?"&":"?",d=`${r}${a}${s.toString()}`;this.fetcher(d,{signal:i}).then(p=>{if(!p.ok){if(t<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(e,t,i);throw new Error("Network error")}return p.json()}).then(p=>{this.isDestroyed||i!=null&&i.aborted||p&&this.mapResponseToState(p)}).catch(p=>{if(!this.isDestroyed&&p.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,i);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,n){if(this.isDestroyed||n!=null&&n.aborted)return;let i=Math.pow(2,t)*200;setTimeout(()=>{!this.isDestroyed&&!(n!=null&&n.aborted)&&this.executeFetch(e,t+1)},i)}mapResponseToState(e){var a,d,l,c;let t={stage:e.stage,isLoading:!1},n=e.suggestions||[],i=[],r=new Set;for(let p of n){let h=`${p.label}|${p.subtitle||""}|${JSON.stringify(p.value||{})}`;if(!r.has(h)){r.add(h);let g=this.reformatSuggestionLabel(p);i.push(g)}}let s=i.length+(((a=e.cities)==null?void 0:a.length)||0)+(((d=e.streets)==null?void 0:d.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((l=t.cities)==null?void 0:l.length)===0&&((c=t.streets)==null?void 0:c.length)===0?t.suggestions=i:t.suggestions=[]):(t.suggestions=i,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&i.length===1&&this.selectItem(i[0])}reformatSuggestionLabel(e){if(!e.value||typeof e.value=="string")return e;let t=e.value;if(!t.street||!t.city)return e;let n=_(this.state.query,t);return n?m(f({},e),{label:n}):e}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let n=e.lastIndexOf(",");return n===-1?t:`${e.slice(0,n+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({},A),{query:this.state.query}))}updateState(e){this.isDestroyed||(this.state=f(f({},this.state),e),this.onStateChange(this.state))}debounce(e,t){let n,i=(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>e.apply(this,r),t)};return i.cancel=()=>{n&&(clearTimeout(n),n=void 0)},i}};function q(o){if(o.length===0)return o;let e=[];for(let t of o){let n=e[e.length-1];n&&n.match===t.match?n.text+=t.text:e.push({text:t.text,match:t.match})}return e}function k(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],n=o.toLowerCase(),i=e.toLowerCase(),r=0,s=0;for(let l=0;l<o.length;l++){if(!(r<e.length&&n[l]===i[r]))continue;l>s&&t.push({text:o.slice(s,l),match:!1}),t.push({text:o[l],match:!0}),r++,s=l+1}return s<o.length&&t.push({text:o.slice(s),match:!1}),r===e.length?q(t):[{text:o,match:!1}]}var E=`
2
2
  .pro6pp-wrapper {
3
3
  position: relative;
4
4
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
@@ -201,16 +201,16 @@
201
201
  @keyframes pro6pp-spin {
202
202
  to { transform: rotate(360deg); }
203
203
  }
204
- `;var v=class{constructor(e,t){this.isOpen=!1;this.boundHandlers=null;this.isDestroyed=!1;var r;let n=typeof e=="string"?document.querySelector(e):e;if(!n)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",n instanceof HTMLInputElement?(this.input=n,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(n.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.input.classList.add("pro6pp-input"),t.inputClass){let i=t.inputClass.split(" ");this.input.classList.add(...i)}let s=document.createElement("div");s.className="pro6pp-input-addons",this.wrapper.appendChild(s),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=`
204
+ `;var x=new WeakMap;function K(o){return x.get(o)}function j(o,...e){e.forEach(t=>x.set(t,o))}var S=class{constructor(e,t){this.isOpen=!1;this.boundHandlers=null;this.isDestroyed=!1;var s;let n=typeof e=="string"?document.querySelector(e):e;if(!n)throw new Error("InferJS: Target element not found.");let i=K(n);if(i&&i.destroy(),this.targetElement=n,this.ownsInput=!(n instanceof HTMLInputElement),this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",n instanceof HTMLInputElement?(this.input=n,(s=this.input.parentNode)==null||s.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(n.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.input.classList.add("pro6pp-input"),t.inputClass){let a=t.inputClass.split(" ");this.input.classList.add(...a)}let r=document.createElement("div");r.className="pro6pp-input-addons",this.wrapper.appendChild(r),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=`
205
205
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
206
206
  <line x1="18" y1="6" x2="6" y2="18"></line>
207
207
  <line x1="6" y1="6" x2="18" y2="18"></line>
208
208
  </svg>
209
- `,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.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
209
+ `,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.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
210
210
  <div class="pro6pp-mini-spinner"></div>
211
211
  <span>${this.loadingText}</span>
212
- `,this.dropdown.appendChild(this.dropdownLoader),this.core=new x(m(f({},t),{onStateChange:i=>{this.render(i),t.onStateChange&&t.onStateChange(i)},onSelect:i=>{typeof i=="string"?this.input.value=i:i&&typeof i=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(i)}})),this.observer=new IntersectionObserver(i=>{i[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=e.addition?` ${e.addition}`:"",n=e.postcode?`${e.postcode}, `:"",s=`${e.street}, ${e.street_number}${t}, ${n}${e.city}`;this.core.selectItem({label:s,value:e})}destroy(){this.isDestroyed||(this.isDestroyed=!0,this.boundHandlers&&(this.input.removeEventListener("input",this.boundHandlers.onInput),this.input.removeEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.removeEventListener("click",this.boundHandlers.onClearClick),document.removeEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.removeEventListener("focus",this.boundHandlers.onFocus),this.boundHandlers=null),this.observer.disconnect(),this.wrapper.parentNode&&(this.wrapper.parentNode.insertBefore(this.input,this.wrapper),this.wrapper.remove()),this.input.classList.remove("pro6pp-input"),this.input.value="")}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=C,document.head.appendChild(t)}}bindEvents(){this.boundHandlers={onInput:e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)},onKeyDown:e=>{this.core.handleKeyDown(e)},onClearClick:()=>{this.core.handleInput(""),this.input.focus()},onDocumentMouseDown:e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")},onFocus:()=>{this.isOpen=!0,this.render(this.core.state)}},this.input.addEventListener("input",this.boundHandlers.onInput),this.input.addEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.addEventListener("click",this.boundHandlers.onClearClick),document.addEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.addEventListener("focus",this.boundHandlers.onFocus)}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],n=t.length>0,s=!e.isLoading&&!e.isError&&e.query.length>0&&!n&&!e.isValid;if(!(this.isOpen&&(n||e.isLoading||s))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&n?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!n){let i=document.createElement("li");i.className="pro6pp-no-results",i.textContent="Searching...",this.list.appendChild(i),this.dropdownLoader.style.display="none";return}if(s){let i=document.createElement("li");i.className="pro6pp-no-results",i.textContent=this.noResultsText,this.list.appendChild(i);return}if(t.forEach((i,p)=>{if(!i.label)return;let u=document.createElement("li");u.className="pro6pp-item",p===e.selectedSuggestionIndex&&u.classList.add("pro6pp-item--active"),u.setAttribute("role","option"),u.setAttribute("aria-selected",p===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",D(i.label,e.query).forEach(({text:d,match:E})=>{let S=document.createElement("span");S.className=E?"pro6pp-item__label--match":"pro6pp-item__label--unmatched",S.textContent=d,a.appendChild(S)}),u.appendChild(a);let l=i.count!==void 0&&i.count!==null?i.count:"",h=i.subtitle||l;if(h!==""){let d=document.createElement("span");d.className="pro6pp-item__subtitle",d.textContent=`, ${h}`,u.appendChild(d)}if(i.value===void 0||i.value===null){let d=document.createElement("div");d.className="pro6pp-item__chevron",d.innerHTML=`
212
+ `,this.dropdown.appendChild(this.dropdownLoader),this.core=new v(m(f({},t),{onStateChange:a=>{this.render(a),t.onStateChange&&t.onStateChange(a)},onSelect:a=>{typeof a=="string"?this.input.value=a:a&&typeof a=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(a)}})),this.observer=new IntersectionObserver(a=>{a[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents(),j(this,this.targetElement,this.input)}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=e.addition?` ${e.addition}`:"",n=e.postcode?`${e.postcode}, `:"",i=`${e.street}, ${e.street_number}${t}, ${n}${e.city}`;this.core.selectItem({label:i,value:e})}destroy(){this.isDestroyed||(this.isDestroyed=!0,this.core.destroy(),this.boundHandlers&&(this.input.removeEventListener("input",this.boundHandlers.onInput),this.input.removeEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.removeEventListener("click",this.boundHandlers.onClearClick),document.removeEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.removeEventListener("focus",this.boundHandlers.onFocus),this.boundHandlers=null),this.observer.disconnect(),x.delete(this.targetElement),x.delete(this.input),this.wrapper.parentNode&&(this.ownsInput||this.wrapper.parentNode.insertBefore(this.input,this.wrapper),this.wrapper.remove()),this.input.classList.remove("pro6pp-input"),this.input.value="")}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=E,document.head.appendChild(t)}}bindEvents(){this.boundHandlers={onInput:e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)},onKeyDown:e=>{this.core.handleKeyDown(e)},onClearClick:()=>{this.core.handleInput(""),this.input.focus()},onDocumentMouseDown:e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")},onFocus:()=>{this.isOpen=!0,this.render(this.core.state)}},this.input.addEventListener("input",this.boundHandlers.onInput),this.input.addEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.addEventListener("click",this.boundHandlers.onClearClick),document.addEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.addEventListener("focus",this.boundHandlers.onFocus)}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],n=t.length>0,i=!e.isLoading&&!e.isError&&e.query.length>0&&!n&&!e.isValid;if(!(this.isOpen&&(n||e.isLoading||i))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&n?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Searching...",this.list.appendChild(s),this.dropdownLoader.style.display="none";return}if(i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}if(t.forEach((s,a)=>{if(!s.label)return;let d=document.createElement("li");d.className="pro6pp-item",a===e.selectedSuggestionIndex&&d.classList.add("pro6pp-item--active"),d.setAttribute("role","option"),d.setAttribute("aria-selected",a===e.selectedSuggestionIndex?"true":"false");let l=document.createElement("span");l.className="pro6pp-item__label",k(s.label,e.query).forEach(({text:u,match:I})=>{let w=document.createElement("span");w.className=I?"pro6pp-item__label--match":"pro6pp-item__label--unmatched",w.textContent=u,l.appendChild(w)}),d.appendChild(l);let p=s.count!==void 0&&s.count!==null?s.count:"",h=s.subtitle||p;if(h!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${h}`,d.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
213
213
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
214
214
  <polyline points="9 18 15 12 9 6"></polyline>
215
215
  </svg>
216
- `,u.appendChild(d)}u.onmousedown=d=>d.preventDefault(),u.onclick=d=>{d.stopPropagation(),this.core.selectItem(i)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(u)}),e.hasMore&&!e.isLoading){let i=document.createElement("li");i.style.height="1px",i.style.opacity="0",this.list.appendChild(i),this.observer.observe(i)}}};function q(o,e){return new v(o,e)}0&&(module.exports={InferJS,attach});
216
+ `,d.appendChild(u)}d.onmousedown=u=>u.preventDefault(),d.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(d)}),e.hasMore&&!e.isLoading){let s=document.createElement("li");s.style.height="1px",s.style.opacity="0",this.list.appendChild(s),this.observer.observe(s)}}};function Q(o,e){return new S(o,e)}0&&(module.exports={InferJS,attach});
package/dist/index.d.mts CHANGED
@@ -53,6 +53,8 @@ declare class InferJS {
53
53
  private showClearButton;
54
54
  private isOpen;
55
55
  private observer;
56
+ private targetElement;
57
+ private ownsInput;
56
58
  private boundHandlers;
57
59
  private isDestroyed;
58
60
  /**
package/dist/index.d.ts CHANGED
@@ -53,6 +53,8 @@ declare class InferJS {
53
53
  private showClearButton;
54
54
  private isOpen;
55
55
  private observer;
56
+ private targetElement;
57
+ private ownsInput;
56
58
  private boundHandlers;
57
59
  private isDestroyed;
58
60
  /**
@@ -1,4 +1,4 @@
1
- "use strict";var Pro6PP=(()=>{var y=Object.defineProperty,k=Object.defineProperties,$=Object.getOwnPropertyDescriptor,R=Object.getOwnPropertyDescriptors,F=Object.getOwnPropertyNames,L=Object.getOwnPropertySymbols;var T=Object.prototype.hasOwnProperty,H=Object.prototype.propertyIsEnumerable;var I=(o,e,t)=>e in o?y(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))T.call(e,t)&&I(o,t,e[t]);if(L)for(var t of L(e))H.call(e,t)&&I(o,t,e[t]);return o},m=(o,e)=>k(o,R(e));var N=(o,e)=>{for(var t in e)y(o,t,{get:e[t],enumerable:!0})},B=(o,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of F(e))!T.call(o,s)&&s!==t&&y(o,s,{get:()=>e[s],enumerable:!(n=$(e,s))||n.enumerable});return o};var O=o=>B(y({},"__esModule",{value:!0}),o);var K={};N(K,{InferJS:()=>v,attach:()=>q});function _(o){return o.toLowerCase().trim()}function U(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function V(o,e){let t=_(o),n=_(e);if(n.includes(" "))return t.indexOf(n);let r=new RegExp(`(?:^|[,\\s])${U(n)}(?:$|[,\\s])`,"g").exec(t);if(r){let i=r.index,p=t[i];return p===","||p===" "?i+1:i}return-1}function P(o,e){let t=[],n=[];e.street&&n.push({value:e.street,type:"street"}),e.city&&n.push({value:e.city,type:"city"}),e.postcode&&n.push({value:e.postcode,type:"postcode"}),e.street_number!==void 0&&e.street_number!==null&&n.push({value:String(e.street_number),type:"street_number"}),e.addition&&n.push({value:e.addition,type:"addition"});for(let s of n){let r=V(o,s.value);r!==-1&&t.push({type:s.type,value:s.value,position:r})}return t.sort((s,r)=>s.position-r.position),t}function M(o,e){if(!e||!o)return"";let t=P(o,e),n=new Set(t.map(i=>i.type)),s=[];for(let i of t)s.push(i.value);let r=["street","street_number","addition","postcode","city"];for(let i of r){if(n.has(i))continue;let p;switch(i){case"street":p=e.street;break;case"city":p=e.city;break;case"street_number":p=e.street_number!==void 0?String(e.street_number):void 0;break;case"postcode":p=e.postcode;break;case"addition":p=e.addition;break}p&&s.push(p)}return s.join(", ")}var b={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},w={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},A={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||b.LIMIT,this.currentLimit=this.baseLimit,this.language=e.language;let t=e.maxRetries!==void 0?e.maxRetries:b.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,i)=>fetch(r,i)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},A);let n=e.debounceMs!==void 0?e.debounceMs:b.DEBOUNCE_MS,s=Math.max(n,b.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 n=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(n>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=n&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=n-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let i=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];i&&(this.selectItem(i),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,n=t;typeof e!="string"&&typeof e.value=="string"&&(n=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 p=t;if(s&&Object.keys(s).length>0){let{street:u,street_number:a,postcode:c,city:l,addition:h}=s;if(u&&a&&l){let g=h?` ${h}`:"",d=c?`${c}, `:"";p=`${u}, ${a}${g}, ${d}${l}`}}return this.finishSelection(p,s),!0}let i=typeof e!="string"?e.subtitle:null;return this.processSelection(n,i),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&w.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let n=this.getCurrentFragment(e);return w.DIGITS_1_3.test(n)}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:n,query:s}=this.state,r=s;if(t&&(n==="city"||n==="street"||n==="mixed")){if(n==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(s),c=!a||!a.includes(t),l=a;if(a&&t){let h=a.match(w.STREET_NUMBER_PREFIX);if(h){let g=h[1];t.startsWith(g)&&(l="")}}c?r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `:r=l?`${l} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(n==="direct"||n==="addition"){this.finishSelection(e);return}!s.includes(",")&&(n==="city"||n==="street"||n==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(s,e),n!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,c;let n=(e||"").toString();if(!n.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=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${b.API_URL}/infer/${this.country.toLowerCase()}`,i=new URLSearchParams({query:n,limit:this.currentLimit.toString()});this.explicitApiUrl&&i.append("country",this.country.toLowerCase()),this.authKey&&i.set("authKey",this.authKey),this.language&&i.set("language",this.language);let p=r.includes("?")?"&":"?",u=`${r}${p}${i.toString()}`;this.fetcher(u,{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,n){if(n!=null&&n.aborted)return;let s=Math.pow(2,t)*200;setTimeout(()=>{n!=null&&n.aborted||this.executeFetch(e,t+1)},s)}mapResponseToState(e){var p,u,a,c;let t={stage:e.stage,isLoading:!1},n=e.suggestions||[],s=[],r=new Set;for(let l of n){let h=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;if(!r.has(h)){r.add(h);let g=this.reformatSuggestionLabel(l);s.push(g)}}let i=s.length+(((p=e.cities)==null?void 0:p.length)||0)+(((u=e.streets)==null?void 0:u.length)||0);t.hasMore=i>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((a=t.cities)==null?void 0:a.length)===0&&((c=t.streets)==null?void 0:c.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])}reformatSuggestionLabel(e){if(!e.value||typeof e.value=="string")return e;let t=e.value;if(!t.street||!t.city)return e;let n=M(this.state.query,t);return n?m(f({},e),{label:n}):e}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let n=e.lastIndexOf(",");return n===-1?t:`${e.slice(0,n+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({},A),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let n,s=(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>e.apply(this,r),t)};return s.cancel=()=>{n&&(clearTimeout(n),n=void 0)},s}};function z(o){if(o.length===0)return o;let e=[];for(let t of o){let n=e[e.length-1];n&&n.match===t.match?n.text+=t.text:e.push({text:t.text,match:t.match})}return e}function D(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],n=o.toLowerCase(),s=e.toLowerCase(),r=0,i=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&n[a]===s[r]))continue;a>i&&t.push({text:o.slice(i,a),match:!1}),t.push({text:o[a],match:!0}),r++,i=a+1}return i<o.length&&t.push({text:o.slice(i),match:!1}),r===e.length?z(t):[{text:o,match:!1}]}var C=`
1
+ "use strict";var Pro6PP=(()=>{var y=Object.defineProperty,H=Object.defineProperties,$=Object.getOwnPropertyDescriptor,F=Object.getOwnPropertyDescriptors,R=Object.getOwnPropertyNames,L=Object.getOwnPropertySymbols;var D=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var T=(o,e,t)=>e in o?y(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))D.call(e,t)&&T(o,t,e[t]);if(L)for(var t of L(e))N.call(e,t)&&T(o,t,e[t]);return o},m=(o,e)=>H(o,F(e));var B=(o,e)=>{for(var t in e)y(o,t,{get:e[t],enumerable:!0})},O=(o,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of R(e))!D.call(o,i)&&i!==t&&y(o,i,{get:()=>e[i],enumerable:!(n=$(e,i))||n.enumerable});return o};var U=o=>O(y({},"__esModule",{value:!0}),o);var J={};B(J,{InferJS:()=>S,attach:()=>Q});function M(o){return o.toLowerCase().trim()}function V(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function P(o,e){let t=M(o),n=M(e);if(n.includes(" "))return t.indexOf(n);let r=new RegExp(`(?:^|[,\\s])${V(n)}(?:$|[,\\s])`,"g").exec(t);if(r){let s=r.index,a=t[s];return a===","||a===" "?s+1:s}return-1}function z(o,e){let t=[],n=[];e.street&&n.push({value:e.street,type:"street"}),e.city&&n.push({value:e.city,type:"city"}),e.postcode&&n.push({value:e.postcode,type:"postcode"}),e.street_number!==void 0&&e.street_number!==null&&n.push({value:String(e.street_number),type:"street_number"}),e.addition&&n.push({value:e.addition,type:"addition"});for(let i of n){let r=P(o,i.value);r!==-1&&t.push({type:i.type,value:i.value,position:r})}return t.sort((i,r)=>i.position-r.position),t}function _(o,e){if(!e||!o)return"";let t=z(o,e),n=new Set(t.map(s=>s.type)),i=[];for(let s of t)i.push(s.value);let r=["street","street_number","addition","postcode","city"];for(let s of r){if(n.has(s))continue;let a;switch(s){case"street":a=e.street;break;case"city":a=e.city;break;case"street_number":a=e.street_number!==void 0?String(e.street_number):void 0;break;case"postcode":a=e.postcode;break;case"addition":a=e.addition;break}a&&i.push(a)}return i.join(", ")}var b={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},C={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},A={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},v=class{constructor(e){this.abortController=null;this.isDestroyed=!1;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||b.LIMIT,this.currentLimit=this.baseLimit,this.language=e.language;let t=e.maxRetries!==void 0?e.maxRetries:b.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({},A);let n=e.debounceMs!==void 0?e.debounceMs:b.DEBOUNCE_MS,i=Math.max(n,b.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),i)}handleInput(e){if(this.isDestroyed)return;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.isDestroyed||this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){if(this.isDestroyed)return;let t=e.target;if(!t)return;let n=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(n>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=n&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=n-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 i=t.value;if(e.key===" "&&this.shouldAutoInsertComma(i)){e.preventDefault();let r=`${i.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){if(this.isDestroyed)return!1;this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,n=t;typeof e!="string"&&typeof e.value=="string"&&(n=e.value);let i=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!i&&Object.keys(i).length>0;if(this.state.stage==="final"||r){let a=t;if(i&&Object.keys(i).length>0){let{street:d,street_number:l,postcode:c,city:p,addition:h}=i;if(d&&l&&p){let g=h?` ${h}`:"",u=c?`${c}, `:"";a=`${d}, ${l}${g}, ${u}${p}`}}return this.finishSelection(a,i),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(n,s),!1}destroy(){var e;this.isDestroyed||(this.isDestroyed=!0,this.debouncedFetch.cancel(),(e=this.abortController)==null||e.abort(),this.abortController=null)}shouldAutoInsertComma(e){if(!e.includes(",")&&C.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let n=this.getCurrentFragment(e);return C.DIGITS_1_3.test(n)}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:n,query:i}=this.state,r=i;if(t&&(n==="city"||n==="street"||n==="mixed")){if(n==="city")r=`${t}, ${e}, `;else{let l=this.getQueryPrefix(i),c=!l||!l.includes(t),p=l;if(l&&t){let h=l.match(C.STREET_NUMBER_PREFIX);if(h){let g=h[1];t.startsWith(g)&&(p="")}}c?r=p?`${p} ${e}, ${t}, `:`${e}, ${t}, `:r=p?`${p} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(n==="direct"||n==="addition"){this.finishSelection(e);return}!i.includes(",")&&(n==="city"||n==="street"||n==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(i,e),n!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var l,c;if(this.isDestroyed)return;let n=(e||"").toString();if(!n.trim()){(l=this.abortController)==null||l.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let i=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${b.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:n,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey),this.language&&s.set("language",this.language);let a=r.includes("?")?"&":"?",d=`${r}${a}${s.toString()}`;this.fetcher(d,{signal:i}).then(p=>{if(!p.ok){if(t<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(e,t,i);throw new Error("Network error")}return p.json()}).then(p=>{this.isDestroyed||i!=null&&i.aborted||p&&this.mapResponseToState(p)}).catch(p=>{if(!this.isDestroyed&&p.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,i);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,n){if(this.isDestroyed||n!=null&&n.aborted)return;let i=Math.pow(2,t)*200;setTimeout(()=>{!this.isDestroyed&&!(n!=null&&n.aborted)&&this.executeFetch(e,t+1)},i)}mapResponseToState(e){var a,d,l,c;let t={stage:e.stage,isLoading:!1},n=e.suggestions||[],i=[],r=new Set;for(let p of n){let h=`${p.label}|${p.subtitle||""}|${JSON.stringify(p.value||{})}`;if(!r.has(h)){r.add(h);let g=this.reformatSuggestionLabel(p);i.push(g)}}let s=i.length+(((a=e.cities)==null?void 0:a.length)||0)+(((d=e.streets)==null?void 0:d.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((l=t.cities)==null?void 0:l.length)===0&&((c=t.streets)==null?void 0:c.length)===0?t.suggestions=i:t.suggestions=[]):(t.suggestions=i,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&i.length===1&&this.selectItem(i[0])}reformatSuggestionLabel(e){if(!e.value||typeof e.value=="string")return e;let t=e.value;if(!t.street||!t.city)return e;let n=_(this.state.query,t);return n?m(f({},e),{label:n}):e}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let n=e.lastIndexOf(",");return n===-1?t:`${e.slice(0,n+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({},A),{query:this.state.query}))}updateState(e){this.isDestroyed||(this.state=f(f({},this.state),e),this.onStateChange(this.state))}debounce(e,t){let n,i=(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>e.apply(this,r),t)};return i.cancel=()=>{n&&(clearTimeout(n),n=void 0)},i}};function q(o){if(o.length===0)return o;let e=[];for(let t of o){let n=e[e.length-1];n&&n.match===t.match?n.text+=t.text:e.push({text:t.text,match:t.match})}return e}function k(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],n=o.toLowerCase(),i=e.toLowerCase(),r=0,s=0;for(let l=0;l<o.length;l++){if(!(r<e.length&&n[l]===i[r]))continue;l>s&&t.push({text:o.slice(s,l),match:!1}),t.push({text:o[l],match:!0}),r++,s=l+1}return s<o.length&&t.push({text:o.slice(s),match:!1}),r===e.length?q(t):[{text:o,match:!1}]}var E=`
2
2
  .pro6pp-wrapper {
3
3
  position: relative;
4
4
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
@@ -201,16 +201,16 @@
201
201
  @keyframes pro6pp-spin {
202
202
  to { transform: rotate(360deg); }
203
203
  }
204
- `;var v=class{constructor(e,t){this.isOpen=!1;this.boundHandlers=null;this.isDestroyed=!1;var r;let n=typeof e=="string"?document.querySelector(e):e;if(!n)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",n instanceof HTMLInputElement?(this.input=n,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(n.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.input.classList.add("pro6pp-input"),t.inputClass){let i=t.inputClass.split(" ");this.input.classList.add(...i)}let s=document.createElement("div");s.className="pro6pp-input-addons",this.wrapper.appendChild(s),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=`
204
+ `;var x=new WeakMap;function K(o){return x.get(o)}function j(o,...e){e.forEach(t=>x.set(t,o))}var S=class{constructor(e,t){this.isOpen=!1;this.boundHandlers=null;this.isDestroyed=!1;var s;let n=typeof e=="string"?document.querySelector(e):e;if(!n)throw new Error("InferJS: Target element not found.");let i=K(n);if(i&&i.destroy(),this.targetElement=n,this.ownsInput=!(n instanceof HTMLInputElement),this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",n instanceof HTMLInputElement?(this.input=n,(s=this.input.parentNode)==null||s.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(n.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.input.classList.add("pro6pp-input"),t.inputClass){let a=t.inputClass.split(" ");this.input.classList.add(...a)}let r=document.createElement("div");r.className="pro6pp-input-addons",this.wrapper.appendChild(r),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=`
205
205
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
206
206
  <line x1="18" y1="6" x2="6" y2="18"></line>
207
207
  <line x1="6" y1="6" x2="18" y2="18"></line>
208
208
  </svg>
209
- `,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.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
209
+ `,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.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
210
210
  <div class="pro6pp-mini-spinner"></div>
211
211
  <span>${this.loadingText}</span>
212
- `,this.dropdown.appendChild(this.dropdownLoader),this.core=new x(m(f({},t),{onStateChange:i=>{this.render(i),t.onStateChange&&t.onStateChange(i)},onSelect:i=>{typeof i=="string"?this.input.value=i:i&&typeof i=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(i)}})),this.observer=new IntersectionObserver(i=>{i[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=e.addition?` ${e.addition}`:"",n=e.postcode?`${e.postcode}, `:"",s=`${e.street}, ${e.street_number}${t}, ${n}${e.city}`;this.core.selectItem({label:s,value:e})}destroy(){this.isDestroyed||(this.isDestroyed=!0,this.boundHandlers&&(this.input.removeEventListener("input",this.boundHandlers.onInput),this.input.removeEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.removeEventListener("click",this.boundHandlers.onClearClick),document.removeEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.removeEventListener("focus",this.boundHandlers.onFocus),this.boundHandlers=null),this.observer.disconnect(),this.wrapper.parentNode&&(this.wrapper.parentNode.insertBefore(this.input,this.wrapper),this.wrapper.remove()),this.input.classList.remove("pro6pp-input"),this.input.value="")}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=C,document.head.appendChild(t)}}bindEvents(){this.boundHandlers={onInput:e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)},onKeyDown:e=>{this.core.handleKeyDown(e)},onClearClick:()=>{this.core.handleInput(""),this.input.focus()},onDocumentMouseDown:e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")},onFocus:()=>{this.isOpen=!0,this.render(this.core.state)}},this.input.addEventListener("input",this.boundHandlers.onInput),this.input.addEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.addEventListener("click",this.boundHandlers.onClearClick),document.addEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.addEventListener("focus",this.boundHandlers.onFocus)}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],n=t.length>0,s=!e.isLoading&&!e.isError&&e.query.length>0&&!n&&!e.isValid;if(!(this.isOpen&&(n||e.isLoading||s))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&n?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!n){let i=document.createElement("li");i.className="pro6pp-no-results",i.textContent="Searching...",this.list.appendChild(i),this.dropdownLoader.style.display="none";return}if(s){let i=document.createElement("li");i.className="pro6pp-no-results",i.textContent=this.noResultsText,this.list.appendChild(i);return}if(t.forEach((i,p)=>{if(!i.label)return;let u=document.createElement("li");u.className="pro6pp-item",p===e.selectedSuggestionIndex&&u.classList.add("pro6pp-item--active"),u.setAttribute("role","option"),u.setAttribute("aria-selected",p===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",D(i.label,e.query).forEach(({text:d,match:E})=>{let S=document.createElement("span");S.className=E?"pro6pp-item__label--match":"pro6pp-item__label--unmatched",S.textContent=d,a.appendChild(S)}),u.appendChild(a);let l=i.count!==void 0&&i.count!==null?i.count:"",h=i.subtitle||l;if(h!==""){let d=document.createElement("span");d.className="pro6pp-item__subtitle",d.textContent=`, ${h}`,u.appendChild(d)}if(i.value===void 0||i.value===null){let d=document.createElement("div");d.className="pro6pp-item__chevron",d.innerHTML=`
212
+ `,this.dropdown.appendChild(this.dropdownLoader),this.core=new v(m(f({},t),{onStateChange:a=>{this.render(a),t.onStateChange&&t.onStateChange(a)},onSelect:a=>{typeof a=="string"?this.input.value=a:a&&typeof a=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(a)}})),this.observer=new IntersectionObserver(a=>{a[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents(),j(this,this.targetElement,this.input)}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=e.addition?` ${e.addition}`:"",n=e.postcode?`${e.postcode}, `:"",i=`${e.street}, ${e.street_number}${t}, ${n}${e.city}`;this.core.selectItem({label:i,value:e})}destroy(){this.isDestroyed||(this.isDestroyed=!0,this.core.destroy(),this.boundHandlers&&(this.input.removeEventListener("input",this.boundHandlers.onInput),this.input.removeEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.removeEventListener("click",this.boundHandlers.onClearClick),document.removeEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.removeEventListener("focus",this.boundHandlers.onFocus),this.boundHandlers=null),this.observer.disconnect(),x.delete(this.targetElement),x.delete(this.input),this.wrapper.parentNode&&(this.ownsInput||this.wrapper.parentNode.insertBefore(this.input,this.wrapper),this.wrapper.remove()),this.input.classList.remove("pro6pp-input"),this.input.value="")}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=E,document.head.appendChild(t)}}bindEvents(){this.boundHandlers={onInput:e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)},onKeyDown:e=>{this.core.handleKeyDown(e)},onClearClick:()=>{this.core.handleInput(""),this.input.focus()},onDocumentMouseDown:e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")},onFocus:()=>{this.isOpen=!0,this.render(this.core.state)}},this.input.addEventListener("input",this.boundHandlers.onInput),this.input.addEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.addEventListener("click",this.boundHandlers.onClearClick),document.addEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.addEventListener("focus",this.boundHandlers.onFocus)}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],n=t.length>0,i=!e.isLoading&&!e.isError&&e.query.length>0&&!n&&!e.isValid;if(!(this.isOpen&&(n||e.isLoading||i))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&n?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Searching...",this.list.appendChild(s),this.dropdownLoader.style.display="none";return}if(i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}if(t.forEach((s,a)=>{if(!s.label)return;let d=document.createElement("li");d.className="pro6pp-item",a===e.selectedSuggestionIndex&&d.classList.add("pro6pp-item--active"),d.setAttribute("role","option"),d.setAttribute("aria-selected",a===e.selectedSuggestionIndex?"true":"false");let l=document.createElement("span");l.className="pro6pp-item__label",k(s.label,e.query).forEach(({text:u,match:I})=>{let w=document.createElement("span");w.className=I?"pro6pp-item__label--match":"pro6pp-item__label--unmatched",w.textContent=u,l.appendChild(w)}),d.appendChild(l);let p=s.count!==void 0&&s.count!==null?s.count:"",h=s.subtitle||p;if(h!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${h}`,d.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
213
213
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
214
214
  <polyline points="9 18 15 12 9 6"></polyline>
215
215
  </svg>
216
- `,u.appendChild(d)}u.onmousedown=d=>d.preventDefault(),u.onclick=d=>{d.stopPropagation(),this.core.selectItem(i)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(u)}),e.hasMore&&!e.isLoading){let i=document.createElement("li");i.style.height="1px",i.style.opacity="0",this.list.appendChild(i),this.observer.observe(i)}}};function q(o,e){return new v(o,e)}return O(K);})();
216
+ `,d.appendChild(u)}d.onmousedown=u=>u.preventDefault(),d.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(d)}),e.hasMore&&!e.isLoading){let s=document.createElement("li");s.style.height="1px",s.style.opacity="0",this.list.appendChild(s),this.observer.observe(s)}}};function Q(o,e){return new S(o,e)}return U(J);})();
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- var A=Object.defineProperty,D=Object.defineProperties;var k=Object.getOwnPropertyDescriptors;var E=Object.getOwnPropertySymbols;var $=Object.prototype.hasOwnProperty,R=Object.prototype.propertyIsEnumerable;var L=(o,e,t)=>e in o?A(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))$.call(e,t)&&L(o,t,e[t]);if(E)for(var t of E(e))R.call(e,t)&&L(o,t,e[t]);return o},m=(o,e)=>D(o,k(e));function I(o){return o.toLowerCase().trim()}function F(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function H(o,e){let t=I(o),n=I(e);if(n.includes(" "))return t.indexOf(n);let r=new RegExp(`(?:^|[,\\s])${F(n)}(?:$|[,\\s])`,"g").exec(t);if(r){let i=r.index,p=t[i];return p===","||p===" "?i+1:i}return-1}function N(o,e){let t=[],n=[];e.street&&n.push({value:e.street,type:"street"}),e.city&&n.push({value:e.city,type:"city"}),e.postcode&&n.push({value:e.postcode,type:"postcode"}),e.street_number!==void 0&&e.street_number!==null&&n.push({value:String(e.street_number),type:"street_number"}),e.addition&&n.push({value:e.addition,type:"addition"});for(let s of n){let r=H(o,s.value);r!==-1&&t.push({type:s.type,value:s.value,position:r})}return t.sort((s,r)=>s.position-r.position),t}function T(o,e){if(!e||!o)return"";let t=N(o,e),n=new Set(t.map(i=>i.type)),s=[];for(let i of t)s.push(i.value);let r=["street","street_number","addition","postcode","city"];for(let i of r){if(n.has(i))continue;let p;switch(i){case"street":p=e.street;break;case"city":p=e.city;break;case"street_number":p=e.street_number!==void 0?String(e.street_number):void 0;break;case"postcode":p=e.postcode;break;case"addition":p=e.addition;break}p&&s.push(p)}return s.join(", ")}var b={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},v={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},_={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},y=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||b.LIMIT,this.currentLimit=this.baseLimit,this.language=e.language;let t=e.maxRetries!==void 0?e.maxRetries:b.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,i)=>fetch(r,i)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},_);let n=e.debounceMs!==void 0?e.debounceMs:b.DEBOUNCE_MS,s=Math.max(n,b.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 n=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(n>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=n&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=n-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let i=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];i&&(this.selectItem(i),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,n=t;typeof e!="string"&&typeof e.value=="string"&&(n=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 p=t;if(s&&Object.keys(s).length>0){let{street:u,street_number:a,postcode:c,city:l,addition:h}=s;if(u&&a&&l){let g=h?` ${h}`:"",d=c?`${c}, `:"";p=`${u}, ${a}${g}, ${d}${l}`}}return this.finishSelection(p,s),!0}let i=typeof e!="string"?e.subtitle:null;return this.processSelection(n,i),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&v.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let n=this.getCurrentFragment(e);return v.DIGITS_1_3.test(n)}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:n,query:s}=this.state,r=s;if(t&&(n==="city"||n==="street"||n==="mixed")){if(n==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(s),c=!a||!a.includes(t),l=a;if(a&&t){let h=a.match(v.STREET_NUMBER_PREFIX);if(h){let g=h[1];t.startsWith(g)&&(l="")}}c?r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `:r=l?`${l} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(n==="direct"||n==="addition"){this.finishSelection(e);return}!s.includes(",")&&(n==="city"||n==="street"||n==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(s,e),n!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,c;let n=(e||"").toString();if(!n.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=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${b.API_URL}/infer/${this.country.toLowerCase()}`,i=new URLSearchParams({query:n,limit:this.currentLimit.toString()});this.explicitApiUrl&&i.append("country",this.country.toLowerCase()),this.authKey&&i.set("authKey",this.authKey),this.language&&i.set("language",this.language);let p=r.includes("?")?"&":"?",u=`${r}${p}${i.toString()}`;this.fetcher(u,{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,n){if(n!=null&&n.aborted)return;let s=Math.pow(2,t)*200;setTimeout(()=>{n!=null&&n.aborted||this.executeFetch(e,t+1)},s)}mapResponseToState(e){var p,u,a,c;let t={stage:e.stage,isLoading:!1},n=e.suggestions||[],s=[],r=new Set;for(let l of n){let h=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;if(!r.has(h)){r.add(h);let g=this.reformatSuggestionLabel(l);s.push(g)}}let i=s.length+(((p=e.cities)==null?void 0:p.length)||0)+(((u=e.streets)==null?void 0:u.length)||0);t.hasMore=i>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((a=t.cities)==null?void 0:a.length)===0&&((c=t.streets)==null?void 0:c.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])}reformatSuggestionLabel(e){if(!e.value||typeof e.value=="string")return e;let t=e.value;if(!t.street||!t.city)return e;let n=T(this.state.query,t);return n?m(f({},e),{label:n}):e}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let n=e.lastIndexOf(",");return n===-1?t:`${e.slice(0,n+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({},_),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let n,s=(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>e.apply(this,r),t)};return s.cancel=()=>{n&&(clearTimeout(n),n=void 0)},s}};function B(o){if(o.length===0)return o;let e=[];for(let t of o){let n=e[e.length-1];n&&n.match===t.match?n.text+=t.text:e.push({text:t.text,match:t.match})}return e}function M(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],n=o.toLowerCase(),s=e.toLowerCase(),r=0,i=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&n[a]===s[r]))continue;a>i&&t.push({text:o.slice(i,a),match:!1}),t.push({text:o[a],match:!0}),r++,i=a+1}return i<o.length&&t.push({text:o.slice(i),match:!1}),r===e.length?B(t):[{text:o,match:!1}]}var S=`
1
+ var A=Object.defineProperty,k=Object.defineProperties;var H=Object.getOwnPropertyDescriptors;var I=Object.getOwnPropertySymbols;var $=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable;var L=(o,e,t)=>e in o?A(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))$.call(e,t)&&L(o,t,e[t]);if(I)for(var t of I(e))F.call(e,t)&&L(o,t,e[t]);return o},m=(o,e)=>k(o,H(e));function T(o){return o.toLowerCase().trim()}function R(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function N(o,e){let t=T(o),n=T(e);if(n.includes(" "))return t.indexOf(n);let r=new RegExp(`(?:^|[,\\s])${R(n)}(?:$|[,\\s])`,"g").exec(t);if(r){let i=r.index,a=t[i];return a===","||a===" "?i+1:i}return-1}function B(o,e){let t=[],n=[];e.street&&n.push({value:e.street,type:"street"}),e.city&&n.push({value:e.city,type:"city"}),e.postcode&&n.push({value:e.postcode,type:"postcode"}),e.street_number!==void 0&&e.street_number!==null&&n.push({value:String(e.street_number),type:"street_number"}),e.addition&&n.push({value:e.addition,type:"addition"});for(let s of n){let r=N(o,s.value);r!==-1&&t.push({type:s.type,value:s.value,position:r})}return t.sort((s,r)=>s.position-r.position),t}function D(o,e){if(!e||!o)return"";let t=B(o,e),n=new Set(t.map(i=>i.type)),s=[];for(let i of t)s.push(i.value);let r=["street","street_number","addition","postcode","city"];for(let i of r){if(n.has(i))continue;let a;switch(i){case"street":a=e.street;break;case"city":a=e.city;break;case"street_number":a=e.street_number!==void 0?String(e.street_number):void 0;break;case"postcode":a=e.postcode;break;case"addition":a=e.addition;break}a&&s.push(a)}return s.join(", ")}var b={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},y=class{constructor(e){this.abortController=null;this.isDestroyed=!1;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||b.LIMIT,this.currentLimit=this.baseLimit,this.language=e.language;let t=e.maxRetries!==void 0?e.maxRetries:b.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,i)=>fetch(r,i)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},M);let n=e.debounceMs!==void 0?e.debounceMs:b.DEBOUNCE_MS,s=Math.max(n,b.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),s)}handleInput(e){if(this.isDestroyed)return;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.isDestroyed||this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){if(this.isDestroyed)return;let t=e.target;if(!t)return;let n=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(n>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=n&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=n-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let i=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];i&&(this.selectItem(i),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){if(this.isDestroyed)return!1;this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,n=t;typeof e!="string"&&typeof e.value=="string"&&(n=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 a=t;if(s&&Object.keys(s).length>0){let{street:d,street_number:l,postcode:c,city:p,addition:h}=s;if(d&&l&&p){let g=h?` ${h}`:"",u=c?`${c}, `:"";a=`${d}, ${l}${g}, ${u}${p}`}}return this.finishSelection(a,s),!0}let i=typeof e!="string"?e.subtitle:null;return this.processSelection(n,i),!1}destroy(){var e;this.isDestroyed||(this.isDestroyed=!0,this.debouncedFetch.cancel(),(e=this.abortController)==null||e.abort(),this.abortController=null)}shouldAutoInsertComma(e){if(!e.includes(",")&&S.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let n=this.getCurrentFragment(e);return S.DIGITS_1_3.test(n)}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:n,query:s}=this.state,r=s;if(t&&(n==="city"||n==="street"||n==="mixed")){if(n==="city")r=`${t}, ${e}, `;else{let l=this.getQueryPrefix(s),c=!l||!l.includes(t),p=l;if(l&&t){let h=l.match(S.STREET_NUMBER_PREFIX);if(h){let g=h[1];t.startsWith(g)&&(p="")}}c?r=p?`${p} ${e}, ${t}, `:`${e}, ${t}, `:r=p?`${p} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(n==="direct"||n==="addition"){this.finishSelection(e);return}!s.includes(",")&&(n==="city"||n==="street"||n==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(s,e),n!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var l,c;if(this.isDestroyed)return;let n=(e||"").toString();if(!n.trim()){(l=this.abortController)==null||l.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let s=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${b.API_URL}/infer/${this.country.toLowerCase()}`,i=new URLSearchParams({query:n,limit:this.currentLimit.toString()});this.explicitApiUrl&&i.append("country",this.country.toLowerCase()),this.authKey&&i.set("authKey",this.authKey),this.language&&i.set("language",this.language);let a=r.includes("?")?"&":"?",d=`${r}${a}${i.toString()}`;this.fetcher(d,{signal:s}).then(p=>{if(!p.ok){if(t<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(e,t,s);throw new Error("Network error")}return p.json()}).then(p=>{this.isDestroyed||s!=null&&s.aborted||p&&this.mapResponseToState(p)}).catch(p=>{if(!this.isDestroyed&&p.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,s);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,n){if(this.isDestroyed||n!=null&&n.aborted)return;let s=Math.pow(2,t)*200;setTimeout(()=>{!this.isDestroyed&&!(n!=null&&n.aborted)&&this.executeFetch(e,t+1)},s)}mapResponseToState(e){var a,d,l,c;let t={stage:e.stage,isLoading:!1},n=e.suggestions||[],s=[],r=new Set;for(let p of n){let h=`${p.label}|${p.subtitle||""}|${JSON.stringify(p.value||{})}`;if(!r.has(h)){r.add(h);let g=this.reformatSuggestionLabel(p);s.push(g)}}let i=s.length+(((a=e.cities)==null?void 0:a.length)||0)+(((d=e.streets)==null?void 0:d.length)||0);t.hasMore=i>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((l=t.cities)==null?void 0:l.length)===0&&((c=t.streets)==null?void 0:c.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])}reformatSuggestionLabel(e){if(!e.value||typeof e.value=="string")return e;let t=e.value;if(!t.street||!t.city)return e;let n=D(this.state.query,t);return n?m(f({},e),{label:n}):e}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let n=e.lastIndexOf(",");return n===-1?t:`${e.slice(0,n+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({},M),{query:this.state.query}))}updateState(e){this.isDestroyed||(this.state=f(f({},this.state),e),this.onStateChange(this.state))}debounce(e,t){let n,s=(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>e.apply(this,r),t)};return s.cancel=()=>{n&&(clearTimeout(n),n=void 0)},s}};function O(o){if(o.length===0)return o;let e=[];for(let t of o){let n=e[e.length-1];n&&n.match===t.match?n.text+=t.text:e.push({text:t.text,match:t.match})}return e}function _(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],n=o.toLowerCase(),s=e.toLowerCase(),r=0,i=0;for(let l=0;l<o.length;l++){if(!(r<e.length&&n[l]===s[r]))continue;l>i&&t.push({text:o.slice(i,l),match:!1}),t.push({text:o[l],match:!0}),r++,i=l+1}return i<o.length&&t.push({text:o.slice(i),match:!1}),r===e.length?O(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;
@@ -201,16 +201,16 @@ var A=Object.defineProperty,D=Object.defineProperties;var k=Object.getOwnPropert
201
201
  @keyframes pro6pp-spin {
202
202
  to { transform: rotate(360deg); }
203
203
  }
204
- `;var w=class{constructor(e,t){this.isOpen=!1;this.boundHandlers=null;this.isDestroyed=!1;var r;let n=typeof e=="string"?document.querySelector(e):e;if(!n)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",n instanceof HTMLInputElement?(this.input=n,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(n.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.input.classList.add("pro6pp-input"),t.inputClass){let i=t.inputClass.split(" ");this.input.classList.add(...i)}let s=document.createElement("div");s.className="pro6pp-input-addons",this.wrapper.appendChild(s),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=`
204
+ `;var v=new WeakMap;function U(o){return v.get(o)}function V(o,...e){e.forEach(t=>v.set(t,o))}var C=class{constructor(e,t){this.isOpen=!1;this.boundHandlers=null;this.isDestroyed=!1;var i;let n=typeof e=="string"?document.querySelector(e):e;if(!n)throw new Error("InferJS: Target element not found.");let s=U(n);if(s&&s.destroy(),this.targetElement=n,this.ownsInput=!(n instanceof HTMLInputElement),this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",n instanceof HTMLInputElement?(this.input=n,(i=this.input.parentNode)==null||i.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(n.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.input.classList.add("pro6pp-input"),t.inputClass){let a=t.inputClass.split(" ");this.input.classList.add(...a)}let r=document.createElement("div");r.className="pro6pp-input-addons",this.wrapper.appendChild(r),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=`
205
205
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
206
206
  <line x1="18" y1="6" x2="6" y2="18"></line>
207
207
  <line x1="6" y1="6" x2="18" y2="18"></line>
208
208
  </svg>
209
- `,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.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
209
+ `,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.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
210
210
  <div class="pro6pp-mini-spinner"></div>
211
211
  <span>${this.loadingText}</span>
212
- `,this.dropdown.appendChild(this.dropdownLoader),this.core=new y(m(f({},t),{onStateChange:i=>{this.render(i),t.onStateChange&&t.onStateChange(i)},onSelect:i=>{typeof i=="string"?this.input.value=i:i&&typeof i=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(i)}})),this.observer=new IntersectionObserver(i=>{i[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=e.addition?` ${e.addition}`:"",n=e.postcode?`${e.postcode}, `:"",s=`${e.street}, ${e.street_number}${t}, ${n}${e.city}`;this.core.selectItem({label:s,value:e})}destroy(){this.isDestroyed||(this.isDestroyed=!0,this.boundHandlers&&(this.input.removeEventListener("input",this.boundHandlers.onInput),this.input.removeEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.removeEventListener("click",this.boundHandlers.onClearClick),document.removeEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.removeEventListener("focus",this.boundHandlers.onFocus),this.boundHandlers=null),this.observer.disconnect(),this.wrapper.parentNode&&(this.wrapper.parentNode.insertBefore(this.input,this.wrapper),this.wrapper.remove()),this.input.classList.remove("pro6pp-input"),this.input.value="")}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.boundHandlers={onInput:e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)},onKeyDown:e=>{this.core.handleKeyDown(e)},onClearClick:()=>{this.core.handleInput(""),this.input.focus()},onDocumentMouseDown:e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")},onFocus:()=>{this.isOpen=!0,this.render(this.core.state)}},this.input.addEventListener("input",this.boundHandlers.onInput),this.input.addEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.addEventListener("click",this.boundHandlers.onClearClick),document.addEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.addEventListener("focus",this.boundHandlers.onFocus)}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],n=t.length>0,s=!e.isLoading&&!e.isError&&e.query.length>0&&!n&&!e.isValid;if(!(this.isOpen&&(n||e.isLoading||s))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&n?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!n){let i=document.createElement("li");i.className="pro6pp-no-results",i.textContent="Searching...",this.list.appendChild(i),this.dropdownLoader.style.display="none";return}if(s){let i=document.createElement("li");i.className="pro6pp-no-results",i.textContent=this.noResultsText,this.list.appendChild(i);return}if(t.forEach((i,p)=>{if(!i.label)return;let u=document.createElement("li");u.className="pro6pp-item",p===e.selectedSuggestionIndex&&u.classList.add("pro6pp-item--active"),u.setAttribute("role","option"),u.setAttribute("aria-selected",p===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",M(i.label,e.query).forEach(({text:d,match:C})=>{let x=document.createElement("span");x.className=C?"pro6pp-item__label--match":"pro6pp-item__label--unmatched",x.textContent=d,a.appendChild(x)}),u.appendChild(a);let l=i.count!==void 0&&i.count!==null?i.count:"",h=i.subtitle||l;if(h!==""){let d=document.createElement("span");d.className="pro6pp-item__subtitle",d.textContent=`, ${h}`,u.appendChild(d)}if(i.value===void 0||i.value===null){let d=document.createElement("div");d.className="pro6pp-item__chevron",d.innerHTML=`
212
+ `,this.dropdown.appendChild(this.dropdownLoader),this.core=new y(m(f({},t),{onStateChange:a=>{this.render(a),t.onStateChange&&t.onStateChange(a)},onSelect:a=>{typeof a=="string"?this.input.value=a:a&&typeof a=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(a)}})),this.observer=new IntersectionObserver(a=>{a[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents(),V(this,this.targetElement,this.input)}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=e.addition?` ${e.addition}`:"",n=e.postcode?`${e.postcode}, `:"",s=`${e.street}, ${e.street_number}${t}, ${n}${e.city}`;this.core.selectItem({label:s,value:e})}destroy(){this.isDestroyed||(this.isDestroyed=!0,this.core.destroy(),this.boundHandlers&&(this.input.removeEventListener("input",this.boundHandlers.onInput),this.input.removeEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.removeEventListener("click",this.boundHandlers.onClearClick),document.removeEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.removeEventListener("focus",this.boundHandlers.onFocus),this.boundHandlers=null),this.observer.disconnect(),v.delete(this.targetElement),v.delete(this.input),this.wrapper.parentNode&&(this.ownsInput||this.wrapper.parentNode.insertBefore(this.input,this.wrapper),this.wrapper.remove()),this.input.classList.remove("pro6pp-input"),this.input.value="")}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.boundHandlers={onInput:e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)},onKeyDown:e=>{this.core.handleKeyDown(e)},onClearClick:()=>{this.core.handleInput(""),this.input.focus()},onDocumentMouseDown:e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")},onFocus:()=>{this.isOpen=!0,this.render(this.core.state)}},this.input.addEventListener("input",this.boundHandlers.onInput),this.input.addEventListener("keydown",this.boundHandlers.onKeyDown),this.clearButton.addEventListener("click",this.boundHandlers.onClearClick),document.addEventListener("mousedown",this.boundHandlers.onDocumentMouseDown),this.input.addEventListener("focus",this.boundHandlers.onFocus)}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],n=t.length>0,s=!e.isLoading&&!e.isError&&e.query.length>0&&!n&&!e.isValid;if(!(this.isOpen&&(n||e.isLoading||s))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&n?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!n){let i=document.createElement("li");i.className="pro6pp-no-results",i.textContent="Searching...",this.list.appendChild(i),this.dropdownLoader.style.display="none";return}if(s){let i=document.createElement("li");i.className="pro6pp-no-results",i.textContent=this.noResultsText,this.list.appendChild(i);return}if(t.forEach((i,a)=>{if(!i.label)return;let d=document.createElement("li");d.className="pro6pp-item",a===e.selectedSuggestionIndex&&d.classList.add("pro6pp-item--active"),d.setAttribute("role","option"),d.setAttribute("aria-selected",a===e.selectedSuggestionIndex?"true":"false");let l=document.createElement("span");l.className="pro6pp-item__label",_(i.label,e.query).forEach(({text:u,match:E})=>{let x=document.createElement("span");x.className=E?"pro6pp-item__label--match":"pro6pp-item__label--unmatched",x.textContent=u,l.appendChild(x)}),d.appendChild(l);let p=i.count!==void 0&&i.count!==null?i.count:"",h=i.subtitle||p;if(h!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${h}`,d.appendChild(u)}if(i.value===void 0||i.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
213
213
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
214
214
  <polyline points="9 18 15 12 9 6"></polyline>
215
215
  </svg>
216
- `,u.appendChild(d)}u.onmousedown=d=>d.preventDefault(),u.onclick=d=>{d.stopPropagation(),this.core.selectItem(i)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(u)}),e.hasMore&&!e.isLoading){let i=document.createElement("li");i.style.height="1px",i.style.opacity="0",this.list.appendChild(i),this.observer.observe(i)}}};function te(o,e){return new w(o,e)}export{w as InferJS,te as attach};
216
+ `,d.appendChild(u)}d.onmousedown=u=>u.preventDefault(),d.onclick=u=>{u.stopPropagation(),this.core.selectItem(i)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(d)}),e.hasMore&&!e.isLoading){let i=document.createElement("li");i.style.height="1px",i.style.opacity="0",this.list.appendChild(i),this.observer.observe(i)}}};function se(o,e){return new C(o,e)}export{C as InferJS,se as attach};
package/package.json CHANGED
@@ -18,7 +18,7 @@
18
18
  "sideEffects": [
19
19
  "*.css"
20
20
  ],
21
- "version": "0.1.0-beta.19",
21
+ "version": "0.1.0-beta.20",
22
22
  "main": "./dist/index.js",
23
23
  "module": "./dist/index.mjs",
24
24
  "types": "./dist/index.d.ts",
@@ -44,7 +44,7 @@
44
44
  "test:coverage": "vitest run --coverage"
45
45
  },
46
46
  "dependencies": {
47
- "@pro6pp/infer-core": "0.1.0-beta.18"
47
+ "@pro6pp/infer-core": "0.1.0-beta.19"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@testing-library/dom": "^10.4.1",