function $e(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}function U(e,t){return Array(t+1).join(e)}function ie(e){return e.replace(/^\n*/,"")}function ae(e){for(var t=e.length;t>0&&e[t-1]===`
`;)t--;return e.substring(0,t)}function oe(e){return ae(ie(e))}var Ue=["ADDRESS","ARTICLE","ASIDE","AUDIO","BLOCKQUOTE","BODY","CANVAS","CENTER","DD","DIR","DIV","DL","DT","FIELDSET","FIGCAPTION","FIGURE","FOOTER","FORM","FRAMESET","H1","H2","H3","H4","H5","H6","HEADER","HGROUP","HR","HTML","ISINDEX","LI","MAIN","MENU","NAV","NOFRAMES","NOSCRIPT","OL","OUTPUT","P","PRE","SECTION","TABLE","TBODY","TD","TFOOT","TH","THEAD","TR","UL"];function _(e){return F(e,Ue)}var se=["AREA","BASE","BR","COL","COMMAND","EMBED","HR","IMG","INPUT","KEYGEN","LINK","META","PARAM","SOURCE","TRACK","WBR"];function le(e){return F(e,se)}function _e(e){return ue(e,se)}var ce=["A","TABLE","THEAD","TBODY","TFOOT","TH","TD","IFRAME","SCRIPT","AUDIO","VIDEO"];function Fe(e){return F(e,ce)}function je(e){return ue(e,ce)}function F(e,t){return t.indexOf(e.nodeName)>=0}function ue(e,t){return e.getElementsByTagName&&t.some(function(n){return e.getElementsByTagName(n).length})}var Ve=[[/\\/g,"\\\\"],[/\*/g,"\\*"],[/^-/g,"\\-"],[/^\+ /g,"\\+ "],[/^(=+)/g,"\\$1"],[/^(#{1,6}) /g,"\\$1 "],[/`/g,"\\`"],[/^~~~/g,"\\~~~"],[/\[/g,"\\["],[/\]/g,"\\]"],[/^>/g,"\\>"],[/_/g,"\\_"],[/^(\d+)\. /g,"$1\\. "]];function fe(e){return Ve.reduce(function(t,n){return t.replace(n[0],n[1])},e)}var u={};u.paragraph={filter:"p",replacement:function(e){return`

`+e+`

`}};u.lineBreak={filter:"br",replacement:function(e,t,n){return n.br+`
`}};u.heading={filter:["h1","h2","h3","h4","h5","h6"],replacement:function(e,t,n){var r=Number(t.nodeName.charAt(1));if(n.headingStyle==="setext"&&r<3){var i=U(r===1?"=":"-",e.length);return`

`+e+`
`+i+`

`}else return`

`+U("#",r)+" "+e+`

`}};u.blockquote={filter:"blockquote",replacement:function(e){return e=oe(e).replace(/^/gm,"> "),`

`+e+`

`}};u.list={filter:["ul","ol"],replacement:function(e,t){var n=t.parentNode;return n.nodeName==="LI"&&n.lastElementChild===t?`
`+e:`

`+e+`

`}};u.listItem={filter:"li",replacement:function(e,t,n){var r=n.bulletListMarker+"   ",i=t.parentNode;if(i.nodeName==="OL"){var a=i.getAttribute("start"),o=Array.prototype.indexOf.call(i.children,t);r=(a?Number(a)+o:o+1)+".  "}var c=/\n$/.test(e);return e=oe(e)+(c?`
`:""),e=e.replace(/\n/gm,`
`+" ".repeat(r.length)),r+e+(t.nextSibling?`
`:"")}};u.indentedCodeBlock={filter:function(e,t){return t.codeBlockStyle==="indented"&&e.nodeName==="PRE"&&e.firstChild&&e.firstChild.nodeName==="CODE"},replacement:function(e,t,n){return`

    `+t.firstChild.textContent.replace(/\n/g,`
    `)+`

`}};u.fencedCodeBlock={filter:function(e,t){return t.codeBlockStyle==="fenced"&&e.nodeName==="PRE"&&e.firstChild&&e.firstChild.nodeName==="CODE"},replacement:function(e,t,n){for(var r=t.firstChild.getAttribute("class")||"",i=(r.match(/language-(\S+)/)||[null,""])[1],a=t.firstChild.textContent,o=n.fence.charAt(0),c=3,l=new RegExp("^"+o+"{3,}","gm"),p;p=l.exec(a);)p[0].length>=c&&(c=p[0].length+1);var N=U(o,c);return`

`+N+i+`
`+a.replace(/\n$/,"")+`
`+N+`

`}};u.horizontalRule={filter:"hr",replacement:function(e,t,n){return`

`+n.hr+`

`}};u.inlineLink={filter:function(e,t){return t.linkStyle==="inlined"&&e.nodeName==="A"&&e.getAttribute("href")},replacement:function(e,t){var n=j(t.getAttribute("href")),r=V(M(t.getAttribute("title"))),i=r?' "'+r+'"':"";return"["+e+"]("+n+i+")"}};u.referenceLink={filter:function(e,t){return t.linkStyle==="referenced"&&e.nodeName==="A"&&e.getAttribute("href")},replacement:function(e,t,n){var r=j(t.getAttribute("href")),i=M(t.getAttribute("title"));i&&(i=' "'+V(i)+'"');var a,o;switch(n.linkReferenceStyle){case"collapsed":a="["+e+"][]",o="["+e+"]: "+r+i;break;case"shortcut":a="["+e+"]",o="["+e+"]: "+r+i;break;default:var c=this.references.length+1;a="["+e+"]["+c+"]",o="["+c+"]: "+r+i}return this.references.push(o),a},references:[],append:function(e){var t="";return this.references.length&&(t=`

`+this.references.join(`
`)+`

`,this.references=[]),t}};u.emphasis={filter:["em","i"],replacement:function(e,t,n){return e.trim()?n.emDelimiter+e+n.emDelimiter:""}};u.strong={filter:["strong","b"],replacement:function(e,t,n){return e.trim()?n.strongDelimiter+e+n.strongDelimiter:""}};u.code={filter:function(e){var t=e.previousSibling||e.nextSibling,n=e.parentNode.nodeName==="PRE"&&!t;return e.nodeName==="CODE"&&!n},replacement:function(e){if(!e)return"";e=e.replace(/\r?\n|\r/g," ");for(var t=/^`|^ .*?[^ ].* $|`$/.test(e)?" ":"",n="`",r=e.match(/`+/gm)||[];r.indexOf(n)!==-1;)n=n+"`";return n+t+e+t+n}};u.image={filter:"img",replacement:function(e,t){var n=fe(M(t.getAttribute("alt"))),r=j(t.getAttribute("src")||""),i=M(t.getAttribute("title")),a=i?' "'+V(i)+'"':"";return r?"!["+n+"]("+r+a+")":""}};function M(e){return e?e.replace(/(\n+\s*)+/g,`
`):""}function j(e){var t=e.replace(/([<>()])/g,"\\$1");return t.indexOf(" ")>=0?"<"+t+">":t}function V(e){return e.replace(/"/g,'\\"')}function de(e){this.options=e,this._keep=[],this._remove=[],this.blankRule={replacement:e.blankReplacement},this.keepReplacement=e.keepReplacement,this.defaultRule={replacement:e.defaultReplacement},this.array=[];for(var t in e.rules)this.array.push(e.rules[t])}de.prototype={add:function(e,t){this.array.unshift(t)},keep:function(e){this._keep.unshift({filter:e,replacement:this.keepReplacement})},remove:function(e){this._remove.unshift({filter:e,replacement:function(){return""}})},forNode:function(e){if(e.isBlank)return this.blankRule;var t;return(t=H(this.array,e,this.options))||(t=H(this._keep,e,this.options))||(t=H(this._remove,e,this.options))?t:this.defaultRule},forEach:function(e){for(var t=0;t<this.array.length;t++)e(this.array[t],t)}};function H(e,t,n){for(var r=0;r<e.length;r++){var i=e[r];if(We(i,t,n))return i}}function We(e,t,n){var r=e.filter;if(typeof r=="string"){if(r===t.nodeName.toLowerCase())return!0}else if(Array.isArray(r)){if(r.indexOf(t.nodeName.toLowerCase())>-1)return!0}else if(typeof r=="function"){if(r.call(e,t,n))return!0}else throw new TypeError("`filter` needs to be a string, array, or function")}function qe(e){var t=e.element,n=e.isBlock,r=e.isVoid,i=e.isPre||function(Ie){return Ie.nodeName==="PRE"};if(!(!t.firstChild||i(t))){for(var a=null,o=!1,c=null,l=ne(c,t,i);l!==t;){if(l.nodeType===3||l.nodeType===4){var p=l.data.replace(/[ \r\n\t]+/g," ");if((!a||/ $/.test(a.data))&&!o&&p[0]===" "&&(p=p.substr(1)),!p){l=I(l);continue}l.data=p,a=l}else if(l.nodeType===1)n(l)||l.nodeName==="BR"?(a&&(a.data=a.data.replace(/ $/,"")),a=null,o=!1):r(l)||i(l)?(a=null,o=!0):a&&(o=!1);else{l=I(l);continue}var N=ne(c,l,i);c=l,l=N}a&&(a.data=a.data.replace(/ $/,""),a.data||I(a))}}function I(e){var t=e.nextSibling||e.parentNode;return e.parentNode.removeChild(e),t}function ne(e,t,n){return e&&e.parentNode===t||n(t)?t.nextSibling||t.parentNode:t.firstChild||t.nextSibling||t.parentNode}var W=typeof window<"u"?window:{};function Ge(){var e=W.DOMParser,t=!1;try{new e().parseFromString("","text/html")&&(t=!0)}catch{}return t}function Xe(){var e=function(){};return ze()?e.prototype.parseFromString=function(t){var n=new window.ActiveXObject("htmlfile");return n.designMode="on",n.open(),n.write(t),n.close(),n}:e.prototype.parseFromString=function(t){var n=document.implementation.createHTMLDocument("");return n.open(),n.write(t),n.close(),n},e}function ze(){var e=!1;try{document.implementation.createHTMLDocument("").open()}catch{W.ActiveXObject&&(e=!0)}return e}var Ke=Ge()?W.DOMParser:Xe();function Qe(e,t){var n;if(typeof e=="string"){var r=Ye().parseFromString('<x-turndown id="turndown-root">'+e+"</x-turndown>","text/html");n=r.getElementById("turndown-root")}else n=e.cloneNode(!0);return qe({element:n,isBlock:_,isVoid:le,isPre:t.preformattedCode?Je:null}),n}var $;function Ye(){return $=$||new Ke,$}function Je(e){return e.nodeName==="PRE"||e.nodeName==="CODE"}function Ze(e,t){return e.isBlock=_(e),e.isCode=e.nodeName==="CODE"||e.parentNode.isCode,e.isBlank=et(e),e.flankingWhitespace=tt(e,t),e}function et(e){return!le(e)&&!Fe(e)&&/^\s*$/i.test(e.textContent)&&!_e(e)&&!je(e)}function tt(e,t){if(e.isBlock||t.preformattedCode&&e.isCode)return{leading:"",trailing:""};var n=nt(e.textContent);return n.leadingAscii&&re("left",e,t)&&(n.leading=n.leadingNonAscii),n.trailingAscii&&re("right",e,t)&&(n.trailing=n.trailingNonAscii),{leading:n.leading,trailing:n.trailing}}function nt(e){var t=e.match(/^(([ \t\r\n]*)(\s*))(?:(?=\S)[\s\S]*\S)?((\s*?)([ \t\r\n]*))$/);return{leading:t[1],leadingAscii:t[2],leadingNonAscii:t[3],trailing:t[4],trailingNonAscii:t[5],trailingAscii:t[6]}}function re(e,t,n){var r,i,a;return e==="left"?(r=t.previousSibling,i=/ $/):(r=t.nextSibling,i=/^ /),r&&(r.nodeType===3?a=i.test(r.nodeValue):n.preformattedCode&&r.nodeName==="CODE"?a=!1:r.nodeType===1&&!_(r)&&(a=i.test(r.textContent))),a}var rt=Array.prototype.reduce;function w(e){if(!(this instanceof w))return new w(e);var t={rules:u,headingStyle:"setext",hr:"* * *",bulletListMarker:"*",codeBlockStyle:"indented",fence:"```",emDelimiter:"_",strongDelimiter:"**",linkStyle:"inlined",linkReferenceStyle:"full",br:"  ",preformattedCode:!1,blankReplacement:function(n,r){return r.isBlock?`

`:""},keepReplacement:function(n,r){return r.isBlock?`

`+r.outerHTML+`

`:r.outerHTML},defaultReplacement:function(n,r){return r.isBlock?`

`+n+`

`:n}};this.options=$e({},t,e),this.rules=new de(this.options)}w.prototype={turndown:function(e){if(!ot(e))throw new TypeError(e+" is not a string, or an element/document/fragment node.");if(e==="")return"";var t=pe.call(this,new Qe(e,this.options));return it.call(this,t)},use:function(e){if(Array.isArray(e))for(var t=0;t<e.length;t++)this.use(e[t]);else if(typeof e=="function")e(this);else throw new TypeError("plugin must be a Function or an Array of Functions");return this},addRule:function(e,t){return this.rules.add(e,t),this},keep:function(e){return this.rules.keep(e),this},remove:function(e){return this.rules.remove(e),this},escape:function(e){return fe(e)}};function pe(e){var t=this;return rt.call(e.childNodes,function(n,r){r=new Ze(r,t.options);var i="";return r.nodeType===3?i=r.isCode?r.nodeValue:t.escape(r.nodeValue):r.nodeType===1&&(i=at.call(t,r)),me(n,i)},"")}function it(e){var t=this;return this.rules.forEach(function(n){typeof n.append=="function"&&(e=me(e,n.append(t.options)))}),e.replace(/^[\t\r\n]+/,"").replace(/[\t\r\n\s]+$/,"")}function at(e){var t=this.rules.forNode(e),n=pe.call(this,e),r=e.flankingWhitespace;return(r.leading||r.trailing)&&(n=n.trim()),r.leading+t.replacement(n,e,this.options)+r.trailing}function me(e,t){var n=ae(e),r=ie(t),i=Math.max(e.length-n.length,t.length-r.length),a=`

`.substring(0,i);return n+a+r}function ot(e){return e!=null&&(typeof e=="string"||e.nodeType&&(e.nodeType===1||e.nodeType===9||e.nodeType===11))}var G="mindos_clipper_config",q={mindosUrl:"http://localhost:3456",authToken:""};async function X(){try{let t=(await chrome.storage.local.get(G))[G];return t?{...q,...t}:{...q}}catch{return{...q}}}async function he(e){let n={...await X(),...e};return await chrome.storage.local.set({[G]:n}),n}function ge(e){return!!e.mindosUrl&&!!e.authToken}async function b(e,t,n){let r=new AbortController,i=setTimeout(()=>r.abort(),8e3);try{let a=`${e.mindosUrl.replace(/\/+$/,"")}${t}`;return await fetch(a,{...n,signal:r.signal,headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.authToken}`,...n?.headers??{}}})}finally{clearTimeout(i)}}async function ve(e){try{let t=await b(e,"/api/health");if(!t.ok)return{ok:!1,error:`Server returned ${t.status}`};let n=await b(e,"/api/file?op=list_spaces");return n.status===401||n.status===403?{ok:!1,error:"Invalid auth token"}:n.ok?{ok:!0}:{ok:!1,error:`Auth check failed (${n.status})`}}catch(t){return t instanceof DOMException&&t.name==="AbortError"?{ok:!1,error:"Connection timed out"}:{ok:!1,error:"Cannot reach MindOS \u2014 is it running?"}}}async function Ce(e){try{let t=await b(e,"/api/file?op=list_dirs");return t.ok?(await t.json()).dirs??[]:[]}catch{return[]}}async function Ee(e,t,n){try{let r=await b(e,"/api/inbox",{method:"POST",body:JSON.stringify({files:[{name:t,content:n,encoding:"text"}],source:"web-clipper"})}),i;try{i=await r.json()}catch{return{error:`Server returned invalid response (${r.status})`}}return r.ok?{ok:!0}:{error:i.error??`Server error (${r.status})`}}catch(r){return r instanceof DOMException&&r.name==="AbortError"?{error:"Request timed out"}:{error:"Cannot reach MindOS \u2014 is it running?"}}}async function ye(e,t,n,r){let i=t?`${t.replace(/\/+$/,"")}/${n}`:n;try{let a=await b(e,"/api/file",{method:"POST",body:JSON.stringify({op:"create_file",path:i,content:r,source:"web-clipper"})}),o;try{o=await a.json()}catch{return{error:`Server returned invalid response (${a.status})`}}return a.ok?{ok:!0}:{error:o.error??`Server error (${a.status})`}}catch(a){return a instanceof DOMException&&a.name==="AbortError"?{error:"Request timed out"}:{error:"Cannot reach MindOS \u2014 is it running?"}}}function st(e){return e.replace(/[\/\\?*:|"<>]/g,"-").replace(/\s+/g," ").replace(/^\.+/,"").trim().slice(0,120)||"Untitled"}function lt(e){let t=["---"],n=/^(true|false|null|yes|no|on|off|~)$/i,r=/^[*&!@`>|%-]/;for(let[i,a]of Object.entries(e)){if(a==null||a==="")continue;let o=a.replace(/[\r\n]+/g," ").trim(),l=o.includes(":")||o.includes("#")||o.includes("'")||o.includes('"')||o.includes("[")||o.includes("{")||n.test(o)||r.test(o)?`"${o.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`:o;t.push(`${i}: ${l}`)}return t.push("---",""),t.join(`
`)}function we(e,t,n){let r=st(e.title)+".md",i=n(e.content),o=`${lt({title:e.title,source:e.url,author:e.byline,site:e.siteName,saved:e.savedAt})}# ${e.title}

${i}
`;return{fileName:r,markdown:o,space:t,wordCount:e.wordCount}}var g="__inbox__";function be(e){return!e||e===g?"Inbox":e.split("/").join(" / ")}function Te(e){return e?e.split("/").filter(Boolean):[]}function ke(e,t){let n=t?`${t}/`:"";return e.filter(r=>{if(!r.startsWith(n))return!1;let i=r.slice(n.length);return i.length>0&&!i.includes("/")}).sort((r,i)=>r.localeCompare(i))}function Se(e,t){let n=t?`${t}/`:"";return e.some(r=>{if(!r.startsWith(n))return!1;let i=r.slice(n.length);return i.length>0&&!i.includes("/")})}var s=e=>document.getElementById(e),Y=s("view-setup"),Me=s("view-clip"),Re=s("view-success"),J=s("view-loading"),Z=s("setup-url"),De=s("setup-token"),T=s("setup-error"),z=s("btn-connect"),O=s("clip-title"),K=s("clip-site"),ct=s("clip-site-text"),xe=s("clip-words"),ut=s("clip-words-text"),Le=s("clip-status"),L=s("dir-trigger"),ft=s("dir-label"),A=s("dir-panel"),R=s("dir-breadcrumb"),Ae=s("dir-list"),Oe=s("dir-confirm"),C=s("clip-error"),k=s("btn-save"),Be=s("btn-settings"),dt=s("success-detail"),pt=s("btn-done"),mt=s("btn-clip-another"),h,d=null,B=[],f=g,v="";function y(e){[Y,Me,Re,J].forEach(t=>t.hidden=!0),e.hidden=!1}function P(e,t){let n=e.querySelector(".btn-text"),r=e.querySelector(".btn-loading");n&&(n.hidden=t),r&&(r.hidden=!t),e.disabled=t}function Q(e){O.disabled=e,L.disabled=e,Oe.disabled=e,Be.disabled=e}var Pe=new w({headingStyle:"atx",codeBlockStyle:"fenced",bulletListMarker:"-",emDelimiter:"*"});Pe.addRule("pre-code",{filter:e=>e.nodeName==="PRE"&&!!e.querySelector("code"),replacement:(e,t)=>{let n=t.querySelector("code"),r=n?.className?.match(/language-(\w+)/)?.[1]||"",i=n?.textContent||"";return`
\`\`\`${r}
${i}
\`\`\`
`}});async function ht(){let[e]=await chrome.tabs.query({active:!0,currentWindow:!0});if(!e?.id)throw new Error("No active tab");let t=e.url??"";if(t.startsWith("chrome")||t.startsWith("edge")||t.startsWith("about:")||t.startsWith("moz-extension"))throw new Error("Cannot clip browser internal pages");try{await chrome.scripting.executeScript({target:{tabId:e.id},files:["content/extractor.js"]})}catch{throw new Error("Cannot read this page \u2014 try refreshing first")}let n;try{n=await chrome.scripting.executeScript({target:{tabId:e.id},func:()=>window.__mindosClipResult})}catch{throw new Error("Cannot read extraction result")}let r=n?.[0]?.result;if(!r||typeof r!="object")throw new Error("Content extraction returned empty result");return r}async function He(){let[e,t]=await Promise.allSettled([ht(),Ce(h)]),n=e.status==="fulfilled"?e.value:null,r=t.status==="fulfilled"?t.value:[],i=t.status==="rejected"?"Could not load spaces. Saving to Inbox is still available.":void 0,a=e.status==="rejected"?e.reason instanceof Error?e.reason.message:"Cannot read this page":void 0;return{content:n,dirs:r,errorMsg:a,dirsErrorMsg:i}}async function gt(){if(h=await X(),!ge(h)){y(Y),Z.value=h.mindosUrl;return}y(J);let e=await He();d=e.content,B=e.dirs,ee(e.errorMsg,e.dirsErrorMsg)}function ee(e,t){y(Me);let n=!!d,r=e??t;if(r&&!n?(m(C,r),k.disabled=!0,x("Read failed","status-chip-error")):(r?m(C,r):te(C),k.disabled=!n,x(n?"Ready":"Read failed",n?"status-chip-success":"status-chip-error")),d){O.value=d.title;try{let i=new URL(d.url).hostname.replace(/^www\./,"");ct.textContent=i,K.style.display=""}catch{K.style.display="none"}ut.textContent=`${d.wordCount.toLocaleString()} words`,xe.style.display=""}else O.value="",K.style.display="none",xe.style.display="none";f=g,v="",S(),E(!1),Q(!1)}function Ne(e,t,n,r){let i=document.createElementNS("http://www.w3.org/2000/svg","svg");if(i.setAttribute("class",e),i.setAttribute("width",t),i.setAttribute("height",t),i.setAttribute("viewBox","0 0 24 24"),i.setAttribute("fill","none"),i.setAttribute("stroke","currentColor"),i.setAttribute("stroke-width","2"),i.setAttribute("stroke-linecap","round"),i.setAttribute("stroke-linejoin","round"),n){let a=document.createElementNS("http://www.w3.org/2000/svg","path");a.setAttribute("d",n),i.appendChild(a)}if(r){let a=document.createElementNS("http://www.w3.org/2000/svg","polyline");a.setAttribute("points",r),i.appendChild(a)}return i}function D(){let e=Te(v);R.replaceChildren();let t=document.createElement("button");t.type="button",t.textContent="Inbox",t.className=f===g&&!v?"active":"",t.addEventListener("click",()=>{v="",f=g,S(),D()}),R.appendChild(t),e.forEach((r,i)=>{let a=document.createElement("span");a.className="crumb-sep",a.textContent="\u203A",R.appendChild(a);let o=document.createElement("button");o.type="button",o.textContent=r;let c=e.slice(0,i+1).join("/");o.className=i===e.length-1?"active":"",o.addEventListener("click",()=>{v=c,f=c,S(),D()}),R.appendChild(o)});let n=ke(B,v);Ae.replaceChildren();for(let r of n){let i=r.split("/").pop()||r,a=Se(B,r),o=document.createElement("button");o.type="button",o.className="dir-item",f===r&&o.classList.add("active"),o.setAttribute("aria-label",a?`Open ${r}`:`Select ${r}`),o.appendChild(Ne("dir-item-icon","12","M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"));let c=document.createElement("span");c.className="dir-item-name",c.textContent=i,o.appendChild(c),a&&o.appendChild(Ne("dir-item-arrow","11",void 0,"9 18 15 12 9 6")),o.addEventListener("click",()=>{if(f=r,S(),a){v=r,D();return}E(!1)}),Ae.appendChild(o)}}function S(){ft.textContent=be(f)}function x(e,t){Le.textContent=e,Le.className=`status-chip ${t}`}function E(e){let t=e??A.hidden;A.hidden=!t,L.classList.toggle("active",t),L.setAttribute("aria-expanded",String(t)),t&&D()}z.addEventListener("click",async()=>{let e=Z.value.trim().replace(/\/+$/,""),t=De.value.trim();if(!e){m(T,"Please enter your MindOS URL");return}if(!t){m(T,"Please paste your auth token");return}te(T),P(z,!0);try{let n={mindosUrl:e,authToken:t},r=await ve(n);if(!r.ok){m(T,r.error||"Connection failed");return}h=await he(n),y(J);let i=await He();d=i.content,B=i.dirs,ee(i.errorMsg,i.dirsErrorMsg)}catch{m(T,"Could not save connection settings")}finally{P(z,!1)}});k.addEventListener("click",async()=>{if(!d){m(C,"No content extracted from this page");return}te(C),x("Saving\u2026","status-chip-loading"),P(k,!0),Q(!0);try{let e={...d,title:O.value.trim()||d.title},t=f===g,n=we(e,t?"":f,a=>Pe.turndown(a)),r=t?await Ee(h,n.fileName,n.markdown):await ye(h,f,n.fileName,n.markdown);if(r.error){m(C,r.error),x("Save failed","status-chip-error");return}let i=t?`Inbox/${n.fileName}`:`${f}/${n.fileName}`;dt.textContent=i,y(Re)}catch{m(C,"Save failed unexpectedly"),x("Save failed","status-chip-error")}finally{P(k,!1),Q(!1)}});Be.addEventListener("click",()=>{Z.value=h.mindosUrl,De.value=h.authToken,E(!1),y(Y)});pt.addEventListener("click",()=>{window.close()});mt.addEventListener("click",()=>{ee()});L.addEventListener("click",()=>E());Oe.addEventListener("click",()=>{f=v||g,S(),E(!1)});document.addEventListener("keydown",e=>{e.key==="Escape"&&!A.hidden&&(e.preventDefault(),E(!1))});document.addEventListener("click",e=>{if(A.hidden)return;let t=e.target;t&&(A.contains(t)||L.contains(t)||E(!1))});function m(e,t){e.textContent=t,e.hidden=!1}function te(e){e.hidden=!0}gt();
