@magicx-eng/ai-autocomplete-vanilla 0.4.1 → 0.4.2
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.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -436,9 +436,9 @@ declare function buildQuery(text: string, completedParams: CompletedParamState[]
|
|
|
436
436
|
/**
|
|
437
437
|
* The dropdown footer's keyboard hint has four states, in priority order:
|
|
438
438
|
* 1. an option is highlighted → "enter to proceed" (Enter commits it);
|
|
439
|
-
* 2. otherwise, the input is empty → "tab to
|
|
440
|
-
*
|
|
441
|
-
*
|
|
439
|
+
* 2. otherwise, the input is empty → "tab to select" — whenever the hint shows
|
|
440
|
+
* the Tab key it always reads "to select". This holds regardless of whether
|
|
441
|
+
* the user has tabbed to highlight yet;
|
|
442
442
|
* 3. otherwise, the user has tabbed to highlight at some point → "→ to skip"
|
|
443
443
|
* (the right arrow skips the active pill);
|
|
444
444
|
* 4. otherwise (fresh, with text) → "tab to select".
|
package/dist/index.d.ts
CHANGED
|
@@ -436,9 +436,9 @@ declare function buildQuery(text: string, completedParams: CompletedParamState[]
|
|
|
436
436
|
/**
|
|
437
437
|
* The dropdown footer's keyboard hint has four states, in priority order:
|
|
438
438
|
* 1. an option is highlighted → "enter to proceed" (Enter commits it);
|
|
439
|
-
* 2. otherwise, the input is empty → "tab to
|
|
440
|
-
*
|
|
441
|
-
*
|
|
439
|
+
* 2. otherwise, the input is empty → "tab to select" — whenever the hint shows
|
|
440
|
+
* the Tab key it always reads "to select". This holds regardless of whether
|
|
441
|
+
* the user has tabbed to highlight yet;
|
|
442
442
|
* 3. otherwise, the user has tabbed to highlight at some point → "→ to skip"
|
|
443
443
|
* (the right arrow skips the active pill);
|
|
444
444
|
* 4. otherwise (fresh, with text) → "tab to select".
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var oe=Object.defineProperty;var Be=Object.getOwnPropertyDescriptor;var Ke=Object.getOwnPropertyNames;var Qe=Object.prototype.hasOwnProperty;var qe=(n,e)=>{for(var t in e)oe(n,t,{get:e[t],enumerable:!0})},$e=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Ke(e))!Qe.call(n,r)&&r!==t&&oe(n,r,{get:()=>e[r],enumerable:!(i=Be(e,r))||i.enumerable});return n};var Ue=n=>$e(oe({},"__esModule",{value:!0}),n);var yt={};qe(yt,{AIAutocomplete:()=>re,ModeController:()=>k,buildQuery:()=>S,createStore:()=>te,cursorIsAtEnd:()=>$,extractPlainText:()=>L,getCursorOffset:()=>C,getFooterHint:()=>z,plainTextLength:()=>q,previousGraphemeBoundary:()=>U,renderEditableContent:()=>ee,setCursorOffset:()=>P});module.exports=Ue(yt);var R=class{constructor(e){this.config=e;this.current=null;this.expiresAt=null;this.inFlightRefresh=null;e.accessToken&&(this.current=e.accessToken)}async getToken(e=!1){if(!e&&this.current&&!this.isExpired())return this.current;if(!e&&this.inFlightRefresh)return this.inFlightRefresh;this.inFlightRefresh=this.refresh();try{return await this.inFlightRefresh}finally{this.inFlightRefresh=null}}async refresh(){let e=await this.config.getAccessToken();return this.current=e.accessToken,this.expiresAt=e.expiresAt??null,this.current}isExpired(){return this.expiresAt==null?!1:Date.now()>=this.expiresAt-3e4}};var Ge="https://api.ai-autocomplete.com",N=`${Ge}/api/suggest`,pe=new WeakMap;function M(n){return n?.type==="accessToken"}function We(n){if(!(!n||M(n)))return n}function _(n){let e=pe.get(n.getAccessToken);return e||(e=new R(n),pe.set(n.getAccessToken,e)),e}function H(n){return{"Content-Type":"application/json",...n?.appIdentifier&&{"X-App-Identifier":n.appIdentifier},...n?.headers}}function F(n){let e=We(n),t=e?.apiKey;return t?(e?.authScheme??"Bearer")==="Basic"?`Basic ${btoa(t)}`:`Bearer ${t}`:null}var je="0.4.1",ue=!1;function Ve(){return crypto.randomUUID()}function ze(n,e){return{placeholder:n.placeholder,type:n.type,...e&&{text:n.text},kind:n.kind}}function Xe(n,e,t,i){let r=e.find(o=>o.type==="contact"&&o.metadata?.contact_account_count)?.metadata?.contact_account_count,s=typeof r=="number"?r:void 0;return{data:{raw_query:n,completed_params:e.map(o=>ze(o,t)),...s!=null&&{contact_account_count:s}},meta:{request_id:Ve(),request_at:new Date().toISOString(),language:typeof navigator<"u"?navigator.language:"en-US",client_version:je,session_id:i}}}async function me(n,e,t,i,r){return fetch(n,{method:"POST",headers:{...e,Authorization:`Bearer ${t}`},body:i,signal:r})}async function ge(n,e,t){let i=t.apiConfig,r=!t.maskCompletedText,s=Xe(n,e,r,t.sessionId),o=H(i),a=i?.endpoint??N,d=JSON.stringify(s);if(M(i)){let c=_(i),u=await c.getToken(),h=await me(a,o,u,d,t.signal);if(h.status===401){let b=await c.getToken(!0);h=await me(a,o,b,d,t.signal)}if(!h.ok)throw new Error(`API error: ${h.status} ${h.statusText}`);return h.json()}let l=F(i);!l&&!ue&&(ue=!0,console.warn("[AIAutocomplete] No apiKey in apiConfig. Requests will be sent without an Authorization header.")),l&&(o.Authorization=l);let p=await fetch(a,{method:"POST",headers:o,body:d,signal:t.signal});if(!p.ok)throw new Error(`API error: ${p.status} ${p.statusText}`);return p.json()}function S(n,e){let t=n,i={},r=[];for(let s of e){let o=(i[s.type]??0)+1;i[s.type]=o;let d=`{{${s.type.toUpperCase().replace(/\s+/g,"_")}_${o}}}`,l=t.indexOf(s.text);l!==-1&&(t=t.slice(0,l)+d+t.slice(l+s.text.length)),r.push({...s,placeholder:d})}return{rawQuery:t,completedParams:r}}function I(n,e,t){return e>0||!t?e:n.toLowerCase().startsWith(t.toLowerCase())?t.length:e}function E(n,e,t){let i=n.slice(e);if(t||e===0||n[e-1]===" ")return i;let r=i.indexOf(" ");return r===-1?"":i.slice(r+1)}function fe(n,e){let t=n.trimEnd().replace(/\s+/g," ");if(t.length===0||e.length===0)return 0;let i=t.split(" "),r=e.toLowerCase();for(let s=0;s<i.length;s++){let o=i.slice(s).join(" ");if(r.startsWith(o.toLowerCase())){let a=t.length-o.length;return n.length-a}}return 0}function w(n,e){if(!n)return[];let t=e.trimStart();if(!t)return n;let i=t.toLowerCase();return n.filter(r=>!r.is_tappable||r.text.toLowerCase().includes(i))}function O(n,e){if(!n)return null;let t=e.trim();if(!t)return null;let i=t.toLowerCase();return n.find(r=>r.is_tappable&&r.text.toLowerCase()===i)??null}function he(n,e){return e?n.map(t=>{let i=e[t.type];if(!i)return t;let r=i("");if(r.length===0)return t;let s=new Set(r.map(a=>a.text)),o=(t.options??[]).filter(a=>!s.has(a.text));return{...t,options:[...r,...o]}}):n}var Ye=100,Je=300,Ze=2,B=class{constructor(e,t,i,r,s,o,a={}){this.store=e;this.getApiConfig=t;this.getOptionOverrides=i;this.getMaskCompletedText=r;this.getOnError=s;this.getSessionId=o;this.callbacks=a;this.fetchVersion=0;this.abortController=null;this.debounceTimer=null;this.slowDebounceTimer=null;this.unsubscribe=null}start(){this.doFetch("",[]);let e=this.store.get().text,t=this.store.get().completedParams;this.unsubscribe=this.store.subscribe(i=>{(i.text!==e||i.completedParams!==t)&&(e=i.text,t=i.completedParams,this.scheduleFetch())})}dispose(){this.abortController?.abort(),this.clearTimers(),this.unsubscribe?.()}async doFetch(e,t){this.abortController?.abort();let i=new AbortController;this.abortController=i;let r=++this.fetchVersion,s=this.store.get().text.length;this.store.set({isLoading:!0,error:null});try{let o=await ge(e,t,{sessionId:this.getSessionId(),maskCompletedText:this.getMaskCompletedText(),signal:i.signal,apiConfig:this.getApiConfig()});if(r!==this.fetchVersion)return;let a=he(o.data.suggestions??[],this.getOptionOverrides()),d=o.data.input??[],l=d[d.length-1],p=this.store.get().text,c,u;if(l?.state==="in_progress"){u=!0;let x=p.toLowerCase().lastIndexOf(l.text.toLowerCase());c=x!==-1?x:s}else u=!1,c=s;let b=a.filter(x=>x.type!=="placeholder")[0],g=null;if(b){let x=E(p,c,u),m=O(b.options,x);m&&(g={id:crypto.randomUUID(),placeholder:"",type:b.type,text:m.text,kind:m.kind,suggestionType:b.type,suggestionPlaceholder:b.text,options:b.options??[],metadata:m.metadata},a=a.filter(f=>f!==b),this.callbacks.onAutoMatch?.({active:b,matched:m,rawQuery:e}))}this.store.set(x=>({suggestions:a,isLoading:!1,isReady:o.data.is_ready??!1,lastRawQuery:e,activeDropdownIndex:-1,filterBase:c,filterInProgress:u,...g?{completedParams:[...x.completedParams,g]}:{}}))}catch(o){if(r===this.fetchVersion){let a=o instanceof Error?o:new Error(String(o));this.store.set({error:a,isLoading:!1}),this.getOnError()?.(a)}}}scheduleFetch(){if(this.clearTimers(),this.store.get().skipNextFetch){this.store.set({skipNextFetch:!1});return}let t=i=>{let r=this.store.get();if(!r.text&&r.completedParams.length===0)return this.doFetch("",[]),!0;let s=r.suggestions.filter(f=>f.type==="placeholder").map(f=>f.text).join(" "),o=I(r.text,r.filterBase,s),a=E(r.text,o,r.filterInProgress),l=r.suggestions.filter(f=>f.type!=="placeholder")[0],c=(l?w(l.options,a):[]).filter(f=>f.is_tappable),u=l?O(l.options,a)!==null:!1,h=a.trim().length>0;if(c.length>0&&!u&&h||r.completedParams.length===0&&r.text.length>0&&s.length>0&&s.toLowerCase().startsWith(r.text.toLowerCase()))return!1;let{rawQuery:b,completedParams:g}=S(r.text,r.completedParams),x=b.length<r.lastRawQuery.length,m=Math.abs(b.length-r.lastRawQuery.length);return x||m>=i?(this.doFetch(b,g),!0):!1};this.debounceTimer=setTimeout(()=>{t(Ze)&&this.slowDebounceTimer&&clearTimeout(this.slowDebounceTimer)},Ye),this.slowDebounceTimer=setTimeout(()=>t(1),Je)}clearTimers(){this.debounceTimer&&clearTimeout(this.debounceTimer),this.slowDebounceTimer&&clearTimeout(this.slowDebounceTimer),this.debounceTimer=null,this.slowDebounceTimer=null}};var xe='[contenteditable="false"]',D;function et(){if(D!==void 0)return D;let n=globalThis.Intl.Segmenter;if(!n)return D=null,null;try{D=new n(void 0,{granularity:"grapheme"})}catch{D=null}return D??null}function K(n,e){let t=n;for(;t&&t!==e;){if(t.nodeType===Node.ELEMENT_NODE&&t.matches(xe))return!0;t=t.parentNode}return!1}function Q(n){return(n.ownerDocument??document).createTreeWalker(n,NodeFilter.SHOW_TEXT,{acceptNode(e){return K(e,n)?NodeFilter.FILTER_REJECT:NodeFilter.FILTER_ACCEPT}})}function L(n){let e=Q(n),t="",i=e.nextNode();for(;i;)t+=i.data,i=e.nextNode();return t}function q(n){let e=Q(n),t=0,i=e.nextNode();for(;i;)t+=i.data.length,i=e.nextNode();return t}function C(n){let e=(n.ownerDocument??document).getSelection();if(!e||e.rangeCount===0)return null;let t=e.anchorNode,i=e.anchorOffset;if(!t||!n.contains(t))return null;if(t.nodeType===Node.ELEMENT_NODE){let r=t;if(K(r,n)&&r!==n)return null;let s=0;for(let o=0;o<i&&o<r.childNodes.length;o++)s+=ye(r.childNodes[o],n);return s+be(r,n)}return t.nodeType!==Node.TEXT_NODE||K(t,n)?null:be(t,n)+i}function ye(n,e){if(n.nodeType===Node.TEXT_NODE)return K(n,e)?0:n.data.length;if(n.nodeType!==Node.ELEMENT_NODE)return 0;let t=n;if(t.matches(xe))return 0;let i=0;for(let r of Array.from(t.childNodes))i+=ye(r,e);return i}function be(n,e){let t=Q(e),i=0,r=t.nextNode();for(;r;){if(r===n||n.nodeType===Node.ELEMENT_NODE&&n.contains(r))return i;i+=r.data.length,r=t.nextNode()}return i}function P(n,e){let t=n.ownerDocument??document,i=t.getSelection();if(!i)return;let r=Math.max(0,Math.min(e,q(n))),s=Q(n),o=0,a=null,d=0,l=s.nextNode(),p=null;for(;l;){let u=l.data.length;if(r<o+u){a=l,d=r-o;break}if(r===o+u){let h=s.nextNode();h?(a=h,d=0):(a=l,d=u);break}o+=u,p=l,l=s.nextNode()}let c=t.createRange();if(a){let u=a.parentElement?.closest('strong[data-seg="completed"]');u&&u!==n&&n.contains(u)?d===0?c.setStartBefore(u):d===a.data.length?c.setStartAfter(u):c.setStart(a,d):c.setStart(a,d)}else p?c.setStart(p,p.data.length):c.setStart(n,0);c.collapse(!0),i.removeAllRanges(),i.addRange(c)}function $(n){let e=C(n);return e==null?!1:e>=q(n)}function U(n,e){if(e<=0)return 0;let t=et();if(!t)return e-1;let i=n.slice(0,e),r=0;for(let{index:s}of t.segment(i))s<e&&(r=s);return r}function se(n,e){return n instanceof HTMLTextAreaElement||n instanceof HTMLInputElement?n.selectionStart!=null&&n.selectionStart===n.value.length:n instanceof HTMLElement&&n.hasAttribute("data-aia-input")?$(n):e?.caretOffset!=null?e.caretOffset>=e.text.length:!1}function tt(n){return n instanceof HTMLElement&&n.hasAttribute("data-aia-input")?C(n):null}var G=class{constructor(e,t){this.store=e;this.ctx=t}handleKeyDown(e){let t=this.store.get(),{listboxId:i,getOnSubmit:r}=this.ctx,s=this.getEffectiveColumns(),o=r(),a=this.getTappableIndices(s);if((e.shiftKey||e.metaKey||e.ctrlKey||e.altKey)&&(e.key==="ArrowDown"||e.key==="ArrowUp"||e.key==="ArrowLeft"||e.key==="ArrowRight"))return;let d=this.ctx.getOptionsPosition()==="above";switch(e.key){case"ArrowDown":{let l=se(e.target,t),p=!!t.editingParam;if(!l&&!p&&t.activeDropdownIndex<0)break;if(t.activeDropdownIndex<0){if(d)break;if(e.preventDefault(),!t.isDropdownOpen&&t.actionableSuggestions.length>0){this.store.set({pillTapped:!0,activeDropdownIndex:a[0]??0});break}if(a.length===0)return;this.store.set({activeDropdownIndex:a[0]});break}if(e.preventDefault(),a.length===0)return;if(t.filteredOptions.length>0){let h=Math.floor((t.filteredOptions.length-1)/s);if(Math.floor(t.activeDropdownIndex/s)===h){this.store.set({activeDropdownIndex:-1});break}}let c=a.indexOf(t.activeDropdownIndex),u=c<a.length-1?c+1:0;this.store.set({activeDropdownIndex:a[u]});break}case"ArrowUp":{if(t.activeDropdownIndex<0){if(!d)break;let c=se(e.target,t),u=!!t.editingParam;if(!c&&!u)break;e.preventDefault();let h=this.firstTappableInBottomRow(s)??a[0]??0;if(!t.isDropdownOpen&&t.actionableSuggestions.length>0){this.store.set({pillTapped:!0,activeDropdownIndex:h});break}if(a.length===0)return;this.store.set({activeDropdownIndex:h});break}if(a.length===0)break;if(e.preventDefault(),t.activeDropdownIndex<s){this.store.set({activeDropdownIndex:-1});break}let l=a.indexOf(t.activeDropdownIndex),p=l>0?l-1:a.length-1;this.store.set({activeDropdownIndex:a[p]});break}case"ArrowRight":{if(t.activeDropdownIndex>=0){if(e.preventDefault(),t.activeDropdownIndex%s<s-1){let c=t.activeDropdownIndex+1;c<t.filteredOptions.length&&t.filteredOptions[c]?.is_tappable&&this.store.set({activeDropdownIndex:c})}break}if(t.editingParam&&e.target instanceof HTMLElement&&t.editingTail!=null){e.preventDefault();let p=e.target.closest("[data-aia-input]")??e.target,c=t.editingTail;this.ctx.exitEditMode?.(),P(p,c);break}se(e.target,t)&&t.actionableSuggestions.length>=1&&(e.preventDefault(),this.removeActivePill());break}case"ArrowLeft":{if(t.activeDropdownIndex>=0){if(e.preventDefault(),t.activeDropdownIndex%s>0){let l=t.activeDropdownIndex-1;l>=0&&t.filteredOptions[l]?.is_tappable&&this.store.set({activeDropdownIndex:l})}break}if(t.editingParam&&e.target instanceof HTMLElement&&t.editingAnchor!=null){e.preventDefault();let l=e.target.closest("[data-aia-input]")??e.target,p=t.editingAnchor;this.ctx.exitEditMode?.(),P(l,p);break}break}case"Backspace":{if(t.editingParam||!this.ctx.removeParamAtCaret)break;let l=tt(e.target);if(l==null)break;this.ctx.removeParamAtCaret(l)&&e.preventDefault();break}case"Enter":{if(e.preventDefault(),t.activeDropdownIndex>=0&&t.filteredOptions[t.activeDropdownIndex]?.is_tappable)this.clickOrSelect(t.activeDropdownIndex,t.filteredOptions,i);else if(o){let{rawQuery:l,completedParams:p}=S(t.text,t.completedParams);o({query:t.text.trim(),raw_query:l,completed_params:p}),this.ctx.afterSubmit?.()}break}case"Tab":{let l=t.filteredOptions.map((u,h)=>u.is_tappable?h:-1).filter(u=>u!==-1);if(l.length===0)break;if(!t.isDropdownOpen){if(t.actionableSuggestions.length===0)break;e.preventDefault();let u=e.shiftKey?l[l.length-1]:l[0];this.store.set({pillTapped:!0,activeDropdownIndex:u,hasTabbedToHighlight:!0});break}e.preventDefault();let p=l.indexOf(t.activeDropdownIndex),c;if(p<0)c=e.shiftKey?l.length-1:0;else{let u=e.shiftKey?-1:1;c=(p+u+l.length)%l.length}this.store.set({activeDropdownIndex:l[c],hasTabbedToHighlight:!0});break}case"Escape":{if(t.editingParam&&e.target instanceof HTMLElement&&t.editingTail!=null){let l=e.target.closest("[data-aia-input]")??e.target,p=t.editingTail;this.ctx.exitEditMode?.(),P(l,p)}this.store.set({activeDropdownIndex:-1});break}}}firstTappableInBottomRow(e){let t=this.store.get();if(t.filteredOptions.length===0)return null;let r=Math.floor((t.filteredOptions.length-1)/e)*e;for(let s=r;s<t.filteredOptions.length;s++)if(t.filteredOptions[s]?.is_tappable)return s;return null}getTappableIndices(e){let i=this.store.get().filteredOptions.map((s,o)=>s.is_tappable?o:-1).filter(s=>s!==-1),r=Array.from({length:e},()=>[]);for(let s of i)r[s%e].push(s);return r.flat()}getEffectiveColumns(){let e=document.getElementById(this.ctx.listboxId);if(!e)return this.ctx.columns;let i=document.getElementById(`${this.ctx.listboxId}-option-0`)?.parentElement??null;for(;i;){let r=getComputedStyle(i).gridTemplateColumns;if(r&&r!=="none"){let s=r.split(" ").filter(Boolean).length;if(s>0)return s}if(i===e)break;i=i.parentElement}return this.ctx.columns}clickOrSelect(e,t,i){let r=document.getElementById(`${i}-option-${e}`);r?r.click():this.ctx.selectOption(t[e])}removeActivePill(){let e=this.store.get(),t=e.suggestions.filter(s=>s.type==="placeholder"),i=e.suggestions.filter(s=>s.type!=="placeholder");if(i.length===0)return;let r=i.slice(1);this.store.set({suggestions:[...t,...r],pillTapped:r.length>0,activeDropdownIndex:-1})}};var W=class{constructor(e,t={}){this.store=e;this.callbacks=t}setActivePill(e){let t=this.store.get(),i=t.suggestions.filter(l=>l.type!=="placeholder");if(e<0||e>=i.length)return;let r=i[e],s=i.filter((l,p)=>p!==e),o=t.suggestions.filter(l=>l.type==="placeholder");if(this.callbacks.onPillSelected){let{rawQuery:l}=S(t.text,t.completedParams);this.callbacks.onPillSelected({rawQuery:l,selectedPill:r.text,otherPills:s.map(p=>p.text)})}let a=[...o,r,...s],d=this.store.peek({suggestions:a}).filteredOptions.findIndex(l=>l.is_tappable);this.store.set({suggestions:a,pillTapped:!0,activeDropdownIndex:d})}removeLastParam(){this.store.get().completedParams.length!==0&&this.store.set(t=>({completedParams:t.completedParams.slice(0,-1),activeDropdownIndex:-1}))}};function ve(n,e){let t=[],i=0;for(let s of e){let o=n.indexOf(s.text,i);o!==-1&&(o>i&&t.push({type:"text",value:n.slice(i,o)}),t.push({type:"completed",value:s.text,param:s}),i=o+s.text.length)}let r=n.slice(i);return r&&t.push({type:"text",value:r}),t}function Se(n,e){let t=[],i=[],r=0;for(let s of e){let o=n.indexOf(s.text,r);o===-1?i.push(s):(t.push(s),r=o+s.text.length)}return{valid:t,invalid:i}}function Pe(n,e){let t=e.dropdownTrigger??"auto",i=e.closeDropdownOnBlur??!0,r=n.filteredOptionsLength>0;if(n.inEditMode){let s=i?n.isFocused:!0;return r&&s}if(t==="auto"){let s=i?n.isFocused:!0,o=n.text.replace(/\s+$/,"").length,a=n.caretOffset==null||n.caretOffset>=o;return(r||n.isLoading)&&s&&a}return t==="manual"?(r||n.isLoading)&&n.pillTapped:!1}function Te(n,e){let t=ve(n.text,n.completedParams),i=n.suggestions.filter(g=>g.type!=="placeholder"),r=i[0],s=r?e.optionOverrides?.[r.type]:void 0,o=n.suggestions.filter(g=>g.type==="placeholder").map(g=>g.text).join(" "),a=I(n.text,Math.min(n.filterBase,n.text.length),o),l=n.lastRawQuery!==""||a>0?E(n.text,a,n.filterInProgress):"",p=r?s?s(l.trim()):r.options??[]:[],c=n.editingParam!=null&&n.editingAnchor!=null,u;if(c&&n.editingParam&&n.editingAnchor!=null){let g=n.editingParam.id,x=n.completedParams.some(y=>y.id===g),m=n.caretOffset??n.editingAnchor,f=x?"":n.text.slice(n.editingAnchor,m);u=w(n.editingParam.options,f)}else u=w(p,l);e.showNonTappableOptions===!1&&(u=u.filter(g=>g.is_tappable));let h=Pe({inEditMode:c,filteredOptionsLength:u.length,isFocused:n.isFocused,text:n.text,caretOffset:n.caretOffset,isLoading:n.isLoading,pillTapped:n.pillTapped},{dropdownTrigger:e.dropdownTrigger,closeDropdownOnBlur:e.closeDropdownOnBlur}),b=h&&n.activeDropdownIndex>=0&&!!u[n.activeDropdownIndex]?.is_tappable;return{segments:t,actionableSuggestions:i,filteredOptions:u,placeholderText:o,isDropdownOpen:h,isActivePillSelected:b}}function j(n){return n.mode==="fresh"?nt(n):it(n)}function nt(n){let{text:e,completedParams:t,suggestions:i,filterBase:r,filterInProgress:s}=n,a=i.filter(v=>v.type!=="placeholder")[0];if(!a?.options)return null;let d=i.filter(v=>v.type==="placeholder").map(v=>v.text).join(" "),l=I(e,r,d),p=E(e,l,s),c=O(a.options,p);if(!c)return null;let u=c.text.toLowerCase(),h=e.toLowerCase().lastIndexOf(u),b=h>=0?h:Math.max(0,e.length-c.text.length),g=b+c.text.length,x=e.slice(b,g),f=g<e.length&&e[g]===" "?g+1:g,y={id:crypto.randomUUID(),placeholder:"",type:a.type,text:x,kind:c.kind,suggestionType:a.type,suggestionPlaceholder:a.text,options:a.options??[],metadata:c.metadata};return{patch:{text:e,completedParams:[...t,y],suggestions:i.filter(v=>v!==a),filterBase:f,newParamId:y.id,caretOffset:f,activeDropdownIndex:-1},caretPos:f}}function it(n){let{text:e,completedParams:t,editingParam:i,editingAnchor:r,editingTail:s}=n;if(t.some(y=>y.id===i.id))return null;let o=e.slice(r,s),a=O(i.options,o);if(!a)return null;let d=a.text.toLowerCase(),l=o.toLowerCase().lastIndexOf(d),p=r+Math.max(0,l),c=p+a.text.length,u=e.slice(p,c),b=c<e.length&&e[c]===" "?c+1:c,g={id:crypto.randomUUID(),placeholder:"",type:i.suggestionType,text:u,kind:a.kind,suggestionType:i.suggestionType,suggestionPlaceholder:i.suggestionPlaceholder,options:i.options,metadata:a.metadata},x=t.length,m=0;for(let y=0;y<t.length;y++){let v=e.indexOf(t[y].text,m);if(v!==-1){if(v>=b){x=y;break}m=v+t[y].text.length}}let f=[...t];return f.splice(x,0,g),{patch:{text:e,completedParams:f,newParamId:g.id,filterBase:b,editingParam:null,editingAnchor:null,editingTail:null,caretOffset:b,activeDropdownIndex:-1},caretPos:b}}var V=class{constructor(e){this.deps=e}start(e){let t=this.deps.store.get();if(t.editingParam?.id===e)return;let i=t.completedParams.find(o=>o.id===e);if(!i)return;let r=0,s=-1;for(let o of t.completedParams){let a=t.text.indexOf(o.text,r);if(a!==-1){if(o.id===e){s=a;break}r=a+o.text.length}}s<0||this.deps.store.set({editingParam:i,editingAnchor:s,editingTail:s+i.text.length,caretOffset:s+i.text.length,activeDropdownIndex:-1})}exit(){this.deps.store.get().editingParam&&this.deps.store.set({editingParam:null,editingAnchor:null,editingTail:null,activeDropdownIndex:-1})}replaceRange(e){let t=this.deps.store.get(),i=t.editingParam,r=t.editingAnchor,s=t.editingTail;if(!i||r==null||s==null||!t.completedParams.some(d=>d.id===i.id))return!1;let o=t.text.slice(0,r)+e+t.text.slice(s),a=r+e.length;return this.deps.store.set(d=>({text:o,completedParams:d.completedParams.filter(l=>l.id!==i.id),editingTail:a,caretOffset:a,activeDropdownIndex:-1})),this.deps.scheduleSetCursor(a),this.tryPromote(),!0}caretAfterInput(e){let t=this.deps.store.get(),i={caretOffset:e};t.editingParam&&t.editingAnchor!=null&&e!=null&&(e<t.editingAnchor?(i.editingParam=null,i.editingAnchor=null,i.editingTail=null,i.activeDropdownIndex=-1):t.editingTail!=null&&(i.editingTail=Math.max(t.editingTail,e))),this.deps.store.set(i),this.tryPromote()}caretMove(e){let t=this.deps.store.get();if(t.editingParam&&t.editingAnchor!=null&&t.editingTail!=null&&e!=null&&(e<t.editingAnchor||e>t.editingTail)){this.deps.store.set({caretOffset:e,editingParam:null,editingAnchor:null,editingTail:null,activeDropdownIndex:-1});return}this.deps.store.set({caretOffset:e})}selectOption(e){let t=this.deps.store.get(),i=t.editingParam,r=t.editingAnchor,s=t.editingTail;if(!i||r==null||s==null)return;this.deps.fireTelemetry("option",{raw_query:S(t.text,t.completedParams).rawQuery,selected_option:e.text,other_options:i.options.filter(m=>m.text!==e.text).map(m=>m.text)});let o=t.text.slice(0,r),a=t.text.slice(s),d=r===0&&e.text.length>0?e.text[0].toUpperCase()+e.text.slice(1):e.text,l=a.length===0||a[0]!==" ",p=l?`${d} `:d,c=o+p+a,u=r+p.length+(l?0:1),h={id:crypto.randomUUID(),placeholder:"",type:i.suggestionType,text:d,kind:e.kind,suggestionType:i.suggestionType,suggestionPlaceholder:i.suggestionPlaceholder,options:i.options,metadata:e.metadata},b=t.completedParams.findIndex(m=>m.id===i.id),g=t.completedParams.filter(m=>m.id!==i.id),x=b>=0?Math.min(b,g.length):g.length;g.splice(x,0,h),this.deps.store.set({text:c,completedParams:g,newParamId:h.id,filterBase:u,editingParam:null,editingAnchor:null,editingTail:null,caretOffset:u,activeDropdownIndex:-1,pillTapped:!1,skipNextFetch:!0,inSelectionAnimation:!0}),this.deps.startSelectionAnimationTimer(),this.deps.scheduleSetCursor(u)}tryPromote(){let e=this.deps.store.get();if(!e.editingParam||e.editingAnchor==null||e.editingTail==null)return;let t=j({mode:"edit",text:e.text,completedParams:e.completedParams,editingParam:e.editingParam,editingAnchor:e.editingAnchor,editingTail:e.editingTail});t&&(this.deps.store.set(t.patch),this.deps.scheduleSetCursor(t.caretPos))}};function z(n,e,t){return e?{key:"enter",hint:"to proceed"}:t?{key:"tab",hint:"to skip"}:n?{key:"\u2192",hint:"to skip"}:{key:"tab",hint:"to select"}}var Ce="data-aia-key";function X(n,e,t){let i=new Map;for(let o of Array.from(n.children)){let a=o.getAttribute(Ce);a!=null&&i.set(a,o)}let r=new Set,s=[];for(let o=0;o<e.length;o++){let a=e[o],d=t.keyOf(a,o);r.add(d);let l=i.get(d);l||(l=t.create(a,o),l.setAttribute(Ce,d)),t.update?.(l,a,o),n.children[o]!==l&&n.insertBefore(l,n.children[o]??null),s.push(l)}for(let[o,a]of i)r.has(o)||a.remove();return s}var Ee=[125,69];function we(n,e){return e?1:n===0?.7:n===1?.4:.2}function Y(n,e,t,i,r=!1,s=!1,o=!1){let a=n.querySelector(".magicx-aia-pill-list");if(a||(a=document.createElement("span"),a.className="magicx-aia-pill-list",n.appendChild(a)),s&&e.length===0){a.setAttribute("data-aia-pill-list-loading",""),a.innerHTML="";for(let d=0;d<Ee.length;d++){let l=Ee[d],p=document.createElement("span");p.setAttribute("data-aia-pill-skeleton",""),p.className=`magicx-aia-pill magicx-aia-pill--skeleton${r?" magicx-aia-pill--rounded":""}`,p.style.width=`${l}px`,p.style.opacity=String(we(d,!1)),a.appendChild(p)}return}s?a.setAttribute("data-aia-pill-list-loading",""):a.removeAttribute("data-aia-pill-list-loading");for(let d of a.querySelectorAll("[data-aia-pill-skeleton]"))d.remove();X(a,e,{keyOf:d=>`${d.type}-${d.text}`,create:d=>{let l=document.createElement("button");return l.type="button",l.tabIndex=-1,l.setAttribute("data-aia-pill",""),l.setAttribute("contenteditable","false"),l.textContent=d.text,l.addEventListener("mousedown",p=>p.preventDefault()),l},update:(d,l,p)=>{let c=d,u=o&&p===t&&!s,h=["magicx-aia-pill"];r&&h.push("magicx-aia-pill--rounded"),u&&h.push("magicx-aia-pill--active"),s&&h.push("magicx-aia-pill--skeleton"),c.className=h.join(" "),c.style.width="",c.style.opacity=String(we(p,u)),s?(c.setAttribute("data-aia-loading",""),c.disabled=!0,c.onclick=null):(c.removeAttribute("data-aia-loading"),c.disabled=!1,c.onclick=()=>i(p))}})}function ae(n){n.querySelector(".magicx-aia-pill-list")?.remove()}function Ae(n,e,t,i,r,s,o){let a=n.querySelector(".aia-grid");if(e.length===0){a?.remove();return}a||(a=document.createElement("div"),a.className="aia-grid magicx-aia-grid",a.setAttribute("data-scroll",""),a.style.setProperty("--aia-grid-min","250px"),a.style.setProperty("--aia-grid-max","250px"),a.style.setProperty("--aia-grid-gap","0"),n.appendChild(a)),rt(a,e,t,i,r,s,o)}function rt(n,e,t,i,r,s,o){let a=o?"1":"0";X(n,e,{keyOf:d=>`${d.text}\0${a}`,create:d=>ot(d,o),update:(d,l,p)=>{let c=p===t&&!o;d.id=`${s}-option-${p}`,d.dataset.aiaIndex=String(p),d.setAttribute("aria-selected",String(c)),d.classList.toggle("magicx-aia-option--highlighted",c),!o&&l.is_tappable?(d.onclick=()=>{d.classList.add("magicx-aia-option--pressed"),i(l),setTimeout(()=>d.classList.remove("magicx-aia-option--pressed"),500)},d.onmouseenter=()=>{let u=Number.parseInt(d.dataset.aiaIndex??"-1",10);u>=0&&r(u)}):(d.onclick=null,d.onmouseenter=null)}})}function ot(n,e){let t=document.createElement("div");t.setAttribute("role","option"),t.setAttribute("data-aia-option",""),e&&t.setAttribute("data-aia-loading",""),t.tabIndex=e||!n.is_tappable?-1:0;let i=["magicx-aia-option"];n.is_tappable?i.push("magicx-aia-option--tappable"):i.push("magicx-aia-option--non-tappable"),t.className=i.join(" ");let r=document.createElement("div");r.className="magicx-aia-streaks",t.appendChild(r);let s=document.createElement("div");s.className="magicx-aia-streaks-vert",t.appendChild(s);let o=document.createElement("span");o.className="magicx-aia-option-content";let a=document.createElement("span");if(a.className="magicx-aia-option-text",a.textContent=n.icon?`${n.icon} ${n.text}`:n.text,o.appendChild(a),n.tag){let d=document.createElement("span");d.className="magicx-aia-option-tag",d.textContent=n.tag,o.appendChild(d)}return t.appendChild(o),t}var st=[159,119,164];function J(n){let e=document.createElement("div");return e.id=n,e.setAttribute("role","listbox"),e.setAttribute("data-aia-dropdown",""),e.className="magicx-aia-dropdown",e.addEventListener("mousedown",t=>t.preventDefault()),e}function Z(n,e){let{filteredOptions:t,activeIndex:i,isOpen:r,isLoading:s,pills:o,showPills:a,isActivePillSelected:d,onSelect:l,onHighlight:p,onPillClick:c}=e,u=a&&o.length>0,h=t.length>0,b=r&&(h||u||s);if(b?n.classList.add("magicx-aia-dropdown--visible"):n.classList.remove("magicx-aia-dropdown--visible"),s?n.setAttribute("data-aia-loading",""):n.removeAttribute("data-aia-loading"),!b)return;let g=n.querySelector(".aia-stack");g||(g=document.createElement("div"),g.className="aia-stack",g.style.setProperty("--aia-stack-space","8px"),n.appendChild(g));let x=u||s&&a,m=g.querySelector(".magicx-aia-pill-bar");x?(m||(m=document.createElement("div"),m.className="magicx-aia-pill-bar aia-cluster",m.setAttribute("data-nowrap",""),m.setAttribute("data-aia-pillbar",""),g.insertBefore(m,g.firstChild)),Y(m,o,0,c,!0,s,d)):m&&m.remove(),Ae(g,t,i,l,p,e.listboxId,s);let f=g.querySelector(".magicx-aia-skeleton-bars");if(s&&!h){if(!f){f=document.createElement("div"),f.className="magicx-aia-skeleton-bars",f.setAttribute("data-aia-skeleton-bars","");for(let T of st){let A=document.createElement("span");A.className="magicx-aia-skeleton-bar",A.style.width=`${T}px`,f.appendChild(A)}g.appendChild(f)}}else f&&f.remove();let y=g.querySelector(".magicx-aia-footer")??lt(),v=i>=0&&!!t[i]?.is_tappable;at(y,z(e.hasTabbedToHighlight,v,e.isInputEmpty)),g.lastElementChild!==y&&g.appendChild(y)}function at(n,{key:e,hint:t}){let i=n.querySelector(".magicx-aia-footer-key"),r=n.querySelector(".magicx-aia-footer-hint");!i||!r||(i.textContent!==e&&(i.textContent=e),r.textContent!==t&&(r.textContent=t))}function lt(){let n=document.createElement("footer");n.className="magicx-aia-footer",n.setAttribute("data-aia-footer","");let e=document.createElement("div");e.className="magicx-aia-footer-divider";let t=document.createElement("div");t.className="aia-cluster",t.setAttribute("data-align","center"),t.setAttribute("data-justify","between"),t.setAttribute("data-nowrap","");let i=document.createElement("div");i.className="aia-cluster",i.setAttribute("data-align","center"),i.style.setProperty("--aia-cluster-gap","5px");let r=document.createElement("kbd");r.className="magicx-aia-footer-key",r.textContent="tab";let s=document.createElement("span");s.className="magicx-aia-footer-hint",s.textContent="to select",i.append(r,s);let o=document.createElement("a");o.className="aia-cluster magicx-aia-footer-brand-link",o.setAttribute("data-align","center"),o.href="https://ai-autocomplete.com",o.target="_blank",o.rel="noopener noreferrer",o.style.setProperty("--aia-cluster-gap","2px");let a=document.createElement("span");a.className="magicx-aia-footer-brand",a.textContent="AI";let d=document.createElement("span");return d.className="magicx-aia-footer-badge",d.textContent="Autocomplete",o.append(a,d),t.append(i,o),n.append(e,t),n}function Ie(n,e){let t=J(e.listboxId);return n.appendChild(t),{dropdown:t}}function le(n,e,t){Z(n.dropdown,{suggestions:e.actionableSuggestions.length>0?[{...e.actionableSuggestions[0],options:e.filteredOptions}]:[],filteredOptions:e.filteredOptions,activeIndex:e.activeDropdownIndex,isOpen:e.isDropdownOpen,isLoading:e.isLoading&&!e.editingParam&&!e.inSelectionAnimation,listboxId:t.listboxId,pills:e.actionableSuggestions,showPills:!0,isActivePillSelected:e.isActivePillSelected,hasTabbedToHighlight:e.hasTabbedToHighlight,isInputEmpty:e.text.trim().length===0,onSelect:t.selectOption,onHighlight:i=>t.store.set({activeDropdownIndex:i}),onPillClick:t.setActivePill})}function ee(n){let{input:e,segments:t,newParamId:i,editingParamId:r,placeholderText:s,isFocused:o}=n,a=t.length===0;e.dataset.aiaEmpty=a?"true":"false",a&&s?e.dataset.placeholder=s:delete e.dataset.placeholder;let d=t.map(x=>`${x.type}:${x.value}`).join("\0"),l=e.dataset.segKey??"",p=e.dataset.newParamId??"",c=e.dataset.editingParamId??"";if(d===l&&(i??"")===p&&(r??"")===c)return;let u=o?C(e):null;e.dataset.segKey=d,e.dataset.newParamId=i??"",e.dataset.editingParamId=r??"";let h=e.ownerDocument??document,b=h.createDocumentFragment(),g=0;for(let x of t)if(g+=x.value.length,x.type==="completed"){let m=h.createElement("strong");m.dataset.seg="completed",m.dataset.paramId=x.param.id;let f=x.param.id===i,y=x.param.id===r,v=["magicx-aia-segment","magicx-aia-segment--completed"];f&&v.push("magicx-aia-shimmer-revealed","magicx-aia-shimmer-sweep"),y&&v.push("magicx-aia-segment--editing"),m.className=v.join(" "),m.textContent=x.value,b.appendChild(m)}else b.appendChild(h.createTextNode(x.value));e.replaceChildren(b),e.dataset.aiaTextLength=String(g),u!=null&&P(e,Math.max(0,Math.min(u,g)))}var dt='<svg width="18" height="18" viewBox="0 0 18 18" fill="none" role="img" aria-label="Submit"><path d="M9 14V4M9 4L4 9M9 4L14 9" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';function Oe(){let n=document.createElement("button");return n.type="button",n.className="magicx-aia-submit",n.setAttribute("aria-label","Submit"),n.setAttribute("data-aia-submit",""),n.innerHTML=dt,n}function ct(){let n=document.createElement("div");return n.setAttribute("contenteditable","plaintext-only"),n.contentEditable==="plaintext-only"}function De(n,e){let t=e.firstElementChild;if(!t){e.removeAttribute("data-aia-pill-wrapped");return}let i=t.getBoundingClientRect(),r=n.getBoundingClientRect();i.top>=r.bottom-2?e.setAttribute("data-aia-pill-wrapped",""):e.removeAttribute("data-aia-pill-wrapped")}function ke(n,e){let{listboxId:t}=e,i=J(t);n.appendChild(i);let r=document.createElement("div");r.className="magicx-aia-input-wrapper",n.appendChild(r);let s=document.createElement("div");s.className="magicx-aia-editor",s.setAttribute("data-aia-editor",""),r.appendChild(s);let o=document.createElement("div");o.className="magicx-aia-input",o.setAttribute("data-aia-input",""),o.setAttribute("contenteditable",ct()?"plaintext-only":"true"),o.setAttribute("role","combobox"),o.setAttribute("aria-autocomplete","list"),o.setAttribute("aria-haspopup","listbox"),o.setAttribute("aria-controls",t),o.setAttribute("aria-expanded","false"),o.setAttribute("spellcheck","true"),o.setAttribute("enterkeyhint","send"),s.appendChild(o);let a=document.createElement("span");a.className="magicx-aia-pill-list-container",a.setAttribute("data-aia-pill-list-container",""),s.appendChild(a);let d=null,l=null;e.submitButton===void 0?(d=Oe(),r.appendChild(d),l=d):e.submitButton!==null&&(l=e.submitButton,l.hasAttribute("data-aia-submit")||l.setAttribute("data-aia-submit",""),r.appendChild(l));let p=new AbortController,{signal:c}=p,u=!1,h=0,b=()=>{let m=L(o),y=m.length>0&&m[0]!==m[0].toUpperCase()?m[0].toUpperCase()+m.slice(1):m;e.handleChange(y)},g=()=>{let m=(o.ownerDocument??document).getSelection();if(!m||m.rangeCount===0)return null;let f=m.anchorNode;return!f||!o.contains(f)?null:(f.nodeType===Node.ELEMENT_NODE?f:f.parentElement)?.closest('strong[data-seg="completed"][data-param-id]')?.dataset.paramId??null};r.addEventListener("click",m=>{m.target?.closest("[data-aia-pill]")||o.focus()},{signal:c}),o.addEventListener("input",()=>{u||(h=performance.now(),b(),e.handleCaretAfterInput(C(o)))},{signal:c});let x=o.ownerDocument??document;if(x.addEventListener("selectionchange",()=>{let m=x.getSelection();if(!m||m.rangeCount===0||!o.contains(m.anchorNode))return;let f=g(),y=e.store.get().editingParam?.id??null;if(f&&f!==y){e.startEditingParam(f);return}performance.now()-h<50||e.handleCaretMove(C(o))},{signal:c}),o.addEventListener("compositionstart",()=>{u=!0},{signal:c}),o.addEventListener("compositionend",()=>{u=!1,b()},{signal:c}),o.addEventListener("beforeinput",m=>{let f=m,y=f.inputType;if(y==="insertParagraph"||y==="insertLineBreak"||y==="insertFromDrop"){m.preventDefault();return}if(y.startsWith("insert")||y.startsWith("delete")){let v=y.startsWith("delete")?"":f.data??"";e.replaceEditingRange(v)&&m.preventDefault()}},{signal:c}),o.addEventListener("paste",m=>{m.preventDefault();let f=(m.clipboardData?.getData("text/plain")??"").replace(/\r?\n/g," ");if(!f)return;let y=o.ownerDocument??document,v=y.getSelection();if(!v||v.rangeCount===0)return;let T=v.getRangeAt(0);if(!o.contains(T.startContainer))return;T.deleteContents();let A=y.createTextNode(f);T.insertNode(A),T.setStartAfter(A),T.collapse(!0),v.removeAllRanges(),v.addRange(T),b()},{signal:c}),o.addEventListener("keydown",m=>e.handleKeyDown(m),{signal:c}),o.addEventListener("focus",()=>e.store.set({isFocused:!0}),{signal:c}),o.addEventListener("blur",()=>e.store.set({isFocused:!1}),{signal:c}),l&&l.addEventListener("click",m=>{let f=e.store.get();if(!(!!f.text||f.completedParams.length>0)||!e.onSubmit)return;m.stopPropagation();let{rawQuery:v,completedParams:T}=S(f.text,f.completedParams);e.onSubmit({query:f.text.trim(),raw_query:v,completed_params:T}),e.afterSubmit?.()},{signal:c}),e.autoFocus!==!1&&o.focus(),typeof ResizeObserver<"u"){let m=new ResizeObserver(()=>De(o,a));m.observe(o),p.signal.addEventListener("abort",()=>m.disconnect(),{once:!0})}return{input:o,inlinePillContainer:a,dropdown:i,submitButton:d,abort:p}}function de(n,e,t){let{input:i,inlinePillContainer:r,dropdown:s,submitButton:o}=n,{pillPlacement:a,setActivePill:d,selectOption:l,store:p}=t;i.setAttribute("aria-expanded",String(e.isDropdownOpen));let c=e.activeDropdownIndex>=0?`${t.listboxId}-option-${e.activeDropdownIndex}`:"";if(c?i.setAttribute("aria-activedescendant",c):i.removeAttribute("aria-activedescendant"),o){let x=!!e.text||e.completedParams.length>0;o.disabled=!x}let u=i.dataset.newParamId??"",h=e.newParamId!==null&&e.newParamId!==u;if(ee({input:i,segments:e.segments,newParamId:e.newParamId,editingParamId:e.editingParam?.id??null,placeholderText:e.placeholderText,isFocused:e.isFocused}),a==="inline"){let x=e.isLoading&&!e.editingParam&&!e.inSelectionAnimation;x||e.actionableSuggestions.length>0?Y(r,e.actionableSuggestions,0,d,!1,x,e.isActivePillSelected):ae(r)}else ae(r);De(i,r),h?(i.focus(),P(i,e.caretOffset??e.text.length)):e.isFocused&&L(i)!==e.text&&P(i,e.text.length);let b=e.editingParam?{type:e.editingParam.suggestionType,text:e.editingParam.suggestionPlaceholder,required:!0,options:e.editingParam.options}:null,g=b??e.actionableSuggestions[0];Z(s,{suggestions:g?[{...g,options:e.filteredOptions}]:[],filteredOptions:e.filteredOptions,activeIndex:e.activeDropdownIndex,isOpen:e.isDropdownOpen,isLoading:e.isLoading&&!e.editingParam&&!e.inSelectionAnimation,listboxId:t.listboxId,pills:b?[b]:e.actionableSuggestions,showPills:a==="dropdown",isActivePillSelected:e.isActivePillSelected,hasTabbedToHighlight:e.hasTabbedToHighlight,isInputEmpty:e.text.trim().length===0,onSelect:l,onHighlight:x=>p.set({activeDropdownIndex:x}),onPillClick:d})}function Me(n,e){let t=n.actionableSuggestions[0];if(!t)return null;let i=n.filterBase,r=n.text.slice(0,i),s=r.length===0&&n.text.length===0,o=r.length===0&&n.text.length>0&&n.placeholderText.length>0&&n.placeholderText.toLowerCase().startsWith(n.text.toLowerCase());(s||o)&&n.placeholderText&&(r=`${n.placeholderText} `);let a=fe(r,e.text);a>0&&(r=r.slice(0,r.length-a));let d=r.length>0&&r[r.length-1]!==" ",l=`${r}${d?" ":""}${e.text} `,p=(s||o)&&l.length>0?l[0].toUpperCase()+l.slice(1):l,c=p.toLowerCase().lastIndexOf(e.text.toLowerCase()),u=c>=0?p.slice(c,c+e.text.length):e.text,h={id:crypto.randomUUID(),placeholder:"",type:t.type,text:u,kind:e.kind,suggestionType:t.type,suggestionPlaceholder:t.text,options:t.options??[],metadata:e.metadata},b=n.actionableSuggestions.length-1;return{patch:{text:p,filterBase:p.length,completedParams:[...n.completedParams,h],newParamId:h.id,caretOffset:p.length,pillTapped:!1,activeDropdownIndex:-1,skipNextFetch:b>0,inSelectionAnimation:!0},telemetry:{selectedOption:e.text,otherOptions:n.filteredOptions.filter(g=>g.text!==e.text).map(g=>g.text)},consumedSuggestion:t,remainingActionable:b}}function te(n){let e=n,t=new Set;return{get:()=>e,set:i=>{let r=typeof i=="function"?i(e):i,s={...e,...r},o=e;e=s;for(let a of t)a(s,o)},subscribe:i=>(t.add(i),()=>{t.delete(i)})}}function Le(n,e){let t,i,r=s=>(s!==t&&(t=s,i=e(s)),i);return{get:()=>{let s=n.get();return{...s,...r(s)}},set:s=>{typeof s=="function"?n.set(o=>{let a={...o,...r(o)};return s(a)}):n.set(s)},peek:s=>{let o={...n.get(),...s};return{...o,...e(o)}},subscribe:s=>n.subscribe((o,a)=>{let d={...a,...r(a)},l={...o,...r(o)};s(l,d)})}}var ce=!1;function Re(){if(ce||typeof document>"u")return;if(document.querySelector("style[data-magicx-aia]")){ce=!0;return}ce=!0;let n=document.createElement("style");n.setAttribute("data-magicx-aia",""),n.textContent=pt,document.head.appendChild(n)}var pt=`@layer layout {
|
|
1
|
+
"use strict";var oe=Object.defineProperty;var Be=Object.getOwnPropertyDescriptor;var Ke=Object.getOwnPropertyNames;var Qe=Object.prototype.hasOwnProperty;var qe=(n,e)=>{for(var t in e)oe(n,t,{get:e[t],enumerable:!0})},$e=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Ke(e))!Qe.call(n,r)&&r!==t&&oe(n,r,{get:()=>e[r],enumerable:!(i=Be(e,r))||i.enumerable});return n};var Ue=n=>$e(oe({},"__esModule",{value:!0}),n);var yt={};qe(yt,{AIAutocomplete:()=>re,ModeController:()=>k,buildQuery:()=>S,createStore:()=>te,cursorIsAtEnd:()=>$,extractPlainText:()=>L,getCursorOffset:()=>C,getFooterHint:()=>z,plainTextLength:()=>q,previousGraphemeBoundary:()=>U,renderEditableContent:()=>ee,setCursorOffset:()=>P});module.exports=Ue(yt);var R=class{constructor(e){this.config=e;this.current=null;this.expiresAt=null;this.inFlightRefresh=null;e.accessToken&&(this.current=e.accessToken)}async getToken(e=!1){if(!e&&this.current&&!this.isExpired())return this.current;if(!e&&this.inFlightRefresh)return this.inFlightRefresh;this.inFlightRefresh=this.refresh();try{return await this.inFlightRefresh}finally{this.inFlightRefresh=null}}async refresh(){let e=await this.config.getAccessToken();return this.current=e.accessToken,this.expiresAt=e.expiresAt??null,this.current}isExpired(){return this.expiresAt==null?!1:Date.now()>=this.expiresAt-3e4}};var Ge="https://api.ai-autocomplete.com",N=`${Ge}/api/suggest`,pe=new WeakMap;function M(n){return n?.type==="accessToken"}function We(n){if(!(!n||M(n)))return n}function _(n){let e=pe.get(n.getAccessToken);return e||(e=new R(n),pe.set(n.getAccessToken,e)),e}function H(n){return{"Content-Type":"application/json",...n?.appIdentifier&&{"X-App-Identifier":n.appIdentifier},...n?.headers}}function F(n){let e=We(n),t=e?.apiKey;return t?(e?.authScheme??"Bearer")==="Basic"?`Basic ${btoa(t)}`:`Bearer ${t}`:null}var je="0.4.2",ue=!1;function Ve(){return crypto.randomUUID()}function ze(n,e){return{placeholder:n.placeholder,type:n.type,...e&&{text:n.text},kind:n.kind}}function Xe(n,e,t,i){let r=e.find(o=>o.type==="contact"&&o.metadata?.contact_account_count)?.metadata?.contact_account_count,s=typeof r=="number"?r:void 0;return{data:{raw_query:n,completed_params:e.map(o=>ze(o,t)),...s!=null&&{contact_account_count:s}},meta:{request_id:Ve(),request_at:new Date().toISOString(),language:typeof navigator<"u"?navigator.language:"en-US",client_version:je,session_id:i}}}async function me(n,e,t,i,r){return fetch(n,{method:"POST",headers:{...e,Authorization:`Bearer ${t}`},body:i,signal:r})}async function ge(n,e,t){let i=t.apiConfig,r=!t.maskCompletedText,s=Xe(n,e,r,t.sessionId),o=H(i),a=i?.endpoint??N,d=JSON.stringify(s);if(M(i)){let c=_(i),u=await c.getToken(),h=await me(a,o,u,d,t.signal);if(h.status===401){let b=await c.getToken(!0);h=await me(a,o,b,d,t.signal)}if(!h.ok)throw new Error(`API error: ${h.status} ${h.statusText}`);return h.json()}let l=F(i);!l&&!ue&&(ue=!0,console.warn("[AIAutocomplete] No apiKey in apiConfig. Requests will be sent without an Authorization header.")),l&&(o.Authorization=l);let p=await fetch(a,{method:"POST",headers:o,body:d,signal:t.signal});if(!p.ok)throw new Error(`API error: ${p.status} ${p.statusText}`);return p.json()}function S(n,e){let t=n,i={},r=[];for(let s of e){let o=(i[s.type]??0)+1;i[s.type]=o;let d=`{{${s.type.toUpperCase().replace(/\s+/g,"_")}_${o}}}`,l=t.indexOf(s.text);l!==-1&&(t=t.slice(0,l)+d+t.slice(l+s.text.length)),r.push({...s,placeholder:d})}return{rawQuery:t,completedParams:r}}function I(n,e,t){return e>0||!t?e:n.toLowerCase().startsWith(t.toLowerCase())?t.length:e}function E(n,e,t){let i=n.slice(e);if(t||e===0||n[e-1]===" ")return i;let r=i.indexOf(" ");return r===-1?"":i.slice(r+1)}function fe(n,e){let t=n.trimEnd().replace(/\s+/g," ");if(t.length===0||e.length===0)return 0;let i=t.split(" "),r=e.toLowerCase();for(let s=0;s<i.length;s++){let o=i.slice(s).join(" ");if(r.startsWith(o.toLowerCase())){let a=t.length-o.length;return n.length-a}}return 0}function w(n,e){if(!n)return[];let t=e.trimStart();if(!t)return n;let i=t.toLowerCase();return n.filter(r=>!r.is_tappable||r.text.toLowerCase().includes(i))}function O(n,e){if(!n)return null;let t=e.trim();if(!t)return null;let i=t.toLowerCase();return n.find(r=>r.is_tappable&&r.text.toLowerCase()===i)??null}function he(n,e){return e?n.map(t=>{let i=e[t.type];if(!i)return t;let r=i("");if(r.length===0)return t;let s=new Set(r.map(a=>a.text)),o=(t.options??[]).filter(a=>!s.has(a.text));return{...t,options:[...r,...o]}}):n}var Ye=100,Je=300,Ze=2,B=class{constructor(e,t,i,r,s,o,a={}){this.store=e;this.getApiConfig=t;this.getOptionOverrides=i;this.getMaskCompletedText=r;this.getOnError=s;this.getSessionId=o;this.callbacks=a;this.fetchVersion=0;this.abortController=null;this.debounceTimer=null;this.slowDebounceTimer=null;this.unsubscribe=null}start(){this.doFetch("",[]);let e=this.store.get().text,t=this.store.get().completedParams;this.unsubscribe=this.store.subscribe(i=>{(i.text!==e||i.completedParams!==t)&&(e=i.text,t=i.completedParams,this.scheduleFetch())})}dispose(){this.abortController?.abort(),this.clearTimers(),this.unsubscribe?.()}async doFetch(e,t){this.abortController?.abort();let i=new AbortController;this.abortController=i;let r=++this.fetchVersion,s=this.store.get().text.length;this.store.set({isLoading:!0,error:null});try{let o=await ge(e,t,{sessionId:this.getSessionId(),maskCompletedText:this.getMaskCompletedText(),signal:i.signal,apiConfig:this.getApiConfig()});if(r!==this.fetchVersion)return;let a=he(o.data.suggestions??[],this.getOptionOverrides()),d=o.data.input??[],l=d[d.length-1],p=this.store.get().text,c,u;if(l?.state==="in_progress"){u=!0;let x=p.toLowerCase().lastIndexOf(l.text.toLowerCase());c=x!==-1?x:s}else u=!1,c=s;let b=a.filter(x=>x.type!=="placeholder")[0],g=null;if(b){let x=E(p,c,u),m=O(b.options,x);m&&(g={id:crypto.randomUUID(),placeholder:"",type:b.type,text:m.text,kind:m.kind,suggestionType:b.type,suggestionPlaceholder:b.text,options:b.options??[],metadata:m.metadata},a=a.filter(f=>f!==b),this.callbacks.onAutoMatch?.({active:b,matched:m,rawQuery:e}))}this.store.set(x=>({suggestions:a,isLoading:!1,isReady:o.data.is_ready??!1,lastRawQuery:e,activeDropdownIndex:-1,filterBase:c,filterInProgress:u,...g?{completedParams:[...x.completedParams,g]}:{}}))}catch(o){if(r===this.fetchVersion){let a=o instanceof Error?o:new Error(String(o));this.store.set({error:a,isLoading:!1}),this.getOnError()?.(a)}}}scheduleFetch(){if(this.clearTimers(),this.store.get().skipNextFetch){this.store.set({skipNextFetch:!1});return}let t=i=>{let r=this.store.get();if(!r.text&&r.completedParams.length===0)return this.doFetch("",[]),!0;let s=r.suggestions.filter(f=>f.type==="placeholder").map(f=>f.text).join(" "),o=I(r.text,r.filterBase,s),a=E(r.text,o,r.filterInProgress),l=r.suggestions.filter(f=>f.type!=="placeholder")[0],c=(l?w(l.options,a):[]).filter(f=>f.is_tappable),u=l?O(l.options,a)!==null:!1,h=a.trim().length>0;if(c.length>0&&!u&&h||r.completedParams.length===0&&r.text.length>0&&s.length>0&&s.toLowerCase().startsWith(r.text.toLowerCase()))return!1;let{rawQuery:b,completedParams:g}=S(r.text,r.completedParams),x=b.length<r.lastRawQuery.length,m=Math.abs(b.length-r.lastRawQuery.length);return x||m>=i?(this.doFetch(b,g),!0):!1};this.debounceTimer=setTimeout(()=>{t(Ze)&&this.slowDebounceTimer&&clearTimeout(this.slowDebounceTimer)},Ye),this.slowDebounceTimer=setTimeout(()=>t(1),Je)}clearTimers(){this.debounceTimer&&clearTimeout(this.debounceTimer),this.slowDebounceTimer&&clearTimeout(this.slowDebounceTimer),this.debounceTimer=null,this.slowDebounceTimer=null}};var xe='[contenteditable="false"]',D;function et(){if(D!==void 0)return D;let n=globalThis.Intl.Segmenter;if(!n)return D=null,null;try{D=new n(void 0,{granularity:"grapheme"})}catch{D=null}return D??null}function K(n,e){let t=n;for(;t&&t!==e;){if(t.nodeType===Node.ELEMENT_NODE&&t.matches(xe))return!0;t=t.parentNode}return!1}function Q(n){return(n.ownerDocument??document).createTreeWalker(n,NodeFilter.SHOW_TEXT,{acceptNode(e){return K(e,n)?NodeFilter.FILTER_REJECT:NodeFilter.FILTER_ACCEPT}})}function L(n){let e=Q(n),t="",i=e.nextNode();for(;i;)t+=i.data,i=e.nextNode();return t}function q(n){let e=Q(n),t=0,i=e.nextNode();for(;i;)t+=i.data.length,i=e.nextNode();return t}function C(n){let e=(n.ownerDocument??document).getSelection();if(!e||e.rangeCount===0)return null;let t=e.anchorNode,i=e.anchorOffset;if(!t||!n.contains(t))return null;if(t.nodeType===Node.ELEMENT_NODE){let r=t;if(K(r,n)&&r!==n)return null;let s=0;for(let o=0;o<i&&o<r.childNodes.length;o++)s+=ye(r.childNodes[o],n);return s+be(r,n)}return t.nodeType!==Node.TEXT_NODE||K(t,n)?null:be(t,n)+i}function ye(n,e){if(n.nodeType===Node.TEXT_NODE)return K(n,e)?0:n.data.length;if(n.nodeType!==Node.ELEMENT_NODE)return 0;let t=n;if(t.matches(xe))return 0;let i=0;for(let r of Array.from(t.childNodes))i+=ye(r,e);return i}function be(n,e){let t=Q(e),i=0,r=t.nextNode();for(;r;){if(r===n||n.nodeType===Node.ELEMENT_NODE&&n.contains(r))return i;i+=r.data.length,r=t.nextNode()}return i}function P(n,e){let t=n.ownerDocument??document,i=t.getSelection();if(!i)return;let r=Math.max(0,Math.min(e,q(n))),s=Q(n),o=0,a=null,d=0,l=s.nextNode(),p=null;for(;l;){let u=l.data.length;if(r<o+u){a=l,d=r-o;break}if(r===o+u){let h=s.nextNode();h?(a=h,d=0):(a=l,d=u);break}o+=u,p=l,l=s.nextNode()}let c=t.createRange();if(a){let u=a.parentElement?.closest('strong[data-seg="completed"]');u&&u!==n&&n.contains(u)?d===0?c.setStartBefore(u):d===a.data.length?c.setStartAfter(u):c.setStart(a,d):c.setStart(a,d)}else p?c.setStart(p,p.data.length):c.setStart(n,0);c.collapse(!0),i.removeAllRanges(),i.addRange(c)}function $(n){let e=C(n);return e==null?!1:e>=q(n)}function U(n,e){if(e<=0)return 0;let t=et();if(!t)return e-1;let i=n.slice(0,e),r=0;for(let{index:s}of t.segment(i))s<e&&(r=s);return r}function se(n,e){return n instanceof HTMLTextAreaElement||n instanceof HTMLInputElement?n.selectionStart!=null&&n.selectionStart===n.value.length:n instanceof HTMLElement&&n.hasAttribute("data-aia-input")?$(n):e?.caretOffset!=null?e.caretOffset>=e.text.length:!1}function tt(n){return n instanceof HTMLElement&&n.hasAttribute("data-aia-input")?C(n):null}var G=class{constructor(e,t){this.store=e;this.ctx=t}handleKeyDown(e){let t=this.store.get(),{listboxId:i,getOnSubmit:r}=this.ctx,s=this.getEffectiveColumns(),o=r(),a=this.getTappableIndices(s);if((e.shiftKey||e.metaKey||e.ctrlKey||e.altKey)&&(e.key==="ArrowDown"||e.key==="ArrowUp"||e.key==="ArrowLeft"||e.key==="ArrowRight"))return;let d=this.ctx.getOptionsPosition()==="above";switch(e.key){case"ArrowDown":{let l=se(e.target,t),p=!!t.editingParam;if(!l&&!p&&t.activeDropdownIndex<0)break;if(t.activeDropdownIndex<0){if(d)break;if(e.preventDefault(),!t.isDropdownOpen&&t.actionableSuggestions.length>0){this.store.set({pillTapped:!0,activeDropdownIndex:a[0]??0});break}if(a.length===0)return;this.store.set({activeDropdownIndex:a[0]});break}if(e.preventDefault(),a.length===0)return;if(t.filteredOptions.length>0){let h=Math.floor((t.filteredOptions.length-1)/s);if(Math.floor(t.activeDropdownIndex/s)===h){this.store.set({activeDropdownIndex:-1});break}}let c=a.indexOf(t.activeDropdownIndex),u=c<a.length-1?c+1:0;this.store.set({activeDropdownIndex:a[u]});break}case"ArrowUp":{if(t.activeDropdownIndex<0){if(!d)break;let c=se(e.target,t),u=!!t.editingParam;if(!c&&!u)break;e.preventDefault();let h=this.firstTappableInBottomRow(s)??a[0]??0;if(!t.isDropdownOpen&&t.actionableSuggestions.length>0){this.store.set({pillTapped:!0,activeDropdownIndex:h});break}if(a.length===0)return;this.store.set({activeDropdownIndex:h});break}if(a.length===0)break;if(e.preventDefault(),t.activeDropdownIndex<s){this.store.set({activeDropdownIndex:-1});break}let l=a.indexOf(t.activeDropdownIndex),p=l>0?l-1:a.length-1;this.store.set({activeDropdownIndex:a[p]});break}case"ArrowRight":{if(t.activeDropdownIndex>=0){if(e.preventDefault(),t.activeDropdownIndex%s<s-1){let c=t.activeDropdownIndex+1;c<t.filteredOptions.length&&t.filteredOptions[c]?.is_tappable&&this.store.set({activeDropdownIndex:c})}break}if(t.editingParam&&e.target instanceof HTMLElement&&t.editingTail!=null){e.preventDefault();let p=e.target.closest("[data-aia-input]")??e.target,c=t.editingTail;this.ctx.exitEditMode?.(),P(p,c);break}se(e.target,t)&&t.actionableSuggestions.length>=1&&(e.preventDefault(),this.removeActivePill());break}case"ArrowLeft":{if(t.activeDropdownIndex>=0){if(e.preventDefault(),t.activeDropdownIndex%s>0){let l=t.activeDropdownIndex-1;l>=0&&t.filteredOptions[l]?.is_tappable&&this.store.set({activeDropdownIndex:l})}break}if(t.editingParam&&e.target instanceof HTMLElement&&t.editingAnchor!=null){e.preventDefault();let l=e.target.closest("[data-aia-input]")??e.target,p=t.editingAnchor;this.ctx.exitEditMode?.(),P(l,p);break}break}case"Backspace":{if(t.editingParam||!this.ctx.removeParamAtCaret)break;let l=tt(e.target);if(l==null)break;this.ctx.removeParamAtCaret(l)&&e.preventDefault();break}case"Enter":{if(e.preventDefault(),t.activeDropdownIndex>=0&&t.filteredOptions[t.activeDropdownIndex]?.is_tappable)this.clickOrSelect(t.activeDropdownIndex,t.filteredOptions,i);else if(o){let{rawQuery:l,completedParams:p}=S(t.text,t.completedParams);o({query:t.text.trim(),raw_query:l,completed_params:p}),this.ctx.afterSubmit?.()}break}case"Tab":{let l=t.filteredOptions.map((u,h)=>u.is_tappable?h:-1).filter(u=>u!==-1);if(l.length===0)break;if(!t.isDropdownOpen){if(t.actionableSuggestions.length===0)break;e.preventDefault();let u=e.shiftKey?l[l.length-1]:l[0];this.store.set({pillTapped:!0,activeDropdownIndex:u,hasTabbedToHighlight:!0});break}e.preventDefault();let p=l.indexOf(t.activeDropdownIndex),c;if(p<0)c=e.shiftKey?l.length-1:0;else{let u=e.shiftKey?-1:1;c=(p+u+l.length)%l.length}this.store.set({activeDropdownIndex:l[c],hasTabbedToHighlight:!0});break}case"Escape":{if(t.editingParam&&e.target instanceof HTMLElement&&t.editingTail!=null){let l=e.target.closest("[data-aia-input]")??e.target,p=t.editingTail;this.ctx.exitEditMode?.(),P(l,p)}this.store.set({activeDropdownIndex:-1});break}}}firstTappableInBottomRow(e){let t=this.store.get();if(t.filteredOptions.length===0)return null;let r=Math.floor((t.filteredOptions.length-1)/e)*e;for(let s=r;s<t.filteredOptions.length;s++)if(t.filteredOptions[s]?.is_tappable)return s;return null}getTappableIndices(e){let i=this.store.get().filteredOptions.map((s,o)=>s.is_tappable?o:-1).filter(s=>s!==-1),r=Array.from({length:e},()=>[]);for(let s of i)r[s%e].push(s);return r.flat()}getEffectiveColumns(){let e=document.getElementById(this.ctx.listboxId);if(!e)return this.ctx.columns;let i=document.getElementById(`${this.ctx.listboxId}-option-0`)?.parentElement??null;for(;i;){let r=getComputedStyle(i).gridTemplateColumns;if(r&&r!=="none"){let s=r.split(" ").filter(Boolean).length;if(s>0)return s}if(i===e)break;i=i.parentElement}return this.ctx.columns}clickOrSelect(e,t,i){let r=document.getElementById(`${i}-option-${e}`);r?r.click():this.ctx.selectOption(t[e])}removeActivePill(){let e=this.store.get(),t=e.suggestions.filter(s=>s.type==="placeholder"),i=e.suggestions.filter(s=>s.type!=="placeholder");if(i.length===0)return;let r=i.slice(1);this.store.set({suggestions:[...t,...r],pillTapped:r.length>0,activeDropdownIndex:-1})}};var W=class{constructor(e,t={}){this.store=e;this.callbacks=t}setActivePill(e){let t=this.store.get(),i=t.suggestions.filter(l=>l.type!=="placeholder");if(e<0||e>=i.length)return;let r=i[e],s=i.filter((l,p)=>p!==e),o=t.suggestions.filter(l=>l.type==="placeholder");if(this.callbacks.onPillSelected){let{rawQuery:l}=S(t.text,t.completedParams);this.callbacks.onPillSelected({rawQuery:l,selectedPill:r.text,otherPills:s.map(p=>p.text)})}let a=[...o,r,...s],d=this.store.peek({suggestions:a}).filteredOptions.findIndex(l=>l.is_tappable);this.store.set({suggestions:a,pillTapped:!0,activeDropdownIndex:d})}removeLastParam(){this.store.get().completedParams.length!==0&&this.store.set(t=>({completedParams:t.completedParams.slice(0,-1),activeDropdownIndex:-1}))}};function ve(n,e){let t=[],i=0;for(let s of e){let o=n.indexOf(s.text,i);o!==-1&&(o>i&&t.push({type:"text",value:n.slice(i,o)}),t.push({type:"completed",value:s.text,param:s}),i=o+s.text.length)}let r=n.slice(i);return r&&t.push({type:"text",value:r}),t}function Se(n,e){let t=[],i=[],r=0;for(let s of e){let o=n.indexOf(s.text,r);o===-1?i.push(s):(t.push(s),r=o+s.text.length)}return{valid:t,invalid:i}}function Pe(n,e){let t=e.dropdownTrigger??"auto",i=e.closeDropdownOnBlur??!0,r=n.filteredOptionsLength>0;if(n.inEditMode){let s=i?n.isFocused:!0;return r&&s}if(t==="auto"){let s=i?n.isFocused:!0,o=n.text.replace(/\s+$/,"").length,a=n.caretOffset==null||n.caretOffset>=o;return(r||n.isLoading)&&s&&a}return t==="manual"?(r||n.isLoading)&&n.pillTapped:!1}function Te(n,e){let t=ve(n.text,n.completedParams),i=n.suggestions.filter(g=>g.type!=="placeholder"),r=i[0],s=r?e.optionOverrides?.[r.type]:void 0,o=n.suggestions.filter(g=>g.type==="placeholder").map(g=>g.text).join(" "),a=I(n.text,Math.min(n.filterBase,n.text.length),o),l=n.lastRawQuery!==""||a>0?E(n.text,a,n.filterInProgress):"",p=r?s?s(l.trim()):r.options??[]:[],c=n.editingParam!=null&&n.editingAnchor!=null,u;if(c&&n.editingParam&&n.editingAnchor!=null){let g=n.editingParam.id,x=n.completedParams.some(y=>y.id===g),m=n.caretOffset??n.editingAnchor,f=x?"":n.text.slice(n.editingAnchor,m);u=w(n.editingParam.options,f)}else u=w(p,l);e.showNonTappableOptions===!1&&(u=u.filter(g=>g.is_tappable));let h=Pe({inEditMode:c,filteredOptionsLength:u.length,isFocused:n.isFocused,text:n.text,caretOffset:n.caretOffset,isLoading:n.isLoading,pillTapped:n.pillTapped},{dropdownTrigger:e.dropdownTrigger,closeDropdownOnBlur:e.closeDropdownOnBlur}),b=h&&n.activeDropdownIndex>=0&&!!u[n.activeDropdownIndex]?.is_tappable;return{segments:t,actionableSuggestions:i,filteredOptions:u,placeholderText:o,isDropdownOpen:h,isActivePillSelected:b}}function j(n){return n.mode==="fresh"?nt(n):it(n)}function nt(n){let{text:e,completedParams:t,suggestions:i,filterBase:r,filterInProgress:s}=n,a=i.filter(v=>v.type!=="placeholder")[0];if(!a?.options)return null;let d=i.filter(v=>v.type==="placeholder").map(v=>v.text).join(" "),l=I(e,r,d),p=E(e,l,s),c=O(a.options,p);if(!c)return null;let u=c.text.toLowerCase(),h=e.toLowerCase().lastIndexOf(u),b=h>=0?h:Math.max(0,e.length-c.text.length),g=b+c.text.length,x=e.slice(b,g),f=g<e.length&&e[g]===" "?g+1:g,y={id:crypto.randomUUID(),placeholder:"",type:a.type,text:x,kind:c.kind,suggestionType:a.type,suggestionPlaceholder:a.text,options:a.options??[],metadata:c.metadata};return{patch:{text:e,completedParams:[...t,y],suggestions:i.filter(v=>v!==a),filterBase:f,newParamId:y.id,caretOffset:f,activeDropdownIndex:-1},caretPos:f}}function it(n){let{text:e,completedParams:t,editingParam:i,editingAnchor:r,editingTail:s}=n;if(t.some(y=>y.id===i.id))return null;let o=e.slice(r,s),a=O(i.options,o);if(!a)return null;let d=a.text.toLowerCase(),l=o.toLowerCase().lastIndexOf(d),p=r+Math.max(0,l),c=p+a.text.length,u=e.slice(p,c),b=c<e.length&&e[c]===" "?c+1:c,g={id:crypto.randomUUID(),placeholder:"",type:i.suggestionType,text:u,kind:a.kind,suggestionType:i.suggestionType,suggestionPlaceholder:i.suggestionPlaceholder,options:i.options,metadata:a.metadata},x=t.length,m=0;for(let y=0;y<t.length;y++){let v=e.indexOf(t[y].text,m);if(v!==-1){if(v>=b){x=y;break}m=v+t[y].text.length}}let f=[...t];return f.splice(x,0,g),{patch:{text:e,completedParams:f,newParamId:g.id,filterBase:b,editingParam:null,editingAnchor:null,editingTail:null,caretOffset:b,activeDropdownIndex:-1},caretPos:b}}var V=class{constructor(e){this.deps=e}start(e){let t=this.deps.store.get();if(t.editingParam?.id===e)return;let i=t.completedParams.find(o=>o.id===e);if(!i)return;let r=0,s=-1;for(let o of t.completedParams){let a=t.text.indexOf(o.text,r);if(a!==-1){if(o.id===e){s=a;break}r=a+o.text.length}}s<0||this.deps.store.set({editingParam:i,editingAnchor:s,editingTail:s+i.text.length,caretOffset:s+i.text.length,activeDropdownIndex:-1})}exit(){this.deps.store.get().editingParam&&this.deps.store.set({editingParam:null,editingAnchor:null,editingTail:null,activeDropdownIndex:-1})}replaceRange(e){let t=this.deps.store.get(),i=t.editingParam,r=t.editingAnchor,s=t.editingTail;if(!i||r==null||s==null||!t.completedParams.some(d=>d.id===i.id))return!1;let o=t.text.slice(0,r)+e+t.text.slice(s),a=r+e.length;return this.deps.store.set(d=>({text:o,completedParams:d.completedParams.filter(l=>l.id!==i.id),editingTail:a,caretOffset:a,activeDropdownIndex:-1})),this.deps.scheduleSetCursor(a),this.tryPromote(),!0}caretAfterInput(e){let t=this.deps.store.get(),i={caretOffset:e};t.editingParam&&t.editingAnchor!=null&&e!=null&&(e<t.editingAnchor?(i.editingParam=null,i.editingAnchor=null,i.editingTail=null,i.activeDropdownIndex=-1):t.editingTail!=null&&(i.editingTail=Math.max(t.editingTail,e))),this.deps.store.set(i),this.tryPromote()}caretMove(e){let t=this.deps.store.get();if(t.editingParam&&t.editingAnchor!=null&&t.editingTail!=null&&e!=null&&(e<t.editingAnchor||e>t.editingTail)){this.deps.store.set({caretOffset:e,editingParam:null,editingAnchor:null,editingTail:null,activeDropdownIndex:-1});return}this.deps.store.set({caretOffset:e})}selectOption(e){let t=this.deps.store.get(),i=t.editingParam,r=t.editingAnchor,s=t.editingTail;if(!i||r==null||s==null)return;this.deps.fireTelemetry("option",{raw_query:S(t.text,t.completedParams).rawQuery,selected_option:e.text,other_options:i.options.filter(m=>m.text!==e.text).map(m=>m.text)});let o=t.text.slice(0,r),a=t.text.slice(s),d=r===0&&e.text.length>0?e.text[0].toUpperCase()+e.text.slice(1):e.text,l=a.length===0||a[0]!==" ",p=l?`${d} `:d,c=o+p+a,u=r+p.length+(l?0:1),h={id:crypto.randomUUID(),placeholder:"",type:i.suggestionType,text:d,kind:e.kind,suggestionType:i.suggestionType,suggestionPlaceholder:i.suggestionPlaceholder,options:i.options,metadata:e.metadata},b=t.completedParams.findIndex(m=>m.id===i.id),g=t.completedParams.filter(m=>m.id!==i.id),x=b>=0?Math.min(b,g.length):g.length;g.splice(x,0,h),this.deps.store.set({text:c,completedParams:g,newParamId:h.id,filterBase:u,editingParam:null,editingAnchor:null,editingTail:null,caretOffset:u,activeDropdownIndex:-1,pillTapped:!1,skipNextFetch:!0,inSelectionAnimation:!0}),this.deps.startSelectionAnimationTimer(),this.deps.scheduleSetCursor(u)}tryPromote(){let e=this.deps.store.get();if(!e.editingParam||e.editingAnchor==null||e.editingTail==null)return;let t=j({mode:"edit",text:e.text,completedParams:e.completedParams,editingParam:e.editingParam,editingAnchor:e.editingAnchor,editingTail:e.editingTail});t&&(this.deps.store.set(t.patch),this.deps.scheduleSetCursor(t.caretPos))}};function z(n,e,t){return e?{key:"enter",hint:"to proceed"}:t?{key:"tab",hint:"to select"}:n?{key:"\u2192",hint:"to skip"}:{key:"tab",hint:"to select"}}var Ce="data-aia-key";function X(n,e,t){let i=new Map;for(let o of Array.from(n.children)){let a=o.getAttribute(Ce);a!=null&&i.set(a,o)}let r=new Set,s=[];for(let o=0;o<e.length;o++){let a=e[o],d=t.keyOf(a,o);r.add(d);let l=i.get(d);l||(l=t.create(a,o),l.setAttribute(Ce,d)),t.update?.(l,a,o),n.children[o]!==l&&n.insertBefore(l,n.children[o]??null),s.push(l)}for(let[o,a]of i)r.has(o)||a.remove();return s}var Ee=[125,69];function we(n,e){return e?1:n===0?.7:n===1?.4:.2}function Y(n,e,t,i,r=!1,s=!1,o=!1){let a=n.querySelector(".magicx-aia-pill-list");if(a||(a=document.createElement("span"),a.className="magicx-aia-pill-list",n.appendChild(a)),s&&e.length===0){a.setAttribute("data-aia-pill-list-loading",""),a.innerHTML="";for(let d=0;d<Ee.length;d++){let l=Ee[d],p=document.createElement("span");p.setAttribute("data-aia-pill-skeleton",""),p.className=`magicx-aia-pill magicx-aia-pill--skeleton${r?" magicx-aia-pill--rounded":""}`,p.style.width=`${l}px`,p.style.opacity=String(we(d,!1)),a.appendChild(p)}return}s?a.setAttribute("data-aia-pill-list-loading",""):a.removeAttribute("data-aia-pill-list-loading");for(let d of a.querySelectorAll("[data-aia-pill-skeleton]"))d.remove();X(a,e,{keyOf:d=>`${d.type}-${d.text}`,create:d=>{let l=document.createElement("button");return l.type="button",l.tabIndex=-1,l.setAttribute("data-aia-pill",""),l.setAttribute("contenteditable","false"),l.textContent=d.text,l.addEventListener("mousedown",p=>p.preventDefault()),l},update:(d,l,p)=>{let c=d,u=o&&p===t&&!s,h=["magicx-aia-pill"];r&&h.push("magicx-aia-pill--rounded"),u&&h.push("magicx-aia-pill--active"),s&&h.push("magicx-aia-pill--skeleton"),c.className=h.join(" "),c.style.width="",c.style.opacity=String(we(p,u)),s?(c.setAttribute("data-aia-loading",""),c.disabled=!0,c.onclick=null):(c.removeAttribute("data-aia-loading"),c.disabled=!1,c.onclick=()=>i(p))}})}function ae(n){n.querySelector(".magicx-aia-pill-list")?.remove()}function Ae(n,e,t,i,r,s,o){let a=n.querySelector(".aia-grid");if(e.length===0){a?.remove();return}a||(a=document.createElement("div"),a.className="aia-grid magicx-aia-grid",a.setAttribute("data-scroll",""),a.style.setProperty("--aia-grid-min","250px"),a.style.setProperty("--aia-grid-max","250px"),a.style.setProperty("--aia-grid-gap","0"),n.appendChild(a)),rt(a,e,t,i,r,s,o)}function rt(n,e,t,i,r,s,o){let a=o?"1":"0";X(n,e,{keyOf:d=>`${d.text}\0${a}`,create:d=>ot(d,o),update:(d,l,p)=>{let c=p===t&&!o;d.id=`${s}-option-${p}`,d.dataset.aiaIndex=String(p),d.setAttribute("aria-selected",String(c)),d.classList.toggle("magicx-aia-option--highlighted",c),!o&&l.is_tappable?(d.onclick=()=>{d.classList.add("magicx-aia-option--pressed"),i(l),setTimeout(()=>d.classList.remove("magicx-aia-option--pressed"),500)},d.onmouseenter=()=>{let u=Number.parseInt(d.dataset.aiaIndex??"-1",10);u>=0&&r(u)}):(d.onclick=null,d.onmouseenter=null)}})}function ot(n,e){let t=document.createElement("div");t.setAttribute("role","option"),t.setAttribute("data-aia-option",""),e&&t.setAttribute("data-aia-loading",""),t.tabIndex=e||!n.is_tappable?-1:0;let i=["magicx-aia-option"];n.is_tappable?i.push("magicx-aia-option--tappable"):i.push("magicx-aia-option--non-tappable"),t.className=i.join(" ");let r=document.createElement("div");r.className="magicx-aia-streaks",t.appendChild(r);let s=document.createElement("div");s.className="magicx-aia-streaks-vert",t.appendChild(s);let o=document.createElement("span");o.className="magicx-aia-option-content";let a=document.createElement("span");if(a.className="magicx-aia-option-text",a.textContent=n.icon?`${n.icon} ${n.text}`:n.text,o.appendChild(a),n.tag){let d=document.createElement("span");d.className="magicx-aia-option-tag",d.textContent=n.tag,o.appendChild(d)}return t.appendChild(o),t}var st=[159,119,164];function J(n){let e=document.createElement("div");return e.id=n,e.setAttribute("role","listbox"),e.setAttribute("data-aia-dropdown",""),e.className="magicx-aia-dropdown",e.addEventListener("mousedown",t=>t.preventDefault()),e}function Z(n,e){let{filteredOptions:t,activeIndex:i,isOpen:r,isLoading:s,pills:o,showPills:a,isActivePillSelected:d,onSelect:l,onHighlight:p,onPillClick:c}=e,u=a&&o.length>0,h=t.length>0,b=r&&(h||u||s);if(b?n.classList.add("magicx-aia-dropdown--visible"):n.classList.remove("magicx-aia-dropdown--visible"),s?n.setAttribute("data-aia-loading",""):n.removeAttribute("data-aia-loading"),!b)return;let g=n.querySelector(".aia-stack");g||(g=document.createElement("div"),g.className="aia-stack",g.style.setProperty("--aia-stack-space","8px"),n.appendChild(g));let x=u||s&&a,m=g.querySelector(".magicx-aia-pill-bar");x?(m||(m=document.createElement("div"),m.className="magicx-aia-pill-bar aia-cluster",m.setAttribute("data-nowrap",""),m.setAttribute("data-aia-pillbar",""),g.insertBefore(m,g.firstChild)),Y(m,o,0,c,!0,s,d)):m&&m.remove(),Ae(g,t,i,l,p,e.listboxId,s);let f=g.querySelector(".magicx-aia-skeleton-bars");if(s&&!h){if(!f){f=document.createElement("div"),f.className="magicx-aia-skeleton-bars",f.setAttribute("data-aia-skeleton-bars","");for(let T of st){let A=document.createElement("span");A.className="magicx-aia-skeleton-bar",A.style.width=`${T}px`,f.appendChild(A)}g.appendChild(f)}}else f&&f.remove();let y=g.querySelector(".magicx-aia-footer")??lt(),v=i>=0&&!!t[i]?.is_tappable;at(y,z(e.hasTabbedToHighlight,v,e.isInputEmpty)),g.lastElementChild!==y&&g.appendChild(y)}function at(n,{key:e,hint:t}){let i=n.querySelector(".magicx-aia-footer-key"),r=n.querySelector(".magicx-aia-footer-hint");!i||!r||(i.textContent!==e&&(i.textContent=e),r.textContent!==t&&(r.textContent=t))}function lt(){let n=document.createElement("footer");n.className="magicx-aia-footer",n.setAttribute("data-aia-footer","");let e=document.createElement("div");e.className="magicx-aia-footer-divider";let t=document.createElement("div");t.className="aia-cluster",t.setAttribute("data-align","center"),t.setAttribute("data-justify","between"),t.setAttribute("data-nowrap","");let i=document.createElement("div");i.className="aia-cluster",i.setAttribute("data-align","center"),i.style.setProperty("--aia-cluster-gap","5px");let r=document.createElement("kbd");r.className="magicx-aia-footer-key",r.textContent="tab";let s=document.createElement("span");s.className="magicx-aia-footer-hint",s.textContent="to select",i.append(r,s);let o=document.createElement("a");o.className="aia-cluster magicx-aia-footer-brand-link",o.setAttribute("data-align","center"),o.href="https://ai-autocomplete.com",o.target="_blank",o.rel="noopener noreferrer",o.style.setProperty("--aia-cluster-gap","2px");let a=document.createElement("span");a.className="magicx-aia-footer-brand",a.textContent="AI";let d=document.createElement("span");return d.className="magicx-aia-footer-badge",d.textContent="Autocomplete",o.append(a,d),t.append(i,o),n.append(e,t),n}function Ie(n,e){let t=J(e.listboxId);return n.appendChild(t),{dropdown:t}}function le(n,e,t){Z(n.dropdown,{suggestions:e.actionableSuggestions.length>0?[{...e.actionableSuggestions[0],options:e.filteredOptions}]:[],filteredOptions:e.filteredOptions,activeIndex:e.activeDropdownIndex,isOpen:e.isDropdownOpen,isLoading:e.isLoading&&!e.editingParam&&!e.inSelectionAnimation,listboxId:t.listboxId,pills:e.actionableSuggestions,showPills:!0,isActivePillSelected:e.isActivePillSelected,hasTabbedToHighlight:e.hasTabbedToHighlight,isInputEmpty:e.text.trim().length===0,onSelect:t.selectOption,onHighlight:i=>t.store.set({activeDropdownIndex:i}),onPillClick:t.setActivePill})}function ee(n){let{input:e,segments:t,newParamId:i,editingParamId:r,placeholderText:s,isFocused:o}=n,a=t.length===0;e.dataset.aiaEmpty=a?"true":"false",a&&s?e.dataset.placeholder=s:delete e.dataset.placeholder;let d=t.map(x=>`${x.type}:${x.value}`).join("\0"),l=e.dataset.segKey??"",p=e.dataset.newParamId??"",c=e.dataset.editingParamId??"";if(d===l&&(i??"")===p&&(r??"")===c)return;let u=o?C(e):null;e.dataset.segKey=d,e.dataset.newParamId=i??"",e.dataset.editingParamId=r??"";let h=e.ownerDocument??document,b=h.createDocumentFragment(),g=0;for(let x of t)if(g+=x.value.length,x.type==="completed"){let m=h.createElement("strong");m.dataset.seg="completed",m.dataset.paramId=x.param.id;let f=x.param.id===i,y=x.param.id===r,v=["magicx-aia-segment","magicx-aia-segment--completed"];f&&v.push("magicx-aia-shimmer-revealed","magicx-aia-shimmer-sweep"),y&&v.push("magicx-aia-segment--editing"),m.className=v.join(" "),m.textContent=x.value,b.appendChild(m)}else b.appendChild(h.createTextNode(x.value));e.replaceChildren(b),e.dataset.aiaTextLength=String(g),u!=null&&P(e,Math.max(0,Math.min(u,g)))}var dt='<svg width="18" height="18" viewBox="0 0 18 18" fill="none" role="img" aria-label="Submit"><path d="M9 14V4M9 4L4 9M9 4L14 9" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';function Oe(){let n=document.createElement("button");return n.type="button",n.className="magicx-aia-submit",n.setAttribute("aria-label","Submit"),n.setAttribute("data-aia-submit",""),n.innerHTML=dt,n}function ct(){let n=document.createElement("div");return n.setAttribute("contenteditable","plaintext-only"),n.contentEditable==="plaintext-only"}function De(n,e){let t=e.firstElementChild;if(!t){e.removeAttribute("data-aia-pill-wrapped");return}let i=t.getBoundingClientRect(),r=n.getBoundingClientRect();i.top>=r.bottom-2?e.setAttribute("data-aia-pill-wrapped",""):e.removeAttribute("data-aia-pill-wrapped")}function ke(n,e){let{listboxId:t}=e,i=J(t);n.appendChild(i);let r=document.createElement("div");r.className="magicx-aia-input-wrapper",n.appendChild(r);let s=document.createElement("div");s.className="magicx-aia-editor",s.setAttribute("data-aia-editor",""),r.appendChild(s);let o=document.createElement("div");o.className="magicx-aia-input",o.setAttribute("data-aia-input",""),o.setAttribute("contenteditable",ct()?"plaintext-only":"true"),o.setAttribute("role","combobox"),o.setAttribute("aria-autocomplete","list"),o.setAttribute("aria-haspopup","listbox"),o.setAttribute("aria-controls",t),o.setAttribute("aria-expanded","false"),o.setAttribute("spellcheck","true"),o.setAttribute("enterkeyhint","send"),s.appendChild(o);let a=document.createElement("span");a.className="magicx-aia-pill-list-container",a.setAttribute("data-aia-pill-list-container",""),s.appendChild(a);let d=null,l=null;e.submitButton===void 0?(d=Oe(),r.appendChild(d),l=d):e.submitButton!==null&&(l=e.submitButton,l.hasAttribute("data-aia-submit")||l.setAttribute("data-aia-submit",""),r.appendChild(l));let p=new AbortController,{signal:c}=p,u=!1,h=0,b=()=>{let m=L(o),y=m.length>0&&m[0]!==m[0].toUpperCase()?m[0].toUpperCase()+m.slice(1):m;e.handleChange(y)},g=()=>{let m=(o.ownerDocument??document).getSelection();if(!m||m.rangeCount===0)return null;let f=m.anchorNode;return!f||!o.contains(f)?null:(f.nodeType===Node.ELEMENT_NODE?f:f.parentElement)?.closest('strong[data-seg="completed"][data-param-id]')?.dataset.paramId??null};r.addEventListener("click",m=>{m.target?.closest("[data-aia-pill]")||o.focus()},{signal:c}),o.addEventListener("input",()=>{u||(h=performance.now(),b(),e.handleCaretAfterInput(C(o)))},{signal:c});let x=o.ownerDocument??document;if(x.addEventListener("selectionchange",()=>{let m=x.getSelection();if(!m||m.rangeCount===0||!o.contains(m.anchorNode))return;let f=g(),y=e.store.get().editingParam?.id??null;if(f&&f!==y){e.startEditingParam(f);return}performance.now()-h<50||e.handleCaretMove(C(o))},{signal:c}),o.addEventListener("compositionstart",()=>{u=!0},{signal:c}),o.addEventListener("compositionend",()=>{u=!1,b()},{signal:c}),o.addEventListener("beforeinput",m=>{let f=m,y=f.inputType;if(y==="insertParagraph"||y==="insertLineBreak"||y==="insertFromDrop"){m.preventDefault();return}if(y.startsWith("insert")||y.startsWith("delete")){let v=y.startsWith("delete")?"":f.data??"";e.replaceEditingRange(v)&&m.preventDefault()}},{signal:c}),o.addEventListener("paste",m=>{m.preventDefault();let f=(m.clipboardData?.getData("text/plain")??"").replace(/\r?\n/g," ");if(!f)return;let y=o.ownerDocument??document,v=y.getSelection();if(!v||v.rangeCount===0)return;let T=v.getRangeAt(0);if(!o.contains(T.startContainer))return;T.deleteContents();let A=y.createTextNode(f);T.insertNode(A),T.setStartAfter(A),T.collapse(!0),v.removeAllRanges(),v.addRange(T),b()},{signal:c}),o.addEventListener("keydown",m=>e.handleKeyDown(m),{signal:c}),o.addEventListener("focus",()=>e.store.set({isFocused:!0}),{signal:c}),o.addEventListener("blur",()=>e.store.set({isFocused:!1}),{signal:c}),l&&l.addEventListener("click",m=>{let f=e.store.get();if(!(!!f.text||f.completedParams.length>0)||!e.onSubmit)return;m.stopPropagation();let{rawQuery:v,completedParams:T}=S(f.text,f.completedParams);e.onSubmit({query:f.text.trim(),raw_query:v,completed_params:T}),e.afterSubmit?.()},{signal:c}),e.autoFocus!==!1&&o.focus(),typeof ResizeObserver<"u"){let m=new ResizeObserver(()=>De(o,a));m.observe(o),p.signal.addEventListener("abort",()=>m.disconnect(),{once:!0})}return{input:o,inlinePillContainer:a,dropdown:i,submitButton:d,abort:p}}function de(n,e,t){let{input:i,inlinePillContainer:r,dropdown:s,submitButton:o}=n,{pillPlacement:a,setActivePill:d,selectOption:l,store:p}=t;i.setAttribute("aria-expanded",String(e.isDropdownOpen));let c=e.activeDropdownIndex>=0?`${t.listboxId}-option-${e.activeDropdownIndex}`:"";if(c?i.setAttribute("aria-activedescendant",c):i.removeAttribute("aria-activedescendant"),o){let x=!!e.text||e.completedParams.length>0;o.disabled=!x}let u=i.dataset.newParamId??"",h=e.newParamId!==null&&e.newParamId!==u;if(ee({input:i,segments:e.segments,newParamId:e.newParamId,editingParamId:e.editingParam?.id??null,placeholderText:e.placeholderText,isFocused:e.isFocused}),a==="inline"){let x=e.isLoading&&!e.editingParam&&!e.inSelectionAnimation;x||e.actionableSuggestions.length>0?Y(r,e.actionableSuggestions,0,d,!1,x,e.isActivePillSelected):ae(r)}else ae(r);De(i,r),h?(i.focus(),P(i,e.caretOffset??e.text.length)):e.isFocused&&L(i)!==e.text&&P(i,e.text.length);let b=e.editingParam?{type:e.editingParam.suggestionType,text:e.editingParam.suggestionPlaceholder,required:!0,options:e.editingParam.options}:null,g=b??e.actionableSuggestions[0];Z(s,{suggestions:g?[{...g,options:e.filteredOptions}]:[],filteredOptions:e.filteredOptions,activeIndex:e.activeDropdownIndex,isOpen:e.isDropdownOpen,isLoading:e.isLoading&&!e.editingParam&&!e.inSelectionAnimation,listboxId:t.listboxId,pills:b?[b]:e.actionableSuggestions,showPills:a==="dropdown",isActivePillSelected:e.isActivePillSelected,hasTabbedToHighlight:e.hasTabbedToHighlight,isInputEmpty:e.text.trim().length===0,onSelect:l,onHighlight:x=>p.set({activeDropdownIndex:x}),onPillClick:d})}function Me(n,e){let t=n.actionableSuggestions[0];if(!t)return null;let i=n.filterBase,r=n.text.slice(0,i),s=r.length===0&&n.text.length===0,o=r.length===0&&n.text.length>0&&n.placeholderText.length>0&&n.placeholderText.toLowerCase().startsWith(n.text.toLowerCase());(s||o)&&n.placeholderText&&(r=`${n.placeholderText} `);let a=fe(r,e.text);a>0&&(r=r.slice(0,r.length-a));let d=r.length>0&&r[r.length-1]!==" ",l=`${r}${d?" ":""}${e.text} `,p=(s||o)&&l.length>0?l[0].toUpperCase()+l.slice(1):l,c=p.toLowerCase().lastIndexOf(e.text.toLowerCase()),u=c>=0?p.slice(c,c+e.text.length):e.text,h={id:crypto.randomUUID(),placeholder:"",type:t.type,text:u,kind:e.kind,suggestionType:t.type,suggestionPlaceholder:t.text,options:t.options??[],metadata:e.metadata},b=n.actionableSuggestions.length-1;return{patch:{text:p,filterBase:p.length,completedParams:[...n.completedParams,h],newParamId:h.id,caretOffset:p.length,pillTapped:!1,activeDropdownIndex:-1,skipNextFetch:b>0,inSelectionAnimation:!0},telemetry:{selectedOption:e.text,otherOptions:n.filteredOptions.filter(g=>g.text!==e.text).map(g=>g.text)},consumedSuggestion:t,remainingActionable:b}}function te(n){let e=n,t=new Set;return{get:()=>e,set:i=>{let r=typeof i=="function"?i(e):i,s={...e,...r},o=e;e=s;for(let a of t)a(s,o)},subscribe:i=>(t.add(i),()=>{t.delete(i)})}}function Le(n,e){let t,i,r=s=>(s!==t&&(t=s,i=e(s)),i);return{get:()=>{let s=n.get();return{...s,...r(s)}},set:s=>{typeof s=="function"?n.set(o=>{let a={...o,...r(o)};return s(a)}):n.set(s)},peek:s=>{let o={...n.get(),...s};return{...o,...e(o)}},subscribe:s=>n.subscribe((o,a)=>{let d={...a,...r(a)},l={...o,...r(o)};s(l,d)})}}var ce=!1;function Re(){if(ce||typeof document>"u")return;if(document.querySelector("style[data-magicx-aia]")){ce=!0;return}ce=!0;let n=document.createElement("style");n.setAttribute("data-magicx-aia",""),n.textContent=pt,document.head.appendChild(n)}var pt=`@layer layout {
|
|
2
2
|
.aia-stack {
|
|
3
3
|
display: flex;
|
|
4
4
|
flex-direction: column;
|