import{a as e,l as t}from"./preact.module-CNakwbc9.js";import{d as n,l as r,s as i,t as a}from"./hooks.module-BrXTvcfS.js";import{Dt as o,H as s,Ot as c}from"./content_renderer-DOj8g78V.js";import{a as l}from"./i18n-BaLR3UdQ.js";import{t as u}from"./jsxRuntime.module-Cao1NCKs.js";import{o as d}from"./react_utils-Bz0ffU2Y.js";import{J as f}from"./hooks-ClTXKdUl.js";import{t as p}from"./note_autocomplete-BT55HtG6.js";import{t as m}from"./Modal-hBkTa-Ee.js";import{t as h}from"./Button-Du0dYm4Z.js";import{t as g}from"./NoteAutocomplete-Cpc7J_0A.js";import{t as _}from"./FormGroup-DqPd4bBP.js";import{t as v}from"./FormRadioGroup-CxQQUk-2.js";n(),t();function y(e){let t=/<a\b([^>]*)>(<\/a>)?/g,n=[],r;for(;r=t.exec(e);){let e=r[1];if(/\bhref\s*=/.test(e))continue;let t=/\bid\s*=\s*"([^"]+)"/.exec(e)??/\bid\s*=\s*'([^']+)'/.exec(e);if(!t)continue;let i=t[1];n.includes(i)||n.push(i)}return n}function b(){let[t,n]=i(),[b,x]=i(``),[S,C]=i(),[w,T]=i(null),[E,D]=i([]),[O,k]=i(``),[A,j]=i(``),[M,N]=i(!1),P=a(!1);f(`showAddLinkDialog`,e=>{n(e),N(!0)}),r(()=>{t?.hasSelection?C(`hyper-link`):C(`reference-link`)},[t]);async function F(e){let t=await o.getNoteTitle(e);j(t),x(t)}r(()=>{let e=()=>C(e=>e===`external-link`?`reference-link`:e);if(!w){e(),D([]),k(``);return}let t=!1;if(w.notePath){let n=o.getNoteIdFromUrl(w.notePath);n&&(F(n),(async()=>{let e=await s.getNote(n);if(t||!e)return;let r=e.getLabels(`internalBookmark`).map(e=>e.value);if(r.length===0&&e.type===`text`){let n=await e.getContent();if(t)return;typeof n==`string`&&(r=y(n))}D(r),k(``)})()),e()}return w.externalLink&&(x(w.externalLink),C(`external-link`)),()=>{t=!0}},[w]),r(()=>{x(O?`${A} - ${O}`:A)},[O,A]);function I(){let e=d(R);t?.text?p.setText(e,t.text):p.showRecentNotes(e),e.trigger(`focus`).trigger(`select`)}function L(){P.current=!0,w?N(!1):c(`No link to add.`)}let R=a(null);return u(m,{className:`add-link-dialog`,size:`lg`,maxWidth:1e3,title:l(`add_link.add_link`),helpPageId:`QEAPj01N5f7w`,footer:u(h,{text:l(`add_link.button_add_link`),keyboardShortcut:`Enter`}),onSubmit:L,onShown:I,onHidden:()=>{if(P.current&&w&&t)if(P.current=!1,w.notePath){let e=O?`${w.notePath}?bookmark=${encodeURIComponent(O)}`:w.notePath;t.addLink(e,S===`reference-link`?null:b)}else w.externalLink&&t.addLink(w.externalLink,b,!0);T(null),D([]),k(``),j(``),N(!1)},show:M,children:[u(_,{label:l(`add_link.note`),name:`note`,children:u(g,{inputRef:R,onChange:T,opts:{allowExternalLinks:!0,allowCreatingNotes:!0}})}),E.length>0&&u(_,{label:l(`add_link.anchor`),name:`anchor`,children:u(`select`,{className:`form-select`,value:O,onChange:e=>k(e.target.value),children:[u(`option`,{value:``,children:l(`add_link.anchor_none`)}),E.map(e=>u(`option`,{value:e,children:e},e))]})}),!t?.hasSelection&&u(`div`,{className:`add-link-title-settings`,children:[S!==`external-link`&&u(e,{children:u(v,{name:`link-type`,currentValue:S,values:[{value:`reference-link`,label:l(`add_link.link_title_mirrors`)},{value:`hyper-link`,label:l(`add_link.link_title_arbitrary`)}],onChange:e=>C(e)})}),S!==`reference-link`&&u(`div`,{className:`add-link-title-form-group form-group`,children:[u(`br`,{}),u(`label`,{children:[l(`add_link.link_title`),u(`input`,{className:`link-title form-control`,style:{width:`100%`},value:b,onInput:e=>x(e.target?.value??``)})]})]})]})]})}export{b as default};