import{M as _e}from"./api-D2MZ7O5A.js";import{S as Ee}from"./messages-CjQo1qp0.js";import{S as c}from"./storage-pXeRtiJs.js";import{c as Ne,C as z,S as y,a as ae}from"./util_positioning-Cx352_Ay.js";import{s as He,a as ge,g as xe,c as Be}from"./mem0_proxy-DKi2ici5.js";He();async function De(){try{const t=await Be();chrome.runtime.sendMessage({action:Ee.OPEN_DASHBOARD,url:t})}catch(t){console.warn("Unable to open Memory Platform dashboard",t)}}let ie=!1,q=!1,D=[];const A=new Set;let L=null,ye=!1,T=!1,h=null,F=null,se=0;const je=10;let B=null,le=null,ce=null;const Me=Ne({fetch:async function(t,i){const e=await new Promise(E=>{chrome.storage.sync.get([c.API_KEY,c.USER_ID_CAMEL,c.ACCESS_TOKEN,c.SELECTED_ORG,c.SELECTED_PROJECT,c.USER_ID,c.SIMILARITY_THRESHOLD,c.TOP_K],function(x){E(x)})}),n=e[c.API_KEY],r=e[c.ACCESS_TOKEN];if(!n&&!r)return[];const o=r?`Bearer ${r}`:`Token ${n}`,s=e[c.USER_ID_CAMEL]||e[c.USER_ID]||"chrome-extension-user",a=e[c.SIMILARITY_THRESHOLD]!==void 0?e[c.SIMILARITY_THRESHOLD]:.1,u=e[c.TOP_K]!==void 0?e[c.TOP_K]:10,m={};e[c.SELECTED_ORG]&&(m.org_id=e[c.SELECTED_ORG]),e[c.SELECTED_PROJECT]&&(m.project_id=e[c.SELECTED_PROJECT]);const g={query:t,filters:{user_id:s},rerank:!0,threshold:a,top_k:u,filter_memories:!1,source:"COGNIZ_CHROME_EXTENSION",...m},p=await fetch("https://api.mem0.ai/v2/memories/search/",{method:"POST",headers:{"Content-Type":"application/json",Authorization:o},body:JSON.stringify(g),signal:i&&i.signal});if(!p.ok)throw new Error(`API request failed with status ${p.status}`);return await p.json()},onSuccess:function(t,i){if(!q)return;const e=(i||[]).map(n=>({id:String(n.id),text:n.memory,categories:n.categories||[]}));me(e,!1)},onError:function(){q&&me([],!1)},minLength:3,debounceMs:75,cacheTTL:6e4});let re=null;function ze(){const t=k();t&&(t.dataset.geminiBackgroundHooked||(t.dataset.geminiBackgroundHooked="true",re||(re=function(){const i=(t.textContent||t.innerText||"").trim();Me.setText(i)}),t.addEventListener("input",re),t.addEventListener("keyup",re)))}function k(){const t=['rich-textarea .ql-editor[contenteditable="true"]',"rich-textarea .ql-editor.textarea",'.ql-editor[aria-label="Enter a prompt here"]',".ql-editor.textarea.new-input-ui",".text-input-field_textarea .ql-editor",'div[contenteditable="true"][role="textbox"][aria-label="Enter a prompt here"]'];for(const i of t){const e=document.querySelector(i);if(e)return le!==e&&(le=e,e.dataset.mem0KeyListener!=="true"&&(T=!1)),T||setTimeout(()=>{T||de()},500),e}return null}function be(){const t=['button[aria-label="Send message"]','button[data-testid="send-button"]','button[type="submit"]:not([aria-label*="attachment"])',".send-button",'button[aria-label*="Send"]','button[title*="Send"]'];for(const i of t){const e=document.querySelector(i);if(e)return ce!==e&&(ce=e,e.dataset.mem0Listener!=="true"&&(T=!1)),e}return null}function Ke(){B&&clearInterval(B),B=window.setInterval(()=>{const t=k(),i=be();(t||i)&&!T&&de(),t&&i&&T&&(B&&clearInterval(B),B=window.setInterval(()=>{const e=k(),n=be();(!e||!n)&&T&&(T=!1,le=null,ce=null)},5e3))},1e3)}function Ie(){if(!k()){se<je&&(se++,setTimeout(Ie,500));return}se=0}function Ge(t,i){if(t)if(t.contentEditable==="true"){t.innerHTML="",i.split(`
`).forEach(o=>{const s=document.createElement("p");o.trim()===""?s.innerHTML="<br>":s.textContent=o,t.appendChild(s)}),t.dispatchEvent(new Event("input",{bubbles:!0})),t.focus();const n=document.createRange(),r=window.getSelection();n.selectNodeContents(t),n.collapse(!1),r&&(r.removeAllRanges(),r.addRange(n))}else t.value=i,t.dispatchEvent(new Event("input",{bubbles:!0}))}function ve(){const t=k();if(!t)return"";let i=t.textContent||"";const e=ae.memory_header_text,n=i.indexOf(e);n!==-1&&(i=i.substring(0,n).trim());try{const r=ae.memory_header_plain_regex;i=i.replace(r,"").trim()}catch{}return i}function V(){return new Promise(t=>{chrome.storage.sync.get([c.MEMORY_ENABLED],function(i){t(i.memory_enabled!==!1)})})}let pe=!1,fe="";function de(){if(T)return;function t(){const n=k();if(!n||!(n.textContent||"").trim())return;const o=ve();pe&&fe===o||(pe=!0,fe=o,setTimeout(()=>{pe=!1,fe=""},1e3),chrome.storage.sync.get([c.API_KEY,c.USER_ID_CAMEL,c.ACCESS_TOKEN,c.MEMORY_ENABLED,c.SELECTED_ORG,c.SELECTED_PROJECT,c.USER_ID],function(s){if(s.memory_enabled===!1||!s.apiKey&&!s.access_token)return;const a=s.access_token?`Bearer ${s.access_token}`:`Token ${s.apiKey}`,u=s.userId||s.user_id||"chrome-extension-user",m={};s.selected_org&&(m.org_id=s.selected_org),s.selected_project&&(m.project_id=s.selected_project),fetch("https://api.mem0.ai/v1/memories/",{method:"POST",headers:{"Content-Type":"application/json",Authorization:a},body:JSON.stringify({messages:[{role:_e.User,content:o}],user_id:u,infer:!0,metadata:{provider:"Gemini"},source:"COGNIZ_CHROME_EXTENSION",...m})}).catch(()=>{})}),setTimeout(()=>{D=[],A.clear()},100))}const i=be();i&&!i.dataset.mem0Listener&&(i.dataset.mem0Listener="true",i.addEventListener("click",function(){t()}));const e=k();e&&(e.dataset.mem0KeyListener||(e.dataset.mem0KeyListener="true",e.addEventListener("keydown",function(n){n.key==="Enter"&&n.shiftKey}))),e&&i&&(T=!0)}function he(){if(z&&z.mountOnEditorFocus){try{document.getElementById("mem0-icon-button")||z.resolveCachedAnchor({learnKey:location.host+":"+location.pathname},null,1440*60*1e3).then(function(n){if(!n||!n.el)return;let r=z.createShadowRootHost("mem0-root"),o=r.host,s=r.shadow;o.id="mem0-icon-button";let a=typeof y<"u"&&y.gemini?y.gemini:null,u=n.placement||a&&a.placement||{strategy:"inline",where:"beforeend",inlineAlign:"end"};z.applyPlacement({container:o,anchor:n.el,placement:u});let m=document.createElement("style");m.textContent=`
              :host { position: relative; }
              .mem0-btn { all: initial; cursor: pointer; display:inline-flex; align-items:center; justify-content:center; width:32px; height:32px; border-radius:50%; }
              .mem0-btn img { width:18px; height:18px; border-radius:50%; }
              .dot { position:absolute; top:-2px; right:-2px; width:8px; height:8px; background:#80DDA2; border-radius:50%; border:2px solid #1C1C1E; display:none; }
              :host([data-has-text="1"]) .dot { display:block; }
            `;let g=document.createElement("button");g.className="mem0-btn";let p=document.createElement("img");p.src=chrome.runtime.getURL("icons/icon48.png");let E=document.createElement("div");E.className="dot",g.appendChild(p),s.append(m,g,E),g.addEventListener("click",function(){Z()}),typeof I=="function"&&setTimeout(I,0)})}catch{}z.mountOnEditorFocus({existingHostSelector:"#mem0-icon-button",editorSelector:typeof y<"u"&&y.gemini&&y.gemini.editorSelector?y.gemini.editorSelector:'textarea, [contenteditable="true"], input[type="text"]',deriveAnchor:typeof y<"u"&&y.gemini&&typeof y.gemini.deriveAnchor=="function"?y.gemini.deriveAnchor:function(n){return n.closest("form")||n.parentElement},placement:typeof y<"u"&&y.gemini&&y.gemini.placement?y.gemini.placement:{strategy:"inline",where:"beforeend",inlineAlign:"end"},render:function(n,r){r.id="mem0-icon-button";let o=document.createElement("style");o.textContent=`
          :host { position: relative; }
          .mem0-btn { all: initial; cursor: pointer; display:inline-flex; align-items:center; justify-content:center; width:32px; height:32px; border-radius:50%; }
          .mem0-btn img { width:18px; height:18px; border-radius:50%; }
          .dot { position:absolute; top:-2px; right:-2px; width:8px; height:8px; background:#80DDA2; border-radius:50%; border:2px solid #1C1C1E; display:none; }
          :host([data-has-text="1"]) .dot { display:block; }
        `;let s=document.createElement("button");s.className="mem0-btn";let a=document.createElement("img");a.src=chrome.runtime.getURL("icons/icon48.png");let u=document.createElement("div");u.className="dot",s.appendChild(a),n.append(o,s,u),s.addEventListener("click",function(){Z()}),typeof I=="function"&&setTimeout(I,0)},fallback:function(){let n=typeof y<"u"&&y.gemini?y.gemini:null;return z.mountResilient({anchors:[{find:function(){let r=n&&n.editorSelector||'textarea, [contenteditable="true"], input[type="text"]',o=document.querySelector(r);if(!o)return null;try{return n&&typeof n.deriveAnchor=="function"?n.deriveAnchor(o):o.closest("form")||o.parentElement}catch{return o.closest("form")||o.parentElement}}}],placement:n&&n.placement||{strategy:"inline",where:"beforeend",inlineAlign:"end"},enableFloatingFallback:!0,render:function(r,o){o.id="mem0-icon-button";let s=document.createElement("style");s.textContent=`
              :host { position: relative; }
              .mem0-btn { all: initial; cursor: pointer; display:inline-flex; align-items:center; justify-content:center; width:32px; height:32px; border-radius:50%; }
              .mem0-btn img { width:18px; height:18px; border-radius:50%; }
              .dot { position:absolute; top:-2px; right:-2px; width:8px; height:8px; background:#80DDA2; border-radius:50%; border:2px solid #1C1C1E; display:none; }
              :host([data-has-text="1"]) .dot { display:block; }
            `;let a=document.createElement("button");a.className="mem0-btn";let u=document.createElement("img");u.src=chrome.runtime.getURL("icons/icon48.png");let m=document.createElement("div");m.className="dot",a.appendChild(u),r.append(s,a,m),a.addEventListener("click",function(){Z()}),typeof I=="function"&&setTimeout(I,0)}})}});return}let t=0;const i=10;async function e(){if(t>=i)return;if(t++,!await V()){const x=document.querySelector("#mem0-icon-button");x&&x.parentElement&&x.parentElement.remove(),t=0,setTimeout(e,1e4);return}const r=document.querySelector("toolbox-drawer .toolbox-drawer-container");if(!r){setTimeout(e,1e3);return}if(document.querySelector("#mem0-icon-button")){t=0;return}t=0;const o=document.createElement("toolbox-drawer-item");o.className="mat-mdc-tooltip-trigger toolbox-drawer-item-button ng-tns-c1279795495-8 mat-mdc-tooltip-disabled ng-star-inserted",o.style.position="relative",o.style.backgroundColor="transparent",o.style.border="none";const s=document.createElement("button");s.className="mat-ripple mat-mdc-tooltip-trigger toolbox-drawer-item-button gds-label-l is-mobile ng-star-inserted",s.setAttribute("matripple",""),s.setAttribute("aria-pressed","false"),s.setAttribute("aria-label","Mem0"),s.id="mem0-icon-button",s.style.backgroundColor="transparent",s.style.border="none";const a=document.createElement("div");a.className="toolbox-drawer-button-label label",a.style.cssText=`
      display: flex;
      align-items: center;
      gap: 6px;
      font-family: 'Google Sans', Roboto, sans-serif;
      font-size: 14px;
      font-weight: 500;
      background-color: transparent;
    `;const u=document.createElement("div");if(u.id="mem0-notification-dot",u.style.cssText=`
      position: absolute;
      top: -2px;
      right: -2px;
      width: 8px;
      height: 8px;
      background-color: #34a853;
      border-radius: 50%;
      border: 1px solid #fff;
      display: none;
      z-index: 1001;
      pointer-events: none;
    `,!document.getElementById("notification-dot-animation")){const x=document.createElement("style");x.id="notification-dot-animation",x.innerHTML=`
        @keyframes popIn {
          0% { transform: scale(0); }
          50% { transform: scale(1.2); }
          100% { transform: scale(1); }
        }
        
        #mem0-notification-dot.active {
          display: block !important;
          animation: popIn 0.3s ease-out forwards;
        }
      `,document.head.appendChild(x)}const m=document.createElement("img");m.src=chrome.runtime.getURL("icons/icon48.png"),m.style.cssText=`
      width: 18px;
      height: 18px;
      border-radius: 50%;
    `;const g=document.createElement("span");g.textContent="Mem0",a.appendChild(m),a.appendChild(g),s.appendChild(a);const p=document.createElement("div");p.className="mem0-button-popover",p.style.cssText=`
      position: absolute;
      bottom: 48px;
      left: 50%;
      transform: translateX(-50%);
      background-color: #2d2e30;
      border: 1px solid #5f6368;
      color: white;
      padding: 8px 12px;
      border-radius: 6px;
      font-size: 12px;
      white-space: nowrap;
      z-index: 10001;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
      display: none;
      transition: opacity 0.2s;
      font-family: 'Google Sans', Roboto, sans-serif;
    `,p.textContent="Add memories to your prompt";const E=document.createElement("div");E.style.cssText=`
      position: absolute;
      top: 100%;
      left: 50%;
      transform: translateX(-50%) rotate(45deg);
      width: 8px;
      height: 8px;
      background-color: #2d2e30;
      border-right: 1px solid #5f6368;
      border-bottom: 1px solid #5f6368;
    `,p.appendChild(E),o.addEventListener("mouseenter",()=>{p.style.display="block",setTimeout(()=>p.style.opacity="1",10)}),o.addEventListener("mouseleave",()=>{p.style.opacity="0",setTimeout(()=>p.style.display="none",200)}),s.addEventListener("click",function(){V().then(x=>{x?Z():chrome.runtime.sendMessage({action:Ee.OPEN_OPTIONS})})}),o.appendChild(s),o.appendChild(u),o.appendChild(p),r.appendChild(o),I(),setTimeout(I,500)}e()}function I(){const t=k(),i=document.querySelector("#mem0-notification-dot");if(t&&i){const e=()=>{(t.textContent||"").trim()!==""?(i.classList.add("active"),i.style.display="block"):(i.classList.remove("active"),i.style.display="none")};F&&F.disconnect(),F=new MutationObserver(e),F.observe(t,{characterData:!0,subtree:!0,childList:!0}),t.dataset.geminiNotificationHooked||(t.dataset.geminiNotificationHooked="true",t.addEventListener("input",e),t.addEventListener("keyup",e),t.addEventListener("focus",e)),e(),setTimeout(e,500)}else{let e=0;const n=5;(()=>{e<n&&(e++,setTimeout(I,1e3))})()}}function Le(){const t=k();if(t&&D.length>0){const i=ve();let e=`

`+ae.memory_header_text+`
`;D.forEach((n,r)=>{e+=`- ${n}`,r<D.length-1&&(e+=`
`)}),Ge(t,i+e)}}function qe(t,i){const e=document.querySelector(".mem0-button-popup");e&&e.remove();const n=document.createElement("div");n.className="mem0-button-popup",n.style.cssText=`
    position: absolute;
    top: -40px;
    left: 50%;
    transform: translateX(-50%);
    background-color: #2d2e30;
    border: 1px solid #5f6368;
    color: white;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 12px;
    white-space: nowrap;
    z-index: 10001;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    font-family: 'Google Sans', Roboto, sans-serif;
  `,n.textContent=i;const r=document.createElement("div");r.style.cssText=`
    position: absolute;
    bottom: -5px;
    left: 50%;
    transform: translateX(-50%) rotate(45deg);
    width: 8px;
    height: 8px;
    background-color: #2d2e30;
    border-right: 1px solid #5f6368;
    border-bottom: 1px solid #5f6368;
  `,n.appendChild(r),t.style.position="relative",t.appendChild(n),setTimeout(()=>{n&&n.parentElement&&n.remove()},3e3)}function Fe(){const t=document.querySelector("#mem0-login-popup");t&&t.remove();const i=document.createElement("div");i.id="mem0-login-popup",i.style.cssText=`
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10001;
  `;const e=document.createElement("div");e.style.cssText=`
    background-color: #2d2e30;
    border-radius: 12px;
    width: 320px;
    padding: 24px;
    color: white;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
    font-family: 'Google Sans', Roboto, sans-serif;
  `;const n=document.createElement("button");n.style.cssText=`
    position: absolute;
    top: 16px;
    right: 16px;
    background: none;
    border: none;
    color: #9aa0a6;
    font-size: 16px;
    cursor: pointer;
  `,n.innerHTML="&times;",n.addEventListener("click",()=>{document.body.removeChild(i)});const r=document.createElement("div");r.style.cssText=`
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 16px;
  `;const o=document.createElement("h2");o.textContent="Sign in to Cogniz",o.style.cssText=`
    margin: 0;
    font-size: 18px;
    font-weight: 600;
  `,r.appendChild(o);const s=document.createElement("p");s.textContent="Please sign in to access your memories and enhance your conversations!",s.style.cssText=`
    margin-bottom: 24px;
    color: #e8eaed;
    font-size: 14px;
    line-height: 1.5;
    text-align: center;
  `;const a=document.createElement("button");a.style.cssText=`
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    padding: 10px;
    background-color: #1a73e8;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s;
    font-family: 'Google Sans', Roboto, sans-serif;
    gap: 8px;
  `;const u=document.createElement("img");u.src=chrome.runtime.getURL("icons/icon48.png"),u.style.cssText=`
    width: 20px;
    height: 20px;
    border-radius: 50%;
  `;const m=document.createElement("span");m.textContent="Sign in with Cogniz",a.appendChild(u),a.appendChild(m),a.addEventListener("mouseenter",()=>{a.style.backgroundColor="#1557b0"}),a.addEventListener("mouseleave",()=>{a.style.backgroundColor="#1a73e8"}),a.addEventListener("click",()=>{De(),document.body.removeChild(i)}),e.appendChild(r),e.appendChild(s),e.appendChild(a),i.appendChild(e),i.appendChild(n),i.addEventListener("click",g=>{g.target===i&&document.body.removeChild(i)}),document.body.appendChild(i)}function me(t,i=!1){q&&L&&document.body.removeChild(L),q=!0;let e=0;const n=447;let r=400,o=3,s,a;const u=document.querySelector("#mem0-icon-button")||document.querySelector('button[aria-label="Mem0"]');if(u){const l=u.getBoundingClientRect(),d=window.innerHeight,_=d-l.bottom;a=Math.max(l.left+l.width/2-n/2,10),a+n>window.innerWidth-10&&(a=window.innerWidth-n-10),_>=r?s=l.bottom+10:(s=l.top-r-10,l.top<d/2&&(r=300,o=2))}else s=100,a=window.innerWidth/2-n/2;const m=document.createElement("div");m.style.cssText=`
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: transparent;
    display: flex;
    z-index: 10000;
    pointer-events: auto;
  `,L=m,m.addEventListener("click",l=>{l.target===m&&ue()});const g=document.createElement("div");g.style.cssText=`
    background-color: #2d2e30;
    border-radius: 12px;
    width: ${n}px;
    height: ${r}px;
    display: flex;
    flex-direction: column;
    color: white;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    position: absolute;
    top: ${s}px;
    left: ${a}px;
    pointer-events: auto;
    border: 1px solid #5f6368;
    font-family: 'Google Sans', Roboto, sans-serif;
    overflow: hidden;
  `;const p=document.createElement("div");p.style.cssText=`
    display: flex;
    align-items: center;
    padding: 10px 16px;
    justify-content: space-between;
    background-color: #35373a;
    flex-shrink: 0;
  `;const E=document.createElement("div");E.style.cssText=`
    display: flex;
    flex-direction: row;
    align-items: center;
  `;const x=document.createElement("img");x.src=chrome.runtime.getURL("icons/icon48.png"),x.style.cssText=`
    width: 26px;
    height: 26px;
    border-radius: 50%;
  `;const U=document.createElement("div");U.textContent="Cogniz Memory",U.style.cssText=`
    font-size: 16px;
    font-weight: 600;
    color: white;
    margin-left: 8px;
  `;const K=document.createElement("div");K.style.cssText=`
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
  `;const R=document.createElement("button");R.style.cssText=`
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 5px 16px;
    gap: 8px;
    background-color:rgb(27, 27, 27);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
    color: white;
    transition: background-color 0.2s;
  `,R.textContent="Add to Prompt";const Q=document.createElement("span");Q.innerHTML=`<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
    <path d="M5 12h14M12 5l7 7-7 7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  </svg>
`,Q.style.position="relative",Q.style.top="2px",R.appendChild(Q),R.addEventListener("mouseenter",()=>{R.style.backgroundColor="rgb(36, 36, 36)"}),R.addEventListener("mouseleave",()=>{R.style.backgroundColor="rgb(27, 27, 27)"});const N=document.createElement("button");N.style.cssText=`
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    opacity: 0.6;
    transition: opacity 0.2s;
  `,N.innerHTML=`<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#FFFFFF" xmlns="http://www.w3.org/2000/svg">
    <path d="M12 15a3 3 0 100-6 3 3 0 000 6z" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
    <path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 010 2.83 2 2 0 01-2.83 0l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 01-2 2 2 2 0 01-2-2v-.09A1.65 1.65 0 009 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 01-2.83 0 2 2 0 010-2.83l.06-.06a1.65 1.65 0 00.33-1.82 1.65 1.65 0 00-1.51-1H3a2 2 0 01-2-2 2 2 0 012-2h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 010-2.83 2 2 0 012.83 0l.06.06a1.65 1.65 0 001.82.33H9a1.65 1.65 0 001-1.51V3a2 2 0 012-2 2 2 0 012 2v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 012.83 0 2 2 0 010 2.83l-.06.06a1.65 1.65 0 00-.33 1.82V9a1.65 1.65 0 001.51 1H21a2 2 0 012 2 2 2 0 01-2 2h-.09a1.65 1.65 0 00-1.51 1z" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  </svg>`,N.addEventListener("click",()=>{L&&document.body.contains(L)&&(document.body.removeChild(L),q=!1,L=null),chrome.runtime.sendMessage({action:Ee.SIDEBAR_SETTINGS})}),N.addEventListener("mouseenter",()=>{N.style.opacity="1"}),N.addEventListener("mouseleave",()=>{N.style.opacity="0.6"});const ee=document.createElement("div"),we=r-130;ee.style.cssText=`
    display: flex;
    flex-direction: column;
    padding: 0 16px;
    gap: 12px;
    overflow: hidden;
    flex: 1;
    height: ${we}px;
  `;const $=document.createElement("div");if($.style.cssText=`
    font-size: 16px;
    font-weight: 600;
    color: #FFFFFF;
    margin-top: 16px;
    flex-shrink: 0;
  `,i)$.textContent="Loading Relevant Memories...";else{const l=t.filter(d=>d&&d.id&&!A.has(d.id)).length;$.textContent=`${l} Relevant Memories`}const Re=we-40,S=document.createElement("div");S.style.cssText=`
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow-y: auto;
    flex: 1;
    max-height: ${Re}px;
    padding-right: 8px;
    margin-right: -8px;
    scrollbar-width: thin;
    scrollbar-color: #5f6368 transparent;
  `;let Y=null;function Oe(){S.innerHTML="";for(let l=0;l<o;l++){const d=document.createElement("div");d.style.cssText=`
        display: flex;
        flex-direction: row;
        align-items: flex-start;
        justify-content: space-between;
        padding: 12px;
        background-color: #3c4043;
        border-radius: 8px;
        height: 72px;
        flex-shrink: 0;
        animation: pulse 1.5s infinite ease-in-out;
      `;const _=document.createElement("div");_.style.cssText=`
        background-color: #5f6368;
        border-radius: 4px;
        height: 14px;
        width: 85%;
        margin-bottom: 8px;
      `;const X=document.createElement("div");X.style.cssText=`
        background-color: #5f6368;
        border-radius: 4px;
        height: 14px;
        width: 65%;
      `;const C=document.createElement("div");C.style.cssText=`
        display: flex;
        gap: 4px;
        margin-left: 10px;
      `;const O=document.createElement("div");O.style.cssText=`
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background-color: #5f6368;
      `;const w=document.createElement("div");w.style.cssText=`
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background-color: #5f6368;
      `,C.appendChild(O),C.appendChild(w);const f=document.createElement("div");f.style.cssText=`
        display: flex;
        flex-direction: column;
        flex-grow: 1;
      `,f.appendChild(_),f.appendChild(X),d.appendChild(f),d.appendChild(C),S.appendChild(d)}if(!document.getElementById("skeleton-animation")){const l=document.createElement("style");l.id="skeleton-animation",l.innerHTML=`
        @keyframes pulse {
          0% { opacity: 0.6; }
          50% { opacity: 0.8; }
          100% { opacity: 0.6; }
        }
      `,document.head.appendChild(l)}}function G(){if(S.innerHTML="",i){Oe();return}const l=t.filter(C=>{const O=C&&C.id,w=O&&A.has(String(C.id));return O&&!w});if($.textContent=i?"Loading Relevant Memories...":`${l.length} Relevant Memories`,l.length===0){Ce(),Te(0,0);return}const d=Math.min(o,l.length),_=Math.ceil(l.length/d),X=Math.floor(e/d)+1;e>=l.length&&(e=Math.max(0,l.length-d)),Te(X,_);for(let C=0;C<d;C++){const O=e+C;if(O>=l.length)break;const w=l[O];if(!w)continue;w.id||(w.id=`memory-${Date.now()}-${O}`);const f=document.createElement("div");f.style.cssText=`
        display: flex;
        flex-direction: row;
        align-items: flex-start;
        justify-content: space-between;
        padding: 12px; 
        background-color: #3c4043;
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.2s ease;
        min-height: 56px; 
        max-height: 56px; 
        overflow: hidden;
        flex-shrink: 0;
      `;const j=document.createElement("div");j.style.cssText=`
        font-size: 14px;
        line-height: 1.5;
        color: #e8eaed;
        flex-grow: 1;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        transition: all 0.2s ease;
        height: 42px; /* Height for 2 lines of text */
      `,j.textContent=w.text;const ne=document.createElement("div");ne.style.cssText=`
        display: flex;
        gap: 4px;
        margin-left: 10px;
        flex-shrink: 0;
      `;const H=document.createElement("button");H.style.cssText=`
        border: none;
        cursor: pointer;
        padding: 4px;
        background: #5f6368;
        color: #e8eaed;
        border-radius: 100%;
        width: 28px;
        height: 28px;
        transition: all 0.2s ease;
      `,H.innerHTML=`<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M12 5v14M5 12h14" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
      </svg>`,H.addEventListener("mouseenter",()=>{H.style.backgroundColor="rgb(36, 36, 36)"}),H.addEventListener("mouseleave",()=>{H.style.backgroundColor="#5f6368"}),H.addEventListener("click",W=>{W.stopPropagation(),ge("memory_injection",{provider:"gemini",source:"COGNIZ_CHROME_EXTENSION",browser:xe(),injected_all:!1,memory_id:w.id}),A.add(String(w.id)),D.push(String(w.text||"")),Le(),G()});const oe=document.createElement("button");oe.style.cssText=`
        background: none;
        border: none;
        cursor: pointer;
        padding: 4px;
        color: #9aa0a6;
      `,oe.innerHTML=`<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
        <circle cx="12" cy="12" r="2"/>
        <circle cx="12" cy="5" r="2"/>
        <circle cx="12" cy="19" r="2"/>
      </svg>`;let J=!1;const M=document.createElement("button");M.style.cssText=`
        display: none;
        align-items: center;
        gap: 6px;
        background: #5f6368;
        color: #e8eaed;
        border-radius: 8px;
        padding: 2px 4px;
        border: none;
        cursor: pointer;
        font-size: 13px;
        margin-top: 12px;
        width: fit-content;
        transition: background-color 0.2s;
      `,M.innerHTML=`
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
          <path d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
        Remove
      `,M.addEventListener("mouseenter",()=>{M.style.backgroundColor="#ea4335"}),M.addEventListener("mouseleave",()=>{M.style.backgroundColor="#5f6368"});const P=document.createElement("div");P.style.cssText=`
        display: flex;
        flex-direction: column;
        flex-grow: 1;
      `,P.appendChild(j),P.appendChild(M);const Pe=()=>{Y&&Y!==f&&Y.dispatchEvent(new Event("collapse")),J=!0,j.style.webkitLineClamp="unset",j.style.height="auto",P.style.overflowY="auto",P.style.maxHeight="240px",P.style.scrollbarWidth="thin",P.style.scrollbarColor="#5f6368 transparent",f.style.backgroundColor="#2d2e30",f.style.maxHeight="300px",f.style.overflow="hidden",M.style.display="flex",Y=f,S.scrollTop=f.offsetTop-S.offsetTop},ke=()=>{J=!1,j.style.webkitLineClamp="2",j.style.height="42px",P.style.overflowY="visible",f.style.backgroundColor="#3c4043",f.style.maxHeight="72px",f.style.overflow="hidden",M.style.display="none",Y=null};f.addEventListener("collapse",ke),oe.addEventListener("click",W=>{W.stopPropagation(),J?ke():Pe()}),M.addEventListener("click",W=>{W.stopPropagation();const Se=t.findIndex(Ae=>Ae.id===w.id);Se!==-1&&(t.splice(Se,1),G())}),ne.appendChild(H),ne.appendChild(oe),f.appendChild(P),f.appendChild(ne),S.appendChild(f),f.addEventListener("mouseenter",()=>{f.style.backgroundColor=J?"#25272a":"#484b4f"}),f.addEventListener("mouseleave",()=>{f.style.backgroundColor=J?"#2d2e30":"#3c4043"})}S.children.length===0&&l.length>0&&(e>0?(e=Math.max(0,e-o),G()):Ce())}function Ce(){S.innerHTML="";const l=document.createElement("div");l.style.cssText=`
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 32px 16px;
      text-align: center;
      flex: 1;
      min-height: 200px;
    `;const d=document.createElement("div");d.innerHTML=`<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#9aa0a6" xmlns="http://www.w3.org/2000/svg">
      <path d="M9 3H5a2 2 0 00-2 2v4m6-6h10a2 2 0 012 2v10a2 2 0 01-2 2h-4M3 21h4a2 2 0 002-2v-4m-6 6V9m18 12a9 9 0 11-18 0 9 9 0 0118 0z" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
    </svg>`,d.style.marginBottom="16px";const _=document.createElement("div");_.textContent="No relevant memories found",_.style.cssText=`
      color: #9aa0a6;
      font-size: 14px;
      font-weight: 500;
    `,l.appendChild(d),l.appendChild(_),S.appendChild(l)}ee.appendChild($),ee.appendChild(S);const te=document.createElement("div");te.style.cssText=`
    display: flex;
    justify-content: center;
    gap: 12px;
    padding: 10px;
    border-top: none;
    flex-shrink: 0;
  `;const b=document.createElement("button");b.innerHTML=`<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
    <path d="M15 19l-7-7 7-7" stroke="#9aa0a6" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  </svg>`,b.style.cssText=`
    background: #3c4043;
    border: none;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.2s;
  `;const v=document.createElement("button");v.innerHTML=`<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
    <path d="M9 5l7 7-7 7" stroke="#9aa0a6" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  </svg>`,v.style.cssText=b.style.cssText,b.addEventListener("click",()=>{e>=o&&(e=Math.max(0,e-o),G())}),v.addEventListener("click",()=>{const l=t.filter(d=>!A.has(String(d.id)));e+o<l.length&&(e=e+o,G())}),[b,v].forEach(l=>{l.addEventListener("mouseenter",()=>{l.disabled||(l.style.backgroundColor="#484b4f")}),l.addEventListener("mouseleave",()=>{l.disabled||(l.style.backgroundColor="#3c4043")})}),te.appendChild(b),te.appendChild(v),E.appendChild(x),E.appendChild(U),K.appendChild(R),K.appendChild(N),p.appendChild(E),p.appendChild(K),g.appendChild(p),g.appendChild(ee),g.appendChild(te),m.appendChild(g),document.body.appendChild(m),G();function Te(l,d){if(t.length===0||d===0){b.disabled=!0,b.style.opacity="0.5",b.style.cursor="not-allowed",v.disabled=!0,v.style.opacity="0.5",v.style.cursor="not-allowed";return}l<=1?(b.disabled=!0,b.style.opacity="0.5",b.style.cursor="not-allowed"):(b.disabled=!1,b.style.opacity="1",b.style.cursor="pointer"),l>=d?(v.disabled=!0,v.style.opacity="0.5",v.style.cursor="not-allowed"):(v.disabled=!1,v.style.opacity="1",v.style.cursor="pointer")}R.addEventListener("click",()=>{const l=t.filter(d=>!A.has(String(d.id))).map(d=>(A.add(String(d.id)),String(d.text||"")));ge("memory_injection",{provider:"gemini",source:"COGNIZ_CHROME_EXTENSION",browser:xe(),injected_all:!0,memory_count:l.length}),D.push(...l),D.length>0?(Le(),ue()):A.size>0&&ue()});function ue(){L&&document.body.contains(L)&&document.body.removeChild(L),L=null,q=!1}}async function Z(){if((k()&&k().textContent||"").includes(ae.memory_marker_prefix)||(A.clear(),D=[]),!await V())return;const n=await new Promise(a=>{chrome.storage.sync.get([c.API_KEY,c.USER_ID_CAMEL,c.ACCESS_TOKEN],function(u){a(u)})});if(!n.apiKey&&!n.access_token){Fe();return}const r=k();let o=r?(r.textContent||"").trim():"";if(!o){const a=document.querySelector("#mem0-icon-button")||document.querySelector('button[aria-label="Mem0"]');a&&qe(a,"Please enter some text first");return}o=ve(),ie=!0;const s=setTimeout(()=>{ie=!1},3e4);me([],!0);try{const a=await new Promise(U=>{chrome.storage.sync.get([c.API_KEY,c.USER_ID_CAMEL,c.ACCESS_TOKEN,c.SELECTED_ORG,c.SELECTED_PROJECT,c.USER_ID,c.SIMILARITY_THRESHOLD,c.TOP_K],function(K){U(K)})}),u=a[c.API_KEY],m=a[c.USER_ID_CAMEL]||a[c.USER_ID]||"chrome-extension-user",g=a[c.ACCESS_TOKEN];if(!u&&!g){ie=!1;return}ge("modal_clicked",{provider:"gemini",source:"COGNIZ_CHROME_EXTENSION",browser:xe()});const p=g?`Bearer ${g}`:`Token ${u}`,E=[{role:_e.User,content:o}],x={};a[c.SELECTED_ORG]&&(x.org_id=a[c.SELECTED_ORG]),a[c.SELECTED_PROJECT]&&(x.project_id=a[c.SELECTED_PROJECT]),Me.runImmediate(o),fetch("https://api.mem0.ai/v1/memories/",{method:"POST",headers:{"Content-Type":"application/json",Authorization:p},body:JSON.stringify({messages:E,user_id:m,infer:!0,metadata:{provider:"Gemini"},source:"COGNIZ_CHROME_EXTENSION",...x})}).catch(()=>{})}catch{me([],!1)}finally{clearTimeout(s),ie=!1}}function Ue(){if(!ye)try{Ie();try{ze()}catch{}he(),de(),Ke(),h&&(h.disconnect(),h.memoryStateInterval&&clearInterval(h.memoryStateInterval));let t=!1;h=new MutationObserver(async e=>{t||e.filter(r=>{const o=r.target;return o&&(o.id==="mem0-notification-dot"||o.classList?.contains("mem0-button-popover")||o.classList?.contains("toolbox-drawer-item")||o.querySelector?.('[aria-label="Mem0"]'))?!1:r.type==="childList"?r.addedNodes.length>0||r.removedNodes.length>0:r.type==="attributes"&&(r.attributeName==="class"||r.attributeName==="id")}).length===0||(h&&clearTimeout(h.debounceTimer),h&&(h.debounceTimer=window.setTimeout(async()=>{if(!t){t=!0;try{if(h&&h.disconnect(),await V())document.querySelector("#mem0-icon-button")||he(),T||de(),I();else{const o=document.querySelector("#mem0-icon-button");o&&o.parentElement&&o.parentElement.remove()}setTimeout(()=>{h&&h.observe(document.body,{childList:!0,subtree:!0,attributeFilter:["class","id"]})},100)}catch{setTimeout(()=>{h&&h.observe(document.body,{childList:!0,subtree:!0,attributeFilter:["class","id"]})},100)}finally{t=!1}}},500)))}),document.addEventListener("keydown",function(e){e.ctrlKey&&e.key==="m"&&(e.preventDefault(),(async()=>await Z())())}),h.observe(document.body,{childList:!0,subtree:!0,attributeFilter:["class","id"]});const i=window.setInterval(async()=>{try{if(await V())document.querySelector("#mem0-icon-button")||he();else{const n=document.querySelector("#mem0-icon-button");n&&n.parentElement&&n.parentElement.remove()}}catch{}},15e3);h.memoryStateInterval=i,ye=!0}catch{}}function $e(){h&&(h.disconnect(),h.memoryStateInterval&&clearInterval(h.memoryStateInterval),h.debounceTimer&&clearTimeout(h.debounceTimer)),F&&F.disconnect(),B&&clearInterval(B),ye=!1,T=!1,se=0,le=null,ce=null}window.addEventListener("beforeunload",$e);Ue();
