import{k as e}from"./utils-BakkG8Vx.js";import{d as t,m as n,s as r,t as i,y as a}from"./hooks.module-5xe-uC6c.js";import{I as o,N as s}from"./content_renderer-CoENEE63.js";import{s as c}from"./options-CDt8ZGCW.js";import{t as l}from"./jsxRuntime.module--xb8gfv5.js";import{A as u,B as d,D as f,G as p,K as m,O as h,U as g,_,h as v,s as y,x as b}from"./hooks-CSTgmcXO.js";import{n as x,r as S,t as C}from"./constants-BXsOmR6c.js";import{n as w,t as T}from"./TouchBar-5MFu7aFl.js";t();function E({className:e,content:t,mime:r,editorRef:a,containerRef:o,onInitialized:s,lineWrapping:c,...u}){let f=d(o),p=i();return n(()=>{if(!f.current)return;let e=new x({parent:f.current,...u});return p.current=e,typeof a==`function`?a(e):a&&(a.current=e),s?.(),()=>e.destroy()},[]),n(()=>{let e=p.current;e?.setText(t??``),e?.setMimeType(r),e?.clearHistory()},[t]),n(()=>{p.current?.setMimeType(r)},[r]),n(()=>p.current?.setLineWrapping(!!c),[c]),n(()=>{(u.indentSize!=null||u.useTabs!=null)&&p.current?.setIndent(u.indentSize??4,!!u.useTabs)},[u.indentSize,u.useTabs]),l(`pre`,{ref:f,className:e})}t();function D({note:e,viewScope:t,ntxId:i,parentComponent:a,editorRef:o}){let[s,c]=r(``),u=b(e),[d]=f(e,`tabWidth`),[p]=h(e,`indentWithTabs`),[m]=h(e,`wrapLines`);return n(()=>{if(!u)return;let n=u.content;t?.viewMode===`source`&&(n=O(e,n)),c(n)},[u]),l(A,{ntxId:i,parentComponent:a,editorRef:o,className:`note-detail-readonly-code-content`,content:s,mime:e.mime,readOnly:!0,...d!=null&&{indentSize:d},...p!=null&&{useTabs:p},...m!=null&&{lineWrapping:m}})}function O(t,n){if(t.type===`text`)return e.formatHtml(n);if(t.type!==`code`&&t.mime===`application/json`&&n.length<512e3)try{return JSON.stringify(JSON.parse(n),null,4)}catch{}return n}function k({note:e,ntxId:t,noteContext:n,debounceUpdate:r,parentComponent:s,updateInterval:d,noteType:p=`code`,onContentChanged:g,dataSaved:b,placeholder:x,editorRef:S,...C}){let E=i(null),D=i(null),O=e=>{E.current=e,typeof S==`function`?S(e):S&&(S.current=e)},[k]=m(`vimKeymapEnabled`),[j]=f(e,`tabWidth`),[M]=h(e,`indentWithTabs`),[N]=h(e,`wrapLines`),P=u(e,`mime`),F=y({note:e,noteType:p,noteContext:n,getData:()=>({content:E.current?.getText()??``}),onContentChange:t=>{let n=E.current;n&&(n.setText(t??``),n.setMimeType(e.mime),n.clearHistory())},dataSaved:b,updateInterval:d});return _({async runActiveNoteCommand(e){return e.ntxId===t&&await F.updateNowIfNecessary(),await s?.parent?.triggerCommand(`runActiveNote`,e)}}),v(`code-detail`,D,s,t),l(a,{children:[l(A,{ntxId:t,parentComponent:s,editorRef:O,containerRef:D,mime:P??`text/plain`,className:`note-detail-code-editor`,placeholder:x??c(`editable_code.placeholder`),vimKeybindings:k,tabIndex:300,onContentChanged:()=>{r&&F.resetUpdateTimer(),F.scheduleUpdate(),E.current&&g&&g(E.current.getText())},...C,...j!=null&&{indentSize:j},...M!=null&&{useTabs:M},...N!=null&&{lineWrapping:N}}),l(T,{children:(e?.mime.startsWith(`application/javascript`)||e?.mime===`text/x-sqlite;schema=trilium`)&&l(w,{icon:`NSImageNameTouchBarPlayTemplate`,click:()=>o.triggerCommand(`runActiveNote`)})})]})}function A({parentComponent:e,ntxId:t,containerRef:a,editorRef:o,mime:c,onInitialized:u,lineWrapping:f,noBackgroundChange:h,..._}){let v=i(null),y=d(a),b=i($.Deferred()),[x]=m(`codeLineWrapEnabled`),[w]=p(`codeNoteTheme`),[T]=p(`codeNoteTabWidth`),[D]=m(`codeNoteIndentWithTabs`),[O,k]=r();return n(()=>{!O||h||e?.$widget.closest(`.scrolling-container`).css(`--code-background-color`,O)},[O]),n(()=>{if(v.current&&w.startsWith(`default:`)){let e=S(w.substring(C.length));e&&v.current.setTheme(e).then(()=>{let e=y.current?.querySelector(`.cm-editor`);e&&k(window.getComputedStyle(e).backgroundColor)})}},[v,w]),g(`executeWithCodeEditor`,async({resolve:e,ntxId:n})=>{n===t&&(await b.current.promise(),e(v.current))}),g(`executeWithContentElement`,async({resolve:e,ntxId:n})=>{n===t&&(await b.current.promise(),e(s(y)))}),g(`scrollToEnd`,({ntxId:e})=>{if(e!==t)return;let n=v.current;n&&(n.scrollToEnd(),n.focus())}),g(`focusOnDetail`,({ntxId:e})=>{e===t&&v.current?.focus()}),l(E,{..._,mime:c,editorRef:v,containerRef:y,lineWrapping:f??x,indentSize:_.indentSize??(parseInt(T)||4),useTabs:_.useTabs??D,onInitialized:()=>{y.current&&(typeof a==`function`?a(y.current):a&&(a.current=y.current)),v.current&&(typeof o==`function`?o(v.current):o&&(o.current=v.current)),b.current.resolve(),u?.()}})}export{k as n,D as r,A as t};