@magicx-eng/ai-autocomplete-react 0.1.12 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import{forwardRef as gt,useCallback as ft,useEffect as bt,useImperativeHandle as ht,useRef as He}from"react";if(typeof document<"u"&&!document.getElementById("ac-style-cc65f4cc")){let e=document.createElement("style");e.id="ac-style-cc65f4cc",e.textContent=`.AIAutocomplete-module_container_KKjFU {
1
+ import{forwardRef as bt,useCallback as ht,useEffect as yt,useImperativeHandle as St,useRef as Ve}from"react";if(typeof document<"u"&&!document.getElementById("ac-style-cc65f4cc")){let e=document.createElement("style");e.id="ac-style-cc65f4cc",e.textContent=`.AIAutocomplete-module_container_KKjFU {
2
2
  position: relative;
3
3
  font-family: "IBM Plex Sans", sans-serif;
4
4
  }
@@ -29,7 +29,7 @@ import{forwardRef as gt,useCallback as ft,useEffect as bt,useImperativeHandle as
29
29
  white-space: pre-wrap;
30
30
  word-break: break-word;
31
31
  font-family: inherit;
32
- font-size: 21px;
32
+ font-size: 19px;
33
33
  line-height: 38px;
34
34
  }
35
35
 
@@ -54,7 +54,7 @@ import{forwardRef as gt,useCallback as ft,useEffect as bt,useImperativeHandle as
54
54
  color: var(--ac-color-text-default, #fff);
55
55
  caret-color: var(--ac-color-text-default, #fff);
56
56
  font-family: inherit;
57
- font-size: 21px;
57
+ font-size: 19px;
58
58
  line-height: 38px;
59
59
  white-space: pre-wrap;
60
60
  word-break: break-word;
@@ -92,7 +92,7 @@ import{forwardRef as gt,useCallback as ft,useEffect as bt,useImperativeHandle as
92
92
  right: 0;
93
93
  top: 100%;
94
94
  margin-top: 6px;
95
- background: var(--ac-color-background-default, #00002d);
95
+ background: var(--ac-color-background-default, #0b0247);
96
96
  border-radius: 23px;
97
97
  overflow: hidden;
98
98
  z-index: 10;
@@ -105,7 +105,7 @@ import{forwardRef as gt,useCallback as ft,useEffect as bt,useImperativeHandle as
105
105
  opacity: 1;
106
106
  pointer-events: auto;
107
107
  }
108
- `,document.head.appendChild(e)}var he={dropdown:"AIAutocompleteDropdown-module_dropdown_yz2KC",visible:"AIAutocompleteDropdown-module_visible_QCoXj"};if(typeof document<"u"&&!document.getElementById("ac-style-d91f2b06")){let e=document.createElement("style");e.id="ac-style-d91f2b06",e.textContent=`.SuggestionGrid-module_grid_jvaPb {
108
+ `,document.head.appendChild(e)}var Se={dropdown:"AIAutocompleteDropdown-module_dropdown_yz2KC",visible:"AIAutocompleteDropdown-module_visible_QCoXj"};if(typeof document<"u"&&!document.getElementById("ac-style-d91f2b06")){let e=document.createElement("style");e.id="ac-style-d91f2b06",e.textContent=`.SuggestionGrid-module_grid_jvaPb {
109
109
  display: grid;
110
110
  grid-template-columns: 1fr 1fr;
111
111
  gap: 12px 18px;
@@ -128,14 +128,15 @@ import{forwardRef as gt,useCallback as ft,useEffect as bt,useImperativeHandle as
128
128
  background: rgba(255, 255, 255, 0.3);
129
129
  border-radius: 3px;
130
130
  }
131
- `,document.head.appendChild(e)}var Oe={grid:"SuggestionGrid-module_grid_jvaPb"};if(typeof document<"u"&&!document.getElementById("ac-style-f6bdc634")){let e=document.createElement("style");e.id="ac-style-f6bdc634",e.textContent=`.SuggestionItem-module_item_d4vpD {
131
+ `,document.head.appendChild(e)}var ke={grid:"SuggestionGrid-module_grid_jvaPb"};if(typeof document<"u"&&!document.getElementById("ac-style-f6bdc634")){let e=document.createElement("style");e.id="ac-style-f6bdc634",e.textContent=`.SuggestionItem-module_item_d4vpD {
132
132
  display: flex;
133
133
  align-items: center;
134
134
  font-family: "IBM Plex Sans", sans-serif;
135
- font-size: 21px;
135
+ font-size: 19px;
136
136
  line-height: 30px;
137
137
  color: var(--ac-color-text-muted, #c1c4cb);
138
- white-space: nowrap;
138
+ white-space: normal;
139
+ word-break: break-word;
139
140
  opacity: 0.35;
140
141
  animation: SuggestionItem-module_fadeIn_I8u35 400ms cubic-bezier(0.4, 0, 0.2, 1) forwards;
141
142
  }
@@ -165,11 +166,11 @@ import{forwardRef as gt,useCallback as ft,useEffect as bt,useImperativeHandle as
165
166
  }
166
167
 
167
168
  .SuggestionItem-module_tag_e3Fwe {
168
- font-size: 13px;
169
+ font-size: 11px;
169
170
  margin-left: 6px;
170
171
  opacity: 0.5;
171
172
  }
172
- `,document.head.appendChild(e)}var G={item:"SuggestionItem-module_item_d4vpD",fadeIn:"SuggestionItem-module_fadeIn_I8u35",tappable:"SuggestionItem-module_tappable_70KcX",nonTappable:"SuggestionItem-module_nonTappable_xSZM-",highlighted:"SuggestionItem-module_highlighted_Hb0SU",tag:"SuggestionItem-module_tag_e3Fwe"};import{jsx as ot,jsxs as nt}from"react/jsx-runtime";function Re({option:e,isHighlighted:r,onSelect:t,onHighlight:o,id:n}){let i=[G.item,r?G.highlighted:"",e.is_tappable?G.tappable:G.nonTappable].filter(Boolean).join(" ");return nt("div",{id:n,role:"option","aria-selected":r,className:i,tabIndex:e.is_tappable?0:-1,onClick:()=>e.is_tappable&&t(e),onKeyDown:s=>{e.is_tappable&&(s.key==="Enter"||s.key===" ")&&(s.preventDefault(),t(e))},onMouseEnter:o,children:[e.icon?`${e.icon} ${e.text}`:e.text,e.tag&&ot("span",{className:G.tag,children:e.tag})]})}import{jsx as ke}from"react/jsx-runtime";function Me({options:e,activeIndex:r,onSelect:t,onHighlight:o,listboxId:n}){return ke("div",{className:Oe.grid,children:e.map((i,s)=>ke(Re,{option:i,isHighlighted:s===r,onSelect:t,onHighlight:()=>o(s),id:`${n}-option-${s}`},i.text))})}import{jsx as Te}from"react/jsx-runtime";function ye({suggestions:e,activeIndex:r,onSelect:t,onHighlight:o,isOpen:n,id:i,className:s}){let p=e[0]?.options??[],g=n&&p.length>0;return Te("div",{id:i,role:"listbox",className:`${he.dropdown} ${g?he.visible:""} ${s??""}`,onMouseDown:c=>c.preventDefault(),children:p.length>0&&Te(Me,{options:p,activeIndex:r,onSelect:t,onHighlight:o,listboxId:i})})}if(typeof document<"u"&&!document.getElementById("ac-style-b745b4fb")){let e=document.createElement("style");e.id="ac-style-b745b4fb",e.textContent=`.PillList-module_list_qvLqO {
173
+ `,document.head.appendChild(e)}var X={item:"SuggestionItem-module_item_d4vpD",fadeIn:"SuggestionItem-module_fadeIn_I8u35",tappable:"SuggestionItem-module_tappable_70KcX",nonTappable:"SuggestionItem-module_nonTappable_xSZM-",highlighted:"SuggestionItem-module_highlighted_Hb0SU",tag:"SuggestionItem-module_tag_e3Fwe"};import{jsx as rt,jsxs as it}from"react/jsx-runtime";function Me({option:e,isHighlighted:r,onSelect:t,onHighlight:o,id:n}){let i=[X.item,r?X.highlighted:"",e.is_tappable?X.tappable:X.nonTappable].filter(Boolean).join(" ");return it("div",{id:n,role:"option","aria-selected":r,className:i,tabIndex:e.is_tappable?0:-1,onClick:()=>e.is_tappable&&t(e),onKeyDown:s=>{e.is_tappable&&(s.key==="Enter"||s.key===" ")&&(s.preventDefault(),t(e))},onMouseEnter:o,children:[e.icon?`${e.icon} ${e.text}`:e.text,e.tag&&rt("span",{className:X.tag,children:e.tag})]})}import{jsx as Ee}from"react/jsx-runtime";function Te({options:e,activeIndex:r,onSelect:t,onHighlight:o,listboxId:n}){return Ee("div",{className:ke.grid,children:e.map((i,s)=>Ee(Me,{option:i,isHighlighted:s===r,onSelect:t,onHighlight:()=>o(s),id:`${n}-option-${s}`},i.text))})}import{jsx as Le}from"react/jsx-runtime";function xe({suggestions:e,activeIndex:r,onSelect:t,onHighlight:o,isOpen:n,id:i,className:s}){let d=e[0]?.options??[],c=n&&d.length>0;return Le("div",{id:i,role:"listbox",className:`${Se.dropdown} ${c?Se.visible:""} ${s??""}`,onMouseDown:u=>u.preventDefault(),children:d.length>0&&Le(Te,{options:d,activeIndex:r,onSelect:t,onHighlight:o,listboxId:i})})}if(typeof document<"u"&&!document.getElementById("ac-style-b745b4fb")){let e=document.createElement("style");e.id="ac-style-b745b4fb",e.textContent=`.PillList-module_list_qvLqO {
173
174
  position: relative;
174
175
  z-index: 1;
175
176
  pointer-events: auto;
@@ -191,7 +192,7 @@ import{forwardRef as gt,useCallback as ft,useEffect as bt,useImperativeHandle as
191
192
  background: var(--ac-color-background-supportive, #313255);
192
193
  color: var(--ac-color-text-muted, #c1c4cb);
193
194
  font-family: "IBM Plex Sans", sans-serif;
194
- font-size: 21px;
195
+ font-size: 19px;
195
196
  line-height: 30px;
196
197
  cursor: pointer;
197
198
  white-space: nowrap;
@@ -211,5 +212,5 @@ import{forwardRef as gt,useCallback as ft,useEffect as bt,useImperativeHandle as
211
212
  opacity: 0;
212
213
  }
213
214
  }
214
- `,document.head.appendChild(e)}var re={list:"PillList-module_list_qvLqO",pill:"PillList-module_pill_osSyz",fadeIn:"PillList-module_fadeIn_Aezob",active:"PillList-module_active_Oll--"};import{jsx as Ee}from"react/jsx-runtime";function rt(e){return e===0?.4:e===1?.3:.15}function Le({pills:e,activePillIndex:r,onSelectPill:t}){return Ee("span",{className:re.list,children:e.map((o,n)=>Ee("button",{type:"button",className:`${re.pill} ${n===r?re.active:""}`,style:{opacity:rt(n)},onClick:()=>t(n),children:o.text},`${o.type}-${o.text}`))})}import{useCallback as J,useMemo as ae,useRef as k,useState as se}from"react";function ie(e,r){if(!e)return[];let t=r.trimStart();if(!t)return e;let o=t.toLowerCase();return e.filter(n=>!n.is_tappable||n.text.toLowerCase().includes(o))}function ee(e,r){if(!e)return null;let t=r.trim();if(!t)return null;let o=t.toLowerCase();return e.find(n=>n.is_tappable&&n.text.toLowerCase()===o)??null}function Ue(e,r){let t=[],o=0;for(let i of r){let s=e.indexOf(i.text,o);s!==-1&&(s>o&&t.push({type:"text",value:e.slice(o,s)}),t.push({type:"completed",value:i.text,param:i}),o=s+i.text.length)}let n=e.slice(o);return n&&t.push({type:"text",value:n}),t}function Ke(e,r){let t=[],o=[],n=0;for(let i of r){let s=e.indexOf(i.text,n);s===-1?o.push(i):(t.push(i),n=s+i.text.length)}return{valid:t,invalid:o}}import{useCallback as lt,useEffect as Fe,useRef as Z,useState as Se}from"react";var it="0.1.12",De=!1;function at(){return crypto.randomUUID()}function st(e,r){return{placeholder:e.placeholder,type:e.type,...r&&{text:e.text},kind:e.kind}}async function ze(e,r,t){let o=t?.apiConfig,n=o?.apiKey??"";!n&&!De&&(De=!0,console.warn("[AIAutocomplete] No apiKey in apiConfig. Requests will be sent without an Authorization header."));let i=o?.authScheme??"Bearer",s=!t?.maskCompletedText,f=r.find(u=>u.type==="contact"&&u.metadata?.contact_account_count)?.metadata?.contact_account_count,p=typeof f=="number"?f:void 0,g={data:{raw_query:e,completed_params:r.map(u=>st(u,s)),...p!=null&&{contact_account_count:p}},meta:{request_id:at(),request_at:new Date().toISOString(),language:typeof navigator<"u"?navigator.language:"en-US",client_version:it}},c={"Content-Type":"application/json",...o?.appIdentifier&&{"X-App-Identifier":o.appIdentifier},...o?.headers};n&&(c.Authorization=i==="Basic"?`Basic ${btoa(n)}`:`Bearer ${n}`);let b=await fetch(o?.endpoint??"/ac/suggest",{method:"POST",headers:c,body:JSON.stringify(g),signal:t?.signal});if(!b.ok)throw new Error(`API error: ${b.status} ${b.statusText}`);return b.json()}function X(e,r){let t=e,o={},n=[];for(let i of r){let s=(o[i.type]??0)+1;o[i.type]=s;let p=`{{${i.type.toUpperCase().replace(/\s+/g,"_")}_${s}}}`,g=t.indexOf(i.text);g!==-1&&(t=t.slice(0,g)+p+t.slice(g+i.text.length)),n.push({...i,placeholder:p})}return{rawQuery:t,completedParams:n}}function je(e,r){return r?e.map(t=>{let o=r[t.type];if(!o)return t;let n=o("");if(n.length===0)return t;let i=new Set(n.map(f=>f.text)),s=(t.options??[]).filter(f=>!i.has(f.text));return{...t,options:[...n,...s]}}):e}var ct=100,ut=300,pt=2;function qe({textRef:e,suggestionsRef:r,filterBaseRef:t,maskCompletedTextRef:o,apiConfigRef:n,optionOverridesRef:i,onErrorRef:s,setCompletedParams:f,setSuggestions:p,setActiveDropdownIndex:g}){let[c,b]=Se(!1),[u,y]=Se(null),[I,h]=Se(!1),_=Z(0),O=Z(null),m=Z(""),d=lt(async(a,l)=>{O.current?.abort();let x=new AbortController;O.current=x;let v=++_.current,K=e.current.length;r.current.some(C=>C.type!=="placeholder")||b(!0),y(null);try{let C=await ze(a,l,{maskCompletedText:o.current,signal:x.signal,apiConfig:n.current});if(v!==_.current)return;let L=je(C.data.suggestions??[],i.current);h(C.data.is_ready??!1),m.current=a;let q=C.data.input??[],M=q[q.length-1],R=e.current;if(M?.state==="in_progress"){let j=R.toLowerCase().lastIndexOf(M.text.toLowerCase());j!==-1?t.current=j:t.current=K}else t.current=K;let T=L.filter(j=>j.type!=="placeholder")[0];if(T){let j=R.slice(t.current),$=ee(T.options,j);$&&(f(B=>[...B,{id:crypto.randomUUID(),placeholder:"",type:T.type,text:$.text,kind:$.kind,suggestionType:T.type,suggestionPlaceholder:T.text,options:T.options??[],metadata:$.metadata}]),L=L.filter(B=>B!==T))}p(L),b(!1),g(-1)}catch(C){if(v===_.current){let L=C instanceof Error?C:new Error(String(C));y(L),b(!1),s.current?.(L)}}},[e,r,t,o,n,i,s,f,p,g]);return Fe(()=>(d("",[]),()=>{O.current?.abort()}),[d]),{doFetch:d,isLoading:c,error:u,isReady:I,lastRawQueryRef:m}}function Ne({text:e,completedParams:r,doFetch:t,filterBaseRef:o,skipNextFetchRef:n,suggestionsRef:i,lastRawQueryRef:s}){let f=Z(null),p=Z(null),g=Z(!0);Fe(()=>{f.current&&clearTimeout(f.current),p.current&&clearTimeout(p.current);let c=b=>{if(n.current)return n.current=!1,!1;if(!e&&r.length===0)return g.current?(t("",[]),!0):(g.current=!0,!1);let u=e.slice(o.current),h=i.current.filter(K=>K.type!=="placeholder")[0],O=(h?ie(h.options,u):[]).filter(K=>K.is_tappable),m=h?ee(h.options,u)!==null:!1,d=u.trim().length>0;if(O.length>0&&!m&&d)return!1;let{rawQuery:a,completedParams:l}=X(e,r),x=a.length<s.current.length,v=Math.abs(a.length-s.current.length);return x||v>=b?(t(a,l),!0):!1};return f.current=setTimeout(()=>{c(pt)&&p.current&&clearTimeout(p.current)},ct),p.current=setTimeout(()=>c(1),ut),()=>{f.current&&clearTimeout(f.current),p.current&&clearTimeout(p.current)}},[e,r,t,o,n,i,s])}import{useCallback as $e}from"react";function Be({activeDropdownIndex:e,setActiveDropdownIndex:r,filteredOptions:t,selectOption:o,onSubmitRef:n,text:i,completedParams:s,isDropdownOpen:f,hasPlaceholder:p,placeholderText:g,suggestions:c,filterBaseRef:b,columns:u,setText:y,setCompletedParams:I,setSuggestions:h}){let _=$e(()=>{let m=t.map((a,l)=>a.is_tappable?l:-1).filter(a=>a!==-1),d=Array.from({length:u},()=>[]);for(let a of m)d[a%u].push(a);return d.flat()},[t,u]);return{handleKeyDown:$e(m=>{let d=_();switch(m.key){case"ArrowDown":{if(m.preventDefault(),d.length===0)return;let a=d.indexOf(e),l=a<d.length-1?a+1:0;r(d[l]);break}case"ArrowUp":{if(m.preventDefault(),d.length===0)return;let a=d.indexOf(e),l=a>0?a-1:d.length-1;r(d[l]);break}case"ArrowRight":{if(e<0)break;if(e%u<u-1){let l=e+1;l<t.length&&t[l]?.is_tappable&&(m.preventDefault(),r(l))}break}case"ArrowLeft":{if(e<0)break;if(e%u>0){let a=e-1;a>=0&&t[a]?.is_tappable&&(m.preventDefault(),r(a))}break}case"Enter":{if(m.preventDefault(),e>=0&&t[e]?.is_tappable)o(t[e]);else if(n.current){let{rawQuery:a,completedParams:l}=X(i,s),x={query:i.trim(),raw_query:a,completed_params:l};n.current(x)}break}case"Tab":{if(e>=0&&t[e]?.is_tappable)m.preventDefault(),o(t[e]);else if(f){let a=t.find(l=>l.is_tappable);a&&(m.preventDefault(),o(a))}else if(!i&&p){m.preventDefault();let a=c.find(l=>l.type==="placeholder");a?(y(g),b.current=g.length,I(l=>[...l,{id:crypto.randomUUID(),placeholder:"",type:a.type,text:g,kind:null,suggestionType:a.type,suggestionPlaceholder:a.text,options:a.options??[]}]),h(l=>l.filter(x=>x!==a))):(y(g),b.current=g.length)}break}case"Escape":r(-1);break}},[e,u,s,t,b,_,p,f,n,g,o,r,I,h,y,c,i]),getTappableIndices:_}}import{useCallback as xe}from"react";function Qe({completedParams:e,suggestions:r,setCompletedParams:t,setSuggestions:o,setActiveDropdownIndex:n,filterBaseRef:i,pillTappedRef:s}){let f=xe(c=>{let b=r.filter(h=>h.type!=="placeholder");if(c<0||c>=b.length)return;let u=b[c],y=b.filter((h,_)=>_!==c),I=r.filter(h=>h.type==="placeholder");o([...I,u,...y]),s.current=!0,n(-1)},[r,o,n,s]),p=xe(()=>{if(e.length===0)return;let c=e[e.length-1],b={type:c.suggestionType,text:c.suggestionPlaceholder,required:!0,options:c.options};t(u=>u.slice(0,-1)),o(u=>[b,...u]),n(-1)},[e,t,o,n]),g=xe(c=>{let b={type:c.suggestionType,text:c.suggestionPlaceholder,required:!0,options:c.options};return{apply:u=>{u(y=>{let I=0;for(let h of e){let _=y.indexOf(h.text,I);if(_!==-1){if(h.id===c.id){let O=y.slice(0,_),m=y.slice(_+c.text.length),d=(O+m).replace(/ {2,}/g," ");return i.current=Math.min(i.current,d.length),d}I=_+h.text.length}}return y}),t(y=>y.filter(I=>I.id!==c.id)),o(y=>[b,...y]),n(-1),s.current=!0}}},[e,t,o,n,i,s]);return{setActivePill:f,removeLastParam:p,reEditParam:g}}var dt=0;function mt(){let e=k(null);return e.current===null&&(e.current=`:ac-${++dt}:`),e.current}function Ae({onSubmit:e,onError:r,optionOverrides:t,maskCompletedText:o,placeholder:n,apiConfig:i,columns:s=2,eagerSuggestions:f=!0,value:p,completedParams:g,onChange:c,onParamsChange:b}){let u=p!==void 0,y=g!==void 0,[I,h]=se(""),[_,O]=se([]),[m,d]=se([]),[a,l]=se(-1),x=u?p:I,v=y?g:_,K=k(e);K.current=e;let z=k(c);z.current=c;let C=k(b);C.current=b;let L=k(p);L.current=p;let q=k(g);q.current=g;let M=J(S=>{if(typeof S=="function")if(u){let P=S(L.current??"");z.current?.(P)}else h(P=>{let N=S(P);return z.current?.(N),N});else u||h(S),z.current?.(S)},[u]),R=J(S=>{if(typeof S=="function")if(y){let P=S(q.current??[]);C.current?.(P)}else O(P=>{let N=S(P);return C.current?.(N),N});else y||O(S),C.current?.(S)},[y]),U=k(r);U.current=r;let T=k(t);T.current=t;let j=k(o);j.current=o;let $=k(i);$.current=i;let B=k(x);B.current=x;let le=k(m);le.current=m;let D=k(0),te=k(!1),_e=k(!1),ce=mt(),{doFetch:ue,isLoading:Pe,error:We,isReady:Ve,lastRawQueryRef:pe}=qe({textRef:B,suggestionsRef:le,filterBaseRef:D,maskCompletedTextRef:j,apiConfigRef:$,optionOverridesRef:T,onErrorRef:U,setCompletedParams:R,setSuggestions:d,setActiveDropdownIndex:l});Ne({text:x,completedParams:v,doFetch:ue,filterBaseRef:D,skipNextFetchRef:_e,suggestionsRef:le,lastRawQueryRef:pe});let Ge=ae(()=>Ue(x,v),[x,v]);D.current=Math.min(D.current,x.length);let de=x.slice(D.current),me=ae(()=>m.filter(P=>P.type==="placeholder").map(P=>P.text).join(" ")||n||"",[m,n]),oe=ae(()=>m.filter(S=>S.type!=="placeholder"),[m]),A=oe[0],ve=A?t?.[A.type]:void 0,Ce=A?ve?ve(de.trim()):A.options??[]:[],ge=ae(()=>ie(Ce,de),[Ce,de]),Ie=me.length>0,fe=!Pe&&ge.length>0&&(!!x||te.current||!Ie),we=J(S=>{if(!A)return;let P={id:crypto.randomUUID(),placeholder:"",type:A.type,text:S.text,kind:S.kind,suggestionType:A.type,suggestionPlaceholder:A.text,options:A.options??[],metadata:S.metadata},N=D.current,E=B.current.slice(0,N);if(E.length>0&&!E.endsWith(" ")){let w=E.split(/\s+/).pop()??"";w&&S.text.toLowerCase().startsWith(w.toLowerCase())&&(E=E.slice(0,E.length-w.length))}let be=E.length>0&&E[E.length-1]!==" ",V=E+(be?" ":"")+S.text+" ";M(V),D.current=V.length,R(w=>[...w,P]),te.current=!1,l(-1);let Q=oe.length-1;f&&Q>0?(d(w=>w.filter(H=>H!==A)),_e.current=!0):d(w=>w.filter(H=>H.type==="placeholder"))},[A,oe,f,M,R]),Xe=J(S=>{let P=S.target.value,E=P.length>0&&!S.nativeEvent?.isComposing&&P[0]!==P[0].toUpperCase()?P[0].toUpperCase()+P.slice(1):P;M(E),te.current=!1,l(-1);let{valid:be,invalid:V}=Ke(E,v);if(V.length>0){R(()=>be);for(let Q of V)d(w=>[{type:Q.suggestionType,text:Q.suggestionPlaceholder,required:!0,options:Q.options},...w])}if(A&&V.length===0){let Q=E.slice(D.current),w=ee(A.options,Q);w&&(R(H=>[...H,{id:crypto.randomUUID(),placeholder:"",type:A.type,text:w.text,kind:w.kind,suggestionType:A.type,suggestionPlaceholder:A.text,options:A.options??[],metadata:w.metadata}]),d(H=>H.filter(tt=>tt!==A)))}},[v,A,M,R]),{handleKeyDown:Ze}=Be({activeDropdownIndex:a,setActiveDropdownIndex:l,filteredOptions:ge,selectOption:we,onSubmitRef:K,text:x,completedParams:v,isDropdownOpen:fe,hasPlaceholder:Ie,placeholderText:me,suggestions:m,filterBaseRef:D,columns:s,setText:M,setCompletedParams:R,setSuggestions:d}),ne=Qe({completedParams:v,suggestions:m,setCompletedParams:R,setSuggestions:d,setActiveDropdownIndex:l,filterBaseRef:D,pillTappedRef:te}),Je=J(S=>{ne.reEditParam(S).apply(M)},[ne,M]),Ye=J(()=>{M(""),R(()=>[]),d([]),l(-1),D.current=0,pe.current="",ue("",[])},[ue,M,R,pe]),et=a>=0?`${ce}-option-${a}`:void 0;return{completedParams:v,suggestionPills:oe,setActivePill:ne.setActivePill,removeLastParam:ne.removeLastParam,reEditParam:Je,segments:Ge,suggestions:m,activeIndex:a,isReady:Ve,isLoading:Pe,error:We,inputProps:{value:x,placeholder:me||void 0,onChange:Xe,onKeyDown:Ze,role:"combobox","aria-expanded":fe,"aria-activedescendant":et,"aria-autocomplete":"list","aria-controls":ce},reset:Ye,dropdownProps:{suggestions:A?[{...A,options:ge}]:[],activeIndex:a,onSelect:we,onHighlight:l,isOpen:fe,id:ce}}}import{jsx as W,jsxs as Y}from"react/jsx-runtime";var yt=gt(function({onSubmit:r,onError:t,optionOverrides:o,maskCompletedText:n,placeholder:i,className:s,apiConfig:f,columns:p,eagerSuggestions:g,value:c,completedParams:b,onChange:u,onParamsChange:y},I){let h=He(null),_=He(()=>{});bt(()=>{h.current?.focus()},[]);let{completedParams:O,suggestionPills:m,setActivePill:d,segments:a,inputProps:l,dropdownProps:x,reset:v}=Ae({onSubmit:U=>_.current(U),onError:t,optionOverrides:o,maskCompletedText:n,placeholder:i,apiConfig:f,columns:p,eagerSuggestions:g,value:c,completedParams:b,onChange:u,onParamsChange:y});ht(I,()=>({focus:()=>h.current?.focus(),reset:v}),[v]);let K=()=>{h.current?.focus()},z=!!l.value||O.length>0,C=ft(()=>{if(!z)return;let{rawQuery:U,completedParams:T}=X(l.value,O);r({query:l.value.trim(),raw_query:U,completed_params:T}),v()},[z,l.value,O,r,v]);_.current=C;let{onChange:L,placeholder:q,...M}=l,R=!l.value;return Y("div",{className:`${F.container} ${s??""}`,children:[W(ye,{...x}),Y("div",{className:F.inputWrapper,onClick:K,children:[Y("div",{className:F.editorArea,children:[Y("div",{className:F.sizerContent,"aria-hidden":"true",children:[R&&q?Y("span",{className:F.placeholderText,children:[q," "]}):Y("span",{className:F.sizerText,children:[a.map((U,T)=>W("span",{children:U.value},`${T}-${U.type}`)),a.length===0&&"\xA0"]})," ",W(Le,{pills:m,activePillIndex:0,onSelectPill:d})]}),W("textarea",{ref:h,className:F.textarea,rows:1,onChange:L,...M})]}),W("button",{type:"button",className:F.submitButton,disabled:!z,onClick:U=>{U.stopPropagation(),C()},"aria-label":"Submit",children:W("svg",{width:"18",height:"18",viewBox:"0 0 18 18",fill:"none",role:"img","aria-label":"Submit",children:W("path",{d:"M9 14V4M9 4L4 9M9 4L14 9",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})]})]})});export{yt as AIAutocomplete,ye as AIAutocompleteDropdown,Ae as useAIAutocomplete};
215
+ `,document.head.appendChild(e)}var se={list:"PillList-module_list_qvLqO",pill:"PillList-module_pill_osSyz",fadeIn:"PillList-module_fadeIn_Aezob",active:"PillList-module_active_Oll--"};import{jsx as Ue}from"react/jsx-runtime";function at(e){return e===0?.4:e===1?.3:.15}function De({pills:e,activePillIndex:r,onSelectPill:t}){return Ue("span",{className:se.list,children:e.map((o,n)=>Ue("button",{type:"button",className:`${se.pill} ${n===r?se.active:""}`,style:{opacity:at(n)},onClick:()=>t(n),children:o.text},`${o.type}-${o.text}`))})}import{useCallback as ee,useMemo as ce,useRef as O,useState as ue}from"react";function Z(e,r,t){let o=e.slice(r);if(t||r===0||e[r-1]===" ")return o;let n=o.indexOf(" ");return n===-1?"":o.slice(n+1)}function le(e,r){if(!e)return[];let t=r.trimStart();if(!t)return e;let o=t.toLowerCase();return e.filter(n=>!n.is_tappable||n.text.toLowerCase().includes(o))}function ie(e,r){if(!e)return null;let t=r.trim();if(!t)return null;let o=t.toLowerCase();return e.find(n=>n.is_tappable&&n.text.toLowerCase()===o)??null}function Ke(e,r){let t=[],o=0;for(let i of r){let s=e.indexOf(i.text,o);s!==-1&&(s>o&&t.push({type:"text",value:e.slice(o,s)}),t.push({type:"completed",value:i.text,param:i}),o=s+i.text.length)}let n=e.slice(o);return n&&t.push({type:"text",value:n}),t}function je(e,r){let t=[],o=[],n=0;for(let i of r){let s=e.indexOf(i.text,n);s===-1?o.push(i):(t.push(i),n=s+i.text.length)}return{valid:t,invalid:o}}import{useCallback as ut,useEffect as Ne,useRef as Y,useState as Ae}from"react";var st="0.1.14",ze=!1;function lt(){return crypto.randomUUID()}function ct(e,r){return{placeholder:e.placeholder,type:e.type,...r&&{text:e.text},kind:e.kind}}async function Fe(e,r,t){let o=t?.apiConfig,n=o?.apiKey??"";!n&&!ze&&(ze=!0,console.warn("[AIAutocomplete] No apiKey in apiConfig. Requests will be sent without an Authorization header."));let i=o?.authScheme??"Bearer",s=!t?.maskCompletedText,b=r.find(m=>m.type==="contact"&&m.metadata?.contact_account_count)?.metadata?.contact_account_count,d=typeof b=="number"?b:void 0,c={data:{raw_query:e,completed_params:r.map(m=>ct(m,s)),...d!=null&&{contact_account_count:d}},meta:{request_id:lt(),request_at:new Date().toISOString(),language:typeof navigator<"u"?navigator.language:"en-US",client_version:st}},u={"Content-Type":"application/json",...o?.appIdentifier&&{"X-App-Identifier":o.appIdentifier},...o?.headers};n&&(u.Authorization=i==="Basic"?`Basic ${btoa(n)}`:`Bearer ${n}`);let h=await fetch(o?.endpoint??"/ac/suggest",{method:"POST",headers:u,body:JSON.stringify(c),signal:t?.signal});if(!h.ok)throw new Error(`API error: ${h.status} ${h.statusText}`);return h.json()}function J(e,r){let t=e,o={},n=[];for(let i of r){let s=(o[i.type]??0)+1;o[i.type]=s;let d=`{{${i.type.toUpperCase().replace(/\s+/g,"_")}_${s}}}`,c=t.indexOf(i.text);c!==-1&&(t=t.slice(0,c)+d+t.slice(c+i.text.length)),n.push({...i,placeholder:d})}return{rawQuery:t,completedParams:n}}function qe(e,r){return r?e.map(t=>{let o=r[t.type];if(!o)return t;let n=o("");if(n.length===0)return t;let i=new Set(n.map(b=>b.text)),s=(t.options??[]).filter(b=>!i.has(b.text));return{...t,options:[...n,...s]}}):e}var pt=100,dt=300,mt=2;function Qe({textRef:e,suggestionsRef:r,filterBaseRef:t,filterInProgressRef:o,maskCompletedTextRef:n,apiConfigRef:i,optionOverridesRef:s,onErrorRef:b,setCompletedParams:d,setSuggestions:c,setActiveDropdownIndex:u}){let[h,m]=Ae(!1),[f,A]=Ae(null),[S,_]=Ae(!1),I=Y(0),x=Y(null),C=Y(""),g=ut(async(p,a)=>{x.current?.abort();let l=new AbortController;x.current=l;let M=++I.current,R=e.current.length;r.current.some(E=>E.type!=="placeholder")||m(!0),A(null);try{let E=await Fe(p,a,{maskCompletedText:n.current,signal:l.signal,apiConfig:i.current});if(M!==I.current)return;let D=qe(E.data.suggestions??[],s.current);_(E.data.is_ready??!1),C.current=p;let T=E.data.input??[],k=T[T.length-1],L=e.current;if(k?.state==="in_progress"){o.current=!0;let z=L.toLowerCase().lastIndexOf(k.text.toLowerCase());z!==-1?t.current=z:t.current=R}else o.current=!1,t.current=R;let j=D.filter(z=>z.type!=="placeholder")[0];if(j){let z=Z(L,t.current,o.current),N=ie(j.options,z);N&&(d(B=>[...B,{id:crypto.randomUUID(),placeholder:"",type:j.type,text:N.text,kind:N.kind,suggestionType:j.type,suggestionPlaceholder:j.text,options:j.options??[],metadata:N.metadata}]),D=D.filter(B=>B!==j))}c(D),m(!1),u(-1)}catch(E){if(M===I.current){let D=E instanceof Error?E:new Error(String(E));A(D),m(!1),b.current?.(D)}}},[e,r,t,o,n,i,s,b,d,c,u]);return Ne(()=>(g("",[]),()=>{x.current?.abort()}),[g]),{doFetch:g,isLoading:h,error:f,isReady:S,lastRawQueryRef:C}}function $e({text:e,completedParams:r,doFetch:t,filterBaseRef:o,filterInProgressRef:n,skipNextFetchRef:i,suggestionsRef:s,lastRawQueryRef:b}){let d=Y(null),c=Y(null),u=Y(!0);Ne(()=>{d.current&&clearTimeout(d.current),c.current&&clearTimeout(c.current);let h=m=>{if(i.current)return i.current=!1,!1;if(!e&&r.length===0)return u.current?(t("",[]),!0):(u.current=!0,!1);let f=Z(e,o.current,n.current),_=s.current.filter(R=>R.type!=="placeholder")[0],x=(_?le(_.options,f):[]).filter(R=>R.is_tappable),C=_?ie(_.options,f)!==null:!1,g=f.trim().length>0;if(x.length>0&&!C&&g)return!1;let{rawQuery:p,completedParams:a}=J(e,r),l=p.length<b.current.length,M=Math.abs(p.length-b.current.length);return l||M>=m?(t(p,a),!0):!1};return d.current=setTimeout(()=>{h(mt)&&c.current&&clearTimeout(c.current)},pt),c.current=setTimeout(()=>h(1),dt),()=>{d.current&&clearTimeout(d.current),c.current&&clearTimeout(c.current)}},[e,r,t,o,n,i,s,b])}import{useCallback as Be}from"react";function He({activeDropdownIndex:e,setActiveDropdownIndex:r,filteredOptions:t,selectOption:o,onSubmitRef:n,text:i,completedParams:s,isDropdownOpen:b,hasPlaceholder:d,placeholderText:c,suggestions:u,actionableSuggestions:h,setActivePill:m,filterBaseRef:f,columns:A,setText:S,setCompletedParams:_,setSuggestions:I}){let x=Be(()=>{let g=t.map((a,l)=>a.is_tappable?l:-1).filter(a=>a!==-1),p=Array.from({length:A},()=>[]);for(let a of g)p[a%A].push(a);return p.flat()},[t,A]);return{handleKeyDown:Be(g=>{let p=x();switch(g.key){case"ArrowDown":{if(g.preventDefault(),p.length===0)return;let a=p.indexOf(e),l=a<p.length-1?a+1:0;r(p[l]);break}case"ArrowUp":{if(g.preventDefault(),p.length===0)return;let a=p.indexOf(e),l=a>0?a-1:p.length-1;r(p[l]);break}case"ArrowRight":{if(e<0){let l=g.target;l.selectionStart!=null&&l.selectionStart===l.value.length&&h.length>1&&(g.preventDefault(),m(1));break}if(e%A<A-1){let l=e+1;l<t.length&&t[l]?.is_tappable&&(g.preventDefault(),r(l))}break}case"ArrowLeft":{if(e<0)break;if(e%A>0){let a=e-1;a>=0&&t[a]?.is_tappable&&(g.preventDefault(),r(a))}break}case"Enter":{if(g.preventDefault(),e>=0&&t[e]?.is_tappable)o(t[e]);else if(n.current){let{rawQuery:a,completedParams:l}=J(i,s),M={query:i.trim(),raw_query:a,completed_params:l};n.current(M)}break}case"Tab":{if(e>=0&&t[e]?.is_tappable)g.preventDefault(),o(t[e]);else if(b){let a=t.find(l=>l.is_tappable);a&&(g.preventDefault(),o(a))}else if(!i&&d){g.preventDefault();let a=u.find(l=>l.type==="placeholder");a?(S(c),f.current=c.length,_(l=>[...l,{id:crypto.randomUUID(),placeholder:"",type:a.type,text:c,kind:null,suggestionType:a.type,suggestionPlaceholder:a.text,options:a.options??[]}]),I(l=>l.filter(M=>M!==a))):(S(c),f.current=c.length)}break}case"Escape":r(-1);break}},[h,e,A,s,t,f,x,d,b,n,c,o,r,m,_,I,S,u,i]),getTappableIndices:x}}import{useCallback as _e}from"react";function We({completedParams:e,suggestions:r,setCompletedParams:t,setSuggestions:o,setActiveDropdownIndex:n,filterBaseRef:i,pillTappedRef:s}){let b=_e(u=>{let h=r.filter(S=>S.type!=="placeholder");if(u<0||u>=h.length)return;let m=h[u],f=h.filter((S,_)=>_!==u),A=r.filter(S=>S.type==="placeholder");o([...A,m,...f]),s.current=!0,n(-1)},[r,o,n,s]),d=_e(()=>{if(e.length===0)return;let u=e[e.length-1],h={type:u.suggestionType,text:u.suggestionPlaceholder,required:!0,options:u.options};t(m=>m.slice(0,-1)),o(m=>[h,...m]),n(-1)},[e,t,o,n]),c=_e(u=>{let h={type:u.suggestionType,text:u.suggestionPlaceholder,required:!0,options:u.options};return{apply:m=>{m(f=>{let A=0;for(let S of e){let _=f.indexOf(S.text,A);if(_!==-1){if(S.id===u.id){let I=f.slice(0,_),x=f.slice(_+u.text.length),C=(I+x).replace(/ {2,}/g," ");return i.current=Math.min(i.current,C.length),C}A=_+S.text.length}}return f}),t(f=>f.filter(A=>A.id!==u.id)),o(f=>[h,...f]),n(-1),s.current=!0}}},[e,t,o,n,i,s]);return{setActivePill:b,removeLastParam:d,reEditParam:c}}var gt=0;function ft(){let e=O(null);return e.current===null&&(e.current=`:ac-${++gt}:`),e.current}function ve({onSubmit:e,onError:r,optionOverrides:t,maskCompletedText:o,placeholder:n,apiConfig:i,columns:s=2,eagerSuggestions:b=!0,value:d,completedParams:c,onChange:u,onParamsChange:h}){let m=d!==void 0,f=c!==void 0,[A,S]=ue(""),[_,I]=ue([]),[x,C]=ue([]),[g,p]=ue(-1),a=m?d:A,l=f?c:_,M=O(e);M.current=e;let R=O(u);R.current=u;let q=O(h);q.current=h;let E=O(d);E.current=d;let D=O(c);D.current=c;let T=ee(y=>{if(typeof y=="function")if(m){let P=y(E.current??"");R.current?.(P)}else S(P=>{let Q=y(P);return R.current?.(Q),Q});else m||S(y),R.current?.(y)},[m]),k=ee(y=>{if(typeof y=="function")if(f){let P=y(D.current??[]);q.current?.(P)}else I(P=>{let Q=y(P);return q.current?.(Q),Q});else f||I(y),q.current?.(y)},[f]),L=O(r);L.current=r;let $=O(t);$.current=t;let j=O(o);j.current=o;let z=O(i);z.current=i;let N=O(a);N.current=a;let B=O(x);B.current=x;let K=O(0),oe=O(!1),ae=O(!1),Pe=O(!1),pe=ft(),{doFetch:de,isLoading:Ce,error:Ge,isReady:Xe,lastRawQueryRef:me}=Qe({textRef:N,suggestionsRef:B,filterBaseRef:K,filterInProgressRef:oe,maskCompletedTextRef:j,apiConfigRef:z,optionOverridesRef:$,onErrorRef:L,setCompletedParams:k,setSuggestions:C,setActiveDropdownIndex:p});$e({text:a,completedParams:l,doFetch:de,filterBaseRef:K,filterInProgressRef:oe,skipNextFetchRef:Pe,suggestionsRef:B,lastRawQueryRef:me});let Ze=ce(()=>Ke(a,l),[a,l]);K.current=Math.min(K.current,a.length);let ge=Z(a,K.current,oe.current),fe=ce(()=>x.filter(P=>P.type==="placeholder").map(P=>P.text).join(" ")||n||"",[x,n]),ne=ce(()=>x.filter(y=>y.type!=="placeholder"),[x]),v=ne[0],Ie=v?t?.[v.type]:void 0,we=v?Ie?Ie(ge.trim()):v.options??[]:[],be=ce(()=>le(we,ge),[we,ge]),Oe=fe.length>0,he=!Ce&&be.length>0&&(!!a||ae.current||!Oe),Re=ee(y=>{if(!v)return;let P={id:crypto.randomUUID(),placeholder:"",type:v.type,text:y.text,kind:y.kind,suggestionType:v.type,suggestionPlaceholder:v.text,options:v.options??[],metadata:y.metadata},Q=K.current,U=N.current.slice(0,Q);if(U.length>0&&!U.endsWith(" ")){let w=U.split(/\s+/).pop()??"";w&&y.text.toLowerCase().startsWith(w.toLowerCase())&&(U=U.slice(0,U.length-w.length))}let ye=U.length>0&&U[U.length-1]!==" ",G=U+(ye?" ":"")+y.text+" ";T(G),K.current=G.length,k(w=>[...w,P]),ae.current=!1,p(-1);let H=ne.length-1;b&&H>0?(C(w=>w.filter(W=>W!==v)),Pe.current=!0):C(w=>w.filter(W=>W.type==="placeholder"))},[v,ne,b,T,k]),Je=ee(y=>{let P=y.target.value,U=P.length>0&&!y.nativeEvent?.isComposing&&P[0]!==P[0].toUpperCase()?P[0].toUpperCase()+P.slice(1):P;T(U),ae.current=!1,p(-1);let{valid:ye,invalid:G}=je(U,l);if(G.length>0){k(()=>ye);for(let H of G)C(w=>[{type:H.suggestionType,text:H.suggestionPlaceholder,required:!0,options:H.options},...w])}if(v&&G.length===0){let H=Z(U,K.current,oe.current),w=ie(v.options,H);w&&(k(W=>[...W,{id:crypto.randomUUID(),placeholder:"",type:v.type,text:w.text,kind:w.kind,suggestionType:v.type,suggestionPlaceholder:v.text,options:v.options??[],metadata:w.metadata}]),C(W=>W.filter(nt=>nt!==v)))}},[l,v,T,k]),re=We({completedParams:l,suggestions:x,setCompletedParams:k,setSuggestions:C,setActiveDropdownIndex:p,filterBaseRef:K,pillTappedRef:ae}),Ye=ee(y=>{re.reEditParam(y).apply(T)},[re,T]),{handleKeyDown:et}=He({activeDropdownIndex:g,setActiveDropdownIndex:p,filteredOptions:be,selectOption:Re,onSubmitRef:M,text:a,completedParams:l,isDropdownOpen:he,hasPlaceholder:Oe,placeholderText:fe,suggestions:x,actionableSuggestions:ne,setActivePill:re.setActivePill,filterBaseRef:K,columns:s,setText:T,setCompletedParams:k,setSuggestions:C}),tt=ee(()=>{T(""),k(()=>[]),C([]),p(-1),K.current=0,oe.current=!1,me.current="",de("",[])},[de,T,k,me]),ot=g>=0?`${pe}-option-${g}`:void 0;return{completedParams:l,suggestionPills:ne,setActivePill:re.setActivePill,removeLastParam:re.removeLastParam,reEditParam:Ye,segments:Ze,suggestions:x,activeIndex:g,isReady:Xe,isLoading:Ce,error:Ge,inputProps:{value:a,placeholder:fe||void 0,onChange:Je,onKeyDown:et,role:"combobox","aria-expanded":he,"aria-activedescendant":ot,"aria-autocomplete":"list","aria-controls":pe},reset:tt,dropdownProps:{suggestions:v?[{...v,options:be}]:[],activeIndex:g,onSelect:Re,onHighlight:p,isOpen:he,id:pe}}}import{jsx as V,jsxs as te}from"react/jsx-runtime";var xt=bt(function({onSubmit:r,onError:t,optionOverrides:o,maskCompletedText:n,placeholder:i,className:s,apiConfig:b,columns:d,eagerSuggestions:c,value:u,completedParams:h,onChange:m,onParamsChange:f},A){let S=Ve(null),_=Ve(()=>{});yt(()=>{S.current?.focus()},[]);let{completedParams:I,suggestionPills:x,setActivePill:C,segments:g,inputProps:p,dropdownProps:a,reset:l}=ve({onSubmit:L=>_.current(L),onError:t,optionOverrides:o,maskCompletedText:n,placeholder:i,apiConfig:b,columns:d,eagerSuggestions:c,value:u,completedParams:h,onChange:m,onParamsChange:f});St(A,()=>({focus:()=>S.current?.focus(),reset:l}),[l]);let M=()=>{S.current?.focus()},R=!!p.value||I.length>0,q=ht(()=>{if(!R)return;let{rawQuery:L,completedParams:$}=J(p.value,I);r({query:p.value.trim(),raw_query:L,completed_params:$}),l()},[R,p.value,I,r,l]);_.current=q;let{onChange:E,placeholder:D,...T}=p,k=!p.value;return te("div",{className:`${F.container} ${s??""}`,children:[V(xe,{...a}),te("div",{className:F.inputWrapper,onClick:M,children:[te("div",{className:F.editorArea,children:[te("div",{className:F.sizerContent,"aria-hidden":"true",children:[k&&D?te("span",{className:F.placeholderText,children:[D," "]}):te("span",{className:F.sizerText,children:[g.map((L,$)=>V("span",{children:L.value},`${$}-${L.type}`)),g.length===0&&"\xA0"]})," ",V(De,{pills:x,activePillIndex:0,onSelectPill:C})]}),V("textarea",{ref:S,className:F.textarea,rows:1,onChange:E,...T})]}),V("button",{type:"button",className:F.submitButton,disabled:!R,onClick:L=>{L.stopPropagation(),q()},"aria-label":"Submit",children:V("svg",{width:"18",height:"18",viewBox:"0 0 18 18",fill:"none",role:"img","aria-label":"Submit",children:V("path",{d:"M9 14V4M9 4L4 9M9 4L14 9",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})]})]})});export{xt as AIAutocomplete,xe as AIAutocompleteDropdown,ve as useAIAutocomplete};
215
216
  //# sourceMappingURL=index.mjs.map