"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react/jsx-runtime"),e=require("./index-BmF6F_Ux.js"),o=require("react"),L=require("./Textarea-ss7Y-nB5.js"),R=1e4,O=async(i,r,p)=>e.lightdashApi({url:`/ai/${i}/tooltip/generate`,method:"POST",body:JSON.stringify(r),signal:p}),$=({projectUuid:i,fields:r,onSuccess:p})=>{const g=o.useRef(null),t=e.useMutation({onSuccess:p,mutationFn:({prompt:d,currentHtml:u})=>{if(!i)throw new Error("Project UUID is required");g.current?.abort();const h=new AbortController;g.current=h;const m=setTimeout(()=>{h.abort()},R),b=r.map(c=>({name:c}));return O(i,{prompt:d,fieldsContext:b,currentHtml:u},h.signal).finally(()=>{clearTimeout(m)})}}),l=o.useCallback((d,u)=>{!i||!d.trim()||t.mutate({prompt:d,currentHtml:u})},[i,t]),C=o.useCallback(()=>{t.reset()},[t]);return{generate:l,reset:C,generatedResult:t.data??null,isLoading:t.isLoading,error:t.error}},P="_container_1x48z_1",z="_inputContainer_1x48z_17",D="_textareaRoot_1x48z_21",B="_textareaInput_1x48z_25",G="_generateButton_1x48z_52",T={container:P,inputContainer:z,textareaRoot:D,textareaInput:B,generateButton:G},W=({fields:i,currentHtml:r,onApply:p})=>{const{projectUuid:g}=e.useParams(),[t,l]=o.useState(""),C=o.useCallback(c=>{p(c.html),l("")},[p]),{generate:d,isLoading:u,error:h}=$({projectUuid:g,fields:i,onSuccess:C}),m=o.useCallback(()=>{const c=t.trim()||"generate a helpful tooltip";d(c,r)},[d,t,r]),b=o.useCallback(c=>{c.key==="Enter"&&!c.shiftKey&&(c.preventDefault(),m())},[m]);return e.useHotkeys([["mod+Enter",m]],[],!0),n.jsxs(e.Box$1,{className:T.container,children:[n.jsxs(e.Group$1,{spacing:"xs",mb:"xs",children:[n.jsx(e.MantineIcon,{icon:e.IconSparkles,color:"indigo.4"}),n.jsx(e.Text$1,{size:"xs",c:"ldDark.9",fw:500,children:r?"Improve your tooltip code with AI":"Generate tooltip with AI"})]}),n.jsxs(e.Box$1,{className:T.inputContainer,children:[n.jsx(L.Textarea,{value:t,onChange:c=>l(c.target.value),onKeyDown:b,placeholder:"Describe what you want to show in the tooltip...",disabled:u,classNames:{root:T.textareaRoot,input:T.textareaInput},minRows:2,maxRows:4,autosize:!0}),n.jsx(e.ActionIcon$1,{size:"sm",radius:"xl",onClick:m,disabled:u||!t.trim()&&!!r,loading:u,className:T.generateButton,children:n.jsx(e.MantineIcon,{icon:e.IconArrowUp,color:"ldGray.0",size:16,stroke:2})})]}),h&&n.jsx(e.Text$1,{size:"xs",c:"red",mt:"xs",children:"Failed to generate. Please try again."})]})},V="_editorWrapper_xvb94_1",q="_placeholderText_xvb94_30",k={editorWrapper:V,placeholderText:q},H={cursorBlinking:"smooth",folding:!1,minimap:{enabled:!1},scrollBeyondLastLine:!1,wordWrap:"off",quickSuggestions:!0,contextmenu:!1,automaticLayout:!0,tabSize:2,lineNumbers:"off",glyphMargin:!1,lineDecorationsWidth:0,lineNumbersMinChars:0,fixedOverflowWidgets:!0};let _=null;const U=(i,r,p)=>{_&&(console.debug("Clearing Monaco completion provider"),_.dispose()),_=i.languages.registerCompletionItemProvider(r,{provideCompletionItems:(g,t)=>{const l=g.getWordUntilPosition(t),C={startLineNumber:t.lineNumber,endLineNumber:t.lineNumber,startColumn:l.startColumn,endColumn:l.endColumn};return{suggestions:p.map(u=>{const m=g.getValueInRange({startLineNumber:t.lineNumber,endLineNumber:t.lineNumber,startColumn:t.column-1,endColumn:t.column})==="$"?`{${u}} `:`\${${u}}`;return{label:`${u}`,kind:i.languages.CompletionItemKind.Class,insertText:m,range:C}})}},triggerCharacters:["$"]})},K=19,F=16,J=10,w=i=>Math.min(i||1,J)*K+F,X=({fields:i})=>{const{visualizationConfig:r}=e.useVisualizationContext(),{colorScheme:p}=e.useMantineColorScheme(),g=e.useAmbientAiEnabled(),t=e.isCartesianVisualizationConfig(r),[l,C]=o.useState(t&&r.chartConfig.tooltip||""),[d]=e.useDebouncedValue(l,1e3),[u,h]=o.useState(w(1)),m=o.useRef(null),b=o.useRef(null),c=o.useRef(t?!!r.chartConfig.tooltip:!1),y=o.useRef(t&&r.chartConfig.tooltip||""),[f,v]=o.useState(t?!!r.chartConfig.tooltip:!1);o.useEffect(()=>{y.current=l},[l]),o.useEffect(()=>{if(!t)return;const{setTooltip:s}=r.chartConfig;c.current!==f?(s(f?y.current:""),c.current=f):f&&s(d)},[t,f,d,r.chartConfig]);const N=s=>{C(s??"");const a=(s??"").split(`
`).length;h(w(a))},E=o.useCallback(s=>{C(s);const a=s.split(`
`).length;h(w(a))},[]),[I,M]=o.useState();e.useDeepCompareEffect(()=>{const s="monaco-overflow-container";let a=document.getElementById(s);if(!a){const x=document.createElement("div");x.className="monaco-editor",a=document.createElement("div"),a.id=s,x.appendChild(a),document.getElementById("root")?.appendChild(x)}M({...H,overflowWidgetsDomNode:a})},[I]);const S=o.useCallback(s=>{U(s,"html",i);const a=e.getLightdashMonacoTheme("light"),x=e.getLightdashMonacoTheme("dark");s.editor.defineTheme("lightdash-light",{base:"vs",inherit:!0,rules:[],colors:{...a.colors}}),s.editor.defineTheme("lightdash-dark",{base:"vs-dark",inherit:!0,rules:[],colors:{...x.colors}})},[i]),j=o.useCallback(s=>{m.current=s;const a=s.getModel();if(a){const x=a.getLineCount();h(w(x)),b.current=a.onDidChangeContent(()=>{const A=a.getLineCount();h(w(A))})}},[]);return o.useEffect(()=>()=>{b.current?.dispose()},[]),I?n.jsxs(e.Stack,{gap:"xs",children:[n.jsxs(e.Group,{gap:"xs",align:"center",children:[n.jsx(e.Config.Label,{children:"Custom"}),n.jsx(e.Tooltip$1,{withinPortal:!0,maw:350,variant:"xs",multiline:!0,label:"Use this input to enhance chart tooltips with additional content. You can incorporate HTML code and include dynamic values using the format ${variable_name}.\n                                Click here to read more about this on our docs.",children:n.jsx(e.MantineIcon,{onClick:()=>{window.open("https://docs.lightdash.com/references/custom-tooltip","_blank")},icon:e.IconHelpCircle,size:"md",display:"inline",color:"ldGray.5",style:{cursor:"pointer"}})}),n.jsx(e.Switch,{checked:f,onChange:()=>v(!f)})]}),n.jsx(e.Collapse$1,{in:f,children:n.jsxs(e.Paper,{className:k.editorWrapper,radius:"md",withBorder:!0,pos:"relative",children:[l?.length===0?n.jsx(e.Text,{ml:"sm",pos:"absolute",w:"400px",c:"ldGray.5",fz:"xs",className:k.placeholderText,style:{zIndex:e.getDefaultZIndex("overlay")},children:"- Total orders: ${orders_total_amount}"}):null,n.jsx(e.de,{beforeMount:S,onMount:j,value:l,options:I,onChange:N,language:"html",height:`${u}px`,width:"100%",theme:p==="dark"?"lightdash-dark":"lightdash-light",wrapperProps:{id:"tooltip-editor-wrapper"}}),g&&n.jsx(W,{fields:i,currentHtml:l||void 0,onApply:E})]})})]}):null};exports.TooltipConfig=X;
//# sourceMappingURL=TooltipConfig-BjtxFVfY.js.map
