import{s as Te,a as ce,g as de,c as Se}from"./mem0_proxy-D5oyS3vJ.js";import{M as Me}from"./api-D2MZ7O5A.js";import{S as ye}from"./messages-CjQo1qp0.js";import{S as l}from"./storage-pXeRtiJs.js";import{c as Le,a as oe,C as T,S as y}from"./util_positioning-Cx352_Ay.js";Te();async function _e(){try{const e=await Se();chrome.runtime.sendMessage({action:ye.OPEN_DASHBOARD,url:e})}catch(e){console.warn("Unable to open Memory Platform dashboard",e)}}let te=!1,z=!1,U=[];const j=new Set;let _=null;const O={x:null,y:null},pe=Le({fetch:async function(e,n){const t=await new Promise(E=>{chrome.storage.sync.get([l.API_KEY,l.USER_ID_CAMEL,l.ACCESS_TOKEN,l.SELECTED_ORG,l.SELECTED_PROJECT,l.USER_ID,l.SIMILARITY_THRESHOLD,l.TOP_K],function(M){E(M)})}),r=t[l.API_KEY],i=t[l.ACCESS_TOKEN];if(!r&&!i)return[];const p=i?`Bearer ${i}`:`Token ${r}`,x=t[l.USER_ID_CAMEL]||t[l.USER_ID]||"chrome-extension-user",u=t[l.SIMILARITY_THRESHOLD]!==void 0?t[l.SIMILARITY_THRESHOLD]:.1,d=t[l.TOP_K]!==void 0?t[l.TOP_K]:10,a={};t[l.SELECTED_ORG]&&(a.org_id=t[l.SELECTED_ORG]),t[l.SELECTED_PROJECT]&&(a.project_id=t[l.SELECTED_PROJECT]);const h={query:(function(){try{const E=oe.memory_header_plain_regex;return String(e).replace(E,"").trim()}catch{return e}})(),filters:{user_id:x},rerank:!0,threshold:u,top_k:d,filter_memories:!1,source:"COGNIZ_CHROME_EXTENSION",...a},S=await fetch("https://api.mem0.ai/v2/memories/search/",{method:"POST",headers:{"Content-Type":"application/json",Authorization:p},body:JSON.stringify(h),signal:n&&n.signal});if(!S.ok)throw new Error(`API request failed with status ${S.status}`);return await S.json()},onSuccess:function(e,n){if(!z)return;const t=(n||[]).map(r=>({id:String(r.id),text:r.memory,categories:r.categories||[]}));ie(t,!1)},onError:function(){z&&ie([],!1)},minLength:3,debounceMs:75,cacheTTL:6e4});function D(){const e=["textarea.w-full.px-2.\\@\\[480px\\]\\/input\\:px-3.bg-transparent.focus\\:outline-none.text-primary.align-bottom.min-h-14.pt-5.my-0.mb-5","textarea.w-full.px-2.\\@\\[480px\\]\\/input\\:px-3.pt-5.mb-5.bg-transparent.focus\\:outline-none.text-primary.align-bottom",'textarea[dir="auto"][spellcheck="false"][placeholder="Ask anything"]','textarea[dir="auto"][spellcheck="false"][placeholder="Ask follow-up"]','textarea[dir="auto"][spellcheck="false"]','textarea[aria-label="Ask Grok anything"]'];for(const n of e){const t=document.querySelector(n);if(t)return t}return null}let ne=null;function Ee(){const e=D();e&&(e.dataset.grokBackgroundHooked||(e.dataset.grokBackgroundHooked="true",ne||(ne=function(){let n=e.value||"";pe.setText(n)}),e.addEventListener("input",ne),e.addEventListener("keyup",ne)))}function be(){if(!D()){setTimeout(be,500);return}Ee()}function Ae(e,n){e&&(e.value=n,e.dispatchEvent(new Event("input",{bubbles:!0})))}function Oe(){be(),Ee();try{!document.getElementById("mem0-icon-button")&&T&&T.resolveCachedAnchor({learnKey:location.host+":"+location.pathname},null,1440*60*1e3).then(function(e){if(!e||!e.el)return;let n=T.createShadowRootHost("mem0-root"),t=n.host,r=n.shadow;t.id="mem0-icon-button";const i=typeof y<"u"&&y.grok?y.grok:null;let p=e.placement||i&&i.placement||{strategy:"inline",where:"beforeend",inlineAlign:"end"};T.applyPlacement({container:t,anchor:e.el,placement:p});let x=document.createElement("style");x.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 u=document.createElement("button");u.className="mem0-btn";let d=document.createElement("img");d.src=chrome.runtime.getURL("icons/icon48.png");let a=document.createElement("div");a.className="dot",u.appendChild(d),r.append(x,u,a),u.addEventListener("click",function(){W()}),typeof R=="function"&&setTimeout(R,0);try{let f=e.el;const h=typeof y<"u"&&y.grok?y.grok:null;let S=Array.from(f.querySelectorAll('button,[role="button"]')).find(function(E){let M=(E.textContent||"").trim();return h&&h.autoButtonTextPattern?h.autoButtonTextPattern.test(M):/\bAuto\b/i.test(M)});if(S){let E=S;for(;E&&E.parentElement!==f;)E=E.parentElement;if(E&&E.parentElement===f&&f){f.insertBefore(t,E.nextSibling);let M=getComputedStyle(f).gap;(!M||M==="normal")&&(M="4px"),t.style.marginLeft=M,t.style.marginRight="0",t.style.display="inline-flex",t.style.alignItems="center",t.style.flexShrink="0"}}}catch{}})}catch{}T&&T.mountOnEditorFocus&&T.mountOnEditorFocus({existingHostSelector:"#mem0-icon-button",editorSelector:typeof y<"u"&&y.grok&&y.grok.editorSelector?y.grok.editorSelector:'textarea, [contenteditable="true"], input[type="text"]',deriveAnchor:typeof y<"u"&&y.grok&&typeof y.grok.deriveAnchor=="function"?y.grok.deriveAnchor:function(e){return e.closest("form")||e.parentElement||document.body},placement:typeof y<"u"&&y.grok&&y.grok.placement?y.grok.placement:{strategy:"inline",where:"beforeend",inlineAlign:"end"},render:function(e,n,t){n.id="mem0-icon-button";let r=document.createElement("style");r.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 i=document.createElement("button");i.className="mem0-btn";let p=document.createElement("img");p.src=chrome.runtime.getURL("icons/icon48.png");let x=document.createElement("div");x.className="dot",i.appendChild(p),e.append(r,i,x),i.addEventListener("click",function(){W()}),typeof R=="function"&&setTimeout(R,0);try{let u=t&&Array.from(t.querySelectorAll('button,[role="button"]')).find(function(d){return/\bAuto\b/i.test((d.textContent||"").trim())});if(u){let d=u;for(;d&&d.parentElement!==t;)d=d.parentElement;d&&d.parentElement===t&&t&&(t.insertBefore(n,d.nextSibling),n.style.marginLeft="0",n.style.marginLeft="0",n.style.marginRight="0",n.style.display="inline-flex",n.style.alignItems="center",n.style.flexShrink="0")}}catch{}}}),document.addEventListener("keydown",function(e){e.ctrlKey&&e.key==="m"&&(e.preventDefault(),(async()=>await W())())})}function R(){const e=D(),n=document.getElementById("mem0-icon-button");if(e&&n){const t=()=>{const i=e.value||"";n.setAttribute("data-has-text",i.trim()?"1":"0")};new MutationObserver(t).observe(e,{characterData:!0,subtree:!0}),e.dataset.grokCheckTextHooked||(e.dataset.grokCheckTextHooked="true",e.addEventListener("input",t),e.addEventListener("keyup",t),e.addEventListener("focus",t)),t(),setTimeout(t,500)}else setTimeout(R,1e3)}function ie(e,n=!1){z&&_&&document.body.removeChild(_),z=!0;let t=0;const r=447;let i=400,p=3,x,u;if(O.x!==null&&O.y!==null)u=O.x,x=O.y;else{const o=document.getElementById("mem0-icon-button")||document.querySelector('button[aria-label="Cogniz Memory"]');if(o){const s=o.getBoundingClientRect(),m=window.innerHeight,b=m-s.bottom;u=Math.max(s.left+s.width/2-r/2,10),u+r>window.innerWidth-10&&(u=window.innerWidth-r-10),b>=i?x=s.bottom+10:(x=s.top-i-10,s.top<m/2&&(i=300,p=2))}else x=100,u=window.innerWidth/2-r/2;O.x=u,O.y=x}const d=document.createElement("div");d.style.cssText=`
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: transparent;
    display: flex;
    z-index: 10000;
    pointer-events: auto;
  `,_=d,d.addEventListener("click",o=>{o.target===d&&le()});try{!document.getElementById("mem0-icon-button")&&T&&T.resolveCachedAnchor({learnKey:location.host+":"+location.pathname},null,1440*60*1e3).then(function(o){if(!o||!o.el)return;let s=T.createShadowRootHost("mem0-root"),m=s.host,b=s.shadow;m.id="mem0-icon-button";let w=o.placement||{strategy:"inline",where:"beforeend",inlineAlign:"end"};T.applyPlacement({container:m,anchor:o.el,placement:w});let g=document.createElement("style");g.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 c=document.createElement("button");c.className="mem0-btn";let C=document.createElement("img");C.src=chrome.runtime.getURL("icons/icon48.png");let P=document.createElement("div");P.className="dot",c.appendChild(C),b.append(g,c,P),c.addEventListener("click",function(){W()}),typeof R=="function"&&setTimeout(R,0)})}catch{}T&&T.mountOnEditorFocus&&T.mountOnEditorFocus({existingHostSelector:"#mem0-icon-button",editorSelector:'textarea, [contenteditable="true"], input[type="text"]',deriveAnchor:function(o){return o.closest("form")||o.parentElement},placement:{strategy:"inline",where:"beforeend",inlineAlign:"end"},render:function(o,s){s.id="mem0-icon-button";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 b=document.createElement("button");b.className="mem0-btn";let w=document.createElement("img");w.src=chrome.runtime.getURL("icons/icon48.png");let g=document.createElement("div");g.className="dot",b.appendChild(w),o.append(m,b,g),b.addEventListener("click",function(){W()}),typeof R=="function"&&setTimeout(R,0)}});const a=document.createElement("div");a.style.cssText=`
    background-color: #1C1C1E;
    border-radius: 12px;
    width: ${r}px;
    height: ${i}px;
    display: flex;
    flex-direction: column;
    color: white;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    position: absolute;
    top: ${x}px;
    left: ${u}px;
    pointer-events: auto;
    border: 1px solid #27272A;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    overflow: hidden;
  `;let f=!1;const h={x:0,y:0};function S(o){const s=o.target;if(s&&(s.tagName==="BUTTON"||s.closest("button")))return;f=!0;const m=a.getBoundingClientRect();h.x=o.clientX-m.left,h.y=o.clientY-m.top,document.addEventListener("mousemove",E),document.addEventListener("mouseup",M),a.style.transition="none"}function E(o){if(!f)return;o.preventDefault();const s=o.clientX-h.x,m=o.clientY-h.y,b=window.innerWidth-r,w=window.innerHeight-i,g=Math.max(0,Math.min(s,b)),c=Math.max(0,Math.min(m,w));a.style.left=g+"px",a.style.top=c+"px",O.x=g,O.y=c}function M(){f=!1,document.removeEventListener("mousemove",E),document.removeEventListener("mouseup",M),a.style.transition=""}const K=document.createElement("div");K.style.cssText=`
    display: flex;
    align-items: center;
    padding: 10px 16px;
    justify-content: space-between;
    background-color: #232325;
    flex-shrink: 0;
    cursor: move;
    user-select: none;
  `;const q=document.createElement("div");q.style.cssText=`
    display: flex;
    flex-direction: row;
    align-items: center;
  `;const re=document.createElement("img");re.src=chrome.runtime.getURL("icons/icon48.png"),re.style.cssText=`
    width: 26px;
    height: 26px;
    border-radius: 50%;
  `;const se=document.createElement("div");se.textContent="Cogniz Memory",se.style.cssText=`
    font-size: 16px;
    font-weight: 600;
    color: white;
    margin-left: 8px;
  `;const J=document.createElement("div");J.style.cssText=`
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
  `;const F=document.createElement("button");F.style.cssText=`
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 5px 16px;
    gap: 8px;
    background-color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
    color: black;
  `,F.textContent="Add to Prompt";const ue=document.createElement("span");ue.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>
`,F.appendChild(ue);const I=document.createElement("button");I.style.cssText=`
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    opacity: 0.6;
    transition: opacity 0.2s;
  `,I.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>`,I.addEventListener("click",()=>{_&&document.body.contains(_)&&(document.body.removeChild(_),z=!1,_=null),chrome.runtime.sendMessage({action:ye.SIDEBAR_SETTINGS})}),I.addEventListener("mouseenter",()=>{I.style.opacity="1"}),I.addEventListener("mouseleave",()=>{I.style.opacity="0.6"}),K.addEventListener("mousedown",S);const Z=document.createElement("div"),me=i-130;Z.style.cssText=`
    display: flex;
    flex-direction: column;
    padding: 0 16px;
    gap: 12px;
    overflow: hidden;
    flex: 1;
    height: ${me}px;
  `;const H=document.createElement("div");H.style.cssText=`
    font-size: 16px;
    font-weight: 600;
    color: #FFFFFF;
    margin-top: 16px;
    flex-shrink: 0;
  `,n?H.textContent="Loading Relevant Memories...":H.textContent=`${e.length} Relevant Memories`;const ve=me-40,L=document.createElement("div");L.style.cssText=`
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow-y: auto;
    flex: 1;
    max-height: ${ve}px;
    padding-right: 8px;
    margin-right: -8px;
    scrollbar-width: none;
    -ms-overflow-style: none;
  `,L.style.cssText+="::-webkit-scrollbar { display: none; }";let G=null;function Ce(){L.innerHTML="";for(let o=0;o<p;o++){const s=document.createElement("div");s.style.cssText=`
        display: flex;
        flex-direction: row;
        align-items: flex-start;
        justify-content: space-between;
        padding: 12px;
        background-color: #27272A;
        border-radius: 8px;
        height: 72px;
        flex-shrink: 0;
        animation: pulse 1.5s infinite ease-in-out;
      `;const m=document.createElement("div");m.style.cssText=`
        background-color: #383838;
        border-radius: 4px;
        height: 14px;
        width: 85%;
        margin-bottom: 8px;
      `;const b=document.createElement("div");b.style.cssText=`
        background-color: #383838;
        border-radius: 4px;
        height: 14px;
        width: 65%;
      `;const w=document.createElement("div");w.style.cssText=`
        display: flex;
        gap: 4px;
        margin-left: 10px;
      `;const g=document.createElement("div");g.style.cssText=`
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background-color: #383838;
      `;const c=document.createElement("div");c.style.cssText=`
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background-color: #383838;
      `,w.appendChild(g),w.appendChild(c);const C=document.createElement("div");C.style.cssText=`
        display: flex;
        flex-direction: column;
        flex-grow: 1;
      `,C.appendChild(m),C.appendChild(b),s.appendChild(C),s.appendChild(w),L.appendChild(s)}if(!document.getElementById("skeleton-animation")){const o=document.createElement("style");o.id="skeleton-animation",o.innerHTML=`
        @keyframes pulse {
          0% { opacity: 0.6; }
          50% { opacity: 0.8; }
          100% { opacity: 0.6; }
        }
      `,document.head.appendChild(o)}}function B(){if(L.innerHTML="",n){Ce();return}if(e.length===0){he(),ge(0,0);return}const o=Math.min(p,e.length),s=Math.ceil(e.length/o),m=Math.floor(t/o)+1;ge(m,s);for(let b=0;b<o;b++){const w=t+b;if(w>=e.length)break;const g=e[w];if(j.has(String(g.id)))continue;g.id||(g.id=`memory-${Date.now()}-${w}`);const c=document.createElement("div");c.style.cssText=`
        display: flex;
        flex-direction: row;
        align-items: flex-start;
        justify-content: space-between;
        padding: 12px; 
        background-color: #27272A;
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.2s ease;
        min-height: 72px; 
        max-height: 72px; 
        overflow: hidden;
        flex-shrink: 0;
      `;const C=document.createElement("div");C.style.cssText=`
        font-size: 14px;
        line-height: 1.5;
        color: #D4D4D8;
        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 */
      `,C.textContent=g.text||"";const P=document.createElement("div");P.style.cssText=`
        display: flex;
        gap: 4px;
        margin-left: 10px;
        flex-shrink: 0;
      `;const Q=document.createElement("button");Q.style.cssText=`
        border: none;
        cursor: pointer;
        padding: 4px;
        background:rgb(66, 66, 69);
        color:rgb(199, 199, 201);
        border-radius: 100%;
        transition: all 0.2s ease;
      `,Q.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>`,Q.addEventListener("click",$=>{$.stopPropagation(),ce("memory_injection",{provider:"grok",source:"COGNIZ_CHROME_EXTENSION",browser:de(),injected_all:!1,memory_id:g.id}),j.add(String(g.id)),U.push(String(g.text||"")),fe();const X=e.findIndex(ae=>ae.id===g.id);X!==-1&&(e.splice(X,1),t>0&&t>=e.length&&(t=Math.max(0,t-p)),H.textContent=`${e.length} Relevant Memories`,B())});const ee=document.createElement("button");ee.style.cssText=`
        background: none;
        border: none;
        cursor: pointer;
        padding: 4px;
        color: #A1A1AA;
      `,ee.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 Y=!1;const N=document.createElement("button");N.style.cssText=`
        display: none;
        align-items: center;
        gap: 6px;
        background:rgb(66, 66, 69);
        color:rgb(199, 199, 201);
        border-radius: 8px;
        padding: 2px 4px;
        border: none;
        cursor: pointer;
        font-size: 13px;
        margin-top: 12px;
        width: fit-content;
      `,N.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
      `;const A=document.createElement("div");A.style.cssText=`
        display: flex;
        flex-direction: column;
        flex-grow: 1;
      `,A.appendChild(C),A.appendChild(N);const ke=()=>{G&&G!==c&&G.dispatchEvent(new Event("collapse")),Y=!0,C.style.webkitLineClamp="unset",C.style.height="auto",A.style.overflowY="auto",A.style.maxHeight="240px",A.style.scrollbarWidth="none",A.style.msOverflowStyle="none",A.style.cssText+="::-webkit-scrollbar { display: none; }",c.style.backgroundColor="#1C1C1E",c.style.maxHeight="300px",c.style.overflow="hidden",N.style.display="flex",G=c,L.scrollTop=c.offsetTop-L.offsetTop},xe=()=>{Y=!1,C.style.webkitLineClamp="2",C.style.height="42px",A.style.overflowY="visible",c.style.backgroundColor="#27272A",c.style.maxHeight="72px",c.style.overflow="hidden",N.style.display="none",G=null};c.addEventListener("collapse",xe),ee.addEventListener("click",$=>{$.stopPropagation(),Y?xe():ke()}),N.addEventListener("click",$=>{$.stopPropagation();const X=e.findIndex(ae=>ae.id===g.id);X!==-1&&(e.splice(X,1),t>0&&t>=e.length&&(t=Math.max(0,t-p)),H.textContent=`${e.length} Relevant Memories`,B())}),P.appendChild(Q),P.appendChild(ee),c.appendChild(A),c.appendChild(P),L.appendChild(c),c.addEventListener("mouseenter",()=>{c.style.backgroundColor=Y?"#18181B":"#323232"}),c.addEventListener("mouseleave",()=>{c.style.backgroundColor=Y?"#1C1C1E":"#27272A"})}L.children.length===0&&e.length>0&&(t>0?(t=Math.max(0,t-p),B()):he())}function he(){L.innerHTML="";const o=document.createElement("div");o.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 s=document.createElement("div");s.innerHTML=`<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#71717A" 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>`,s.style.marginBottom="16px";const m=document.createElement("div");m.textContent="No relevant memories found",m.style.cssText=`
      color: #71717A;
      font-size: 14px;
      font-weight: 500;
    `,o.appendChild(s),o.appendChild(m),L.appendChild(o)}Z.appendChild(H),Z.appendChild(L);const V=document.createElement("div");V.style.cssText=`
    display: flex;
    justify-content: center;
    gap: 12px;
    padding: 10px;
    border-top: none;
    flex-shrink: 0;
  `;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="M15 19l-7-7 7-7" stroke="#A1A1AA" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  </svg>`,v.style.cssText=`
    background: #27272A;
    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 k=document.createElement("button");k.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="#A1A1AA" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  </svg>`,k.style.cssText=v.style.cssText,v.addEventListener("click",()=>{t>=p&&(t=Math.max(0,t-p),B())}),k.addEventListener("click",()=>{t+p<e.length&&(t=t+p,B())}),[v,k].forEach(o=>{o.addEventListener("mouseenter",()=>{o.disabled||(o.style.backgroundColor="#323232")}),o.addEventListener("mouseleave",()=>{o.disabled||(o.style.backgroundColor="#27272A")})}),V.appendChild(v),V.appendChild(k),q.appendChild(re),q.appendChild(se),J.appendChild(F),J.appendChild(I),K.appendChild(q),K.appendChild(J),a.appendChild(K),a.appendChild(Z),a.appendChild(V),d.appendChild(a),document.body.appendChild(d),B();function ge(o,s){if(e.length===0||s===0){v.disabled=!0,v.style.opacity="0.5",v.style.cursor="not-allowed",k.disabled=!0,k.style.opacity="0.5",k.style.cursor="not-allowed";return}o<=1?(v.disabled=!0,v.style.opacity="0.5",v.style.cursor="not-allowed"):(v.disabled=!1,v.style.opacity="1",v.style.cursor="pointer"),o>=s?(k.disabled=!0,k.style.opacity="0.5",k.style.cursor="not-allowed"):(k.disabled=!1,k.style.opacity="1",k.style.cursor="pointer")}F.addEventListener("click",()=>{const o=e.filter(s=>!j.has(String(s.id))&&!s.removed).map(s=>(j.add(String(s.id)),String(s.text||"")));ce("memory_injection",{provider:"grok",source:"COGNIZ_CHROME_EXTENSION",browser:de(),injected_all:!0,memory_count:o.length}),U.push(...o),U.length>0?(fe(),le()):j.size>0&&le();for(let s=e.length-1;s>=0;s--)j.has(String(e[s]?.id))&&e.splice(s,1)});function le(){_&&document.body.contains(_)&&document.body.removeChild(_),_=null,z=!1,O.x=null,O.y=null}}function fe(){const e=D();if(e&&U.length>0){const n=we();let t=`

`+oe.memory_header_text+`
`;U.forEach((r,i)=>{t+=`- ${r}`,i<U.length-1&&(t+=`
`)}),Ae(e,n+t)}}function we(){const e=D();if(!e)return"";let n=e.value||"";const t=oe.memory_header_text,r=n.indexOf(t);r!==-1&&(n=n.substring(0,r).trim());try{const i=oe.memory_header_plain_regex;n=n.replace(i,"").trim()}catch{}return n}function Re(){return new Promise(e=>{chrome.storage.sync.get([l.MEMORY_ENABLED],function(n){e(n.memory_enabled!==!1)})})}async function W(){if(!await Re())return;const n=await new Promise(i=>{chrome.storage.sync.get([l.API_KEY,l.USER_ID_CAMEL,l.ACCESS_TOKEN],function(p){i(p)})});if(!n.apiKey&&!n.access_token){Pe();return}const t=D();let r=t?(t.value||"").trim():"";if(!r){const i=document.querySelector('button[aria-label="Cogniz Memory"]');i&&Ie(i,"Please enter some text first");return}if(r=we(),!te){te=!0,ie([],!0);try{const i=await new Promise(h=>{chrome.storage.sync.get([l.API_KEY,l.USER_ID_CAMEL,l.ACCESS_TOKEN,l.SELECTED_ORG,l.SELECTED_PROJECT,l.USER_ID,l.SIMILARITY_THRESHOLD,l.TOP_K],function(S){h(S)})}),p=i[l.API_KEY],x=i[l.USER_ID_CAMEL]||i[l.USER_ID]||"chrome-extension-user",u=i[l.ACCESS_TOKEN];if(!p&&!u){te=!1;return}ce("modal_clicked",{provider:"grok",source:"COGNIZ_CHROME_EXTENSION",browser:de()});const d=u?`Bearer ${u}`:`Token ${p}`,a=[{role:Me.User,content:r}],f={};i[l.SELECTED_ORG]&&(f.org_id=i[l.SELECTED_ORG]),i[l.SELECTED_PROJECT]&&(f.project_id=i[l.SELECTED_PROJECT]);try{const h=D(),S=h&&h.value?h.value.trim():r;pe.runImmediate(S||r)}catch{pe.runImmediate(r)}fetch("https://api.mem0.ai/v1/memories/",{method:"POST",headers:{"Content-Type":"application/json",Authorization:d},body:JSON.stringify({messages:a,user_id:x,infer:!0,metadata:{provider:"Grok"},source:"COGNIZ_CHROME_EXTENSION",...f})}).catch(h=>{console.error("Error adding memory:",h)})}catch(i){console.error("Error:",i),ie([],!1)}finally{te=!1}}}function Ie(e,n){const t=document.querySelector(".mem0-button-popup");t&&t.remove();const r=document.createElement("div");r.className="mem0-button-popup",r.style.cssText=`
    position: absolute;
    top: -40px;
    left: 50%;
    transform: translateX(-50%);
    background-color: #1C1C1E;
    border: 1px solid #27272A;
    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);
  `,r.textContent=n;const i=document.createElement("div");i.style.cssText=`
    position: absolute;
    bottom: -5px;
    left: 50%;
    transform: translateX(-50%) rotate(45deg);
    width: 10px;
    height: 10px;
    background-color: #1C1C1E;
    border-right: 1px solid #27272A;
    border-bottom: 1px solid #27272A;
  `,r.appendChild(i),e.style.position="relative",e.appendChild(r),setTimeout(()=>{r&&r.parentElement&&r.remove()},3e3)}function Pe(){const e=document.querySelector("#mem0-login-popup");e&&e.remove();const n=document.createElement("div");n.id="mem0-login-popup",n.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 t=document.createElement("div");t.style.cssText=`
    background-color: #1C1C1E;
    border-radius: 12px;
    width: 320px;
    padding: 24px;
    color: white;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  `;const r=document.createElement("button");r.style.cssText=`
    position: absolute;
    top: 16px;
    right: 16px;
    background: none;
    border: none;
    color: #A1A1AA;
    font-size: 16px;
    cursor: pointer;
  `,r.innerHTML="&times;",r.addEventListener("click",()=>{document.body.removeChild(n)});const i=document.createElement("div");i.style.cssText=`
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 16px;
  `;const p=document.createElement("img");p.src=chrome.runtime.getURL("icons/icon48.png"),p.style.cssText=`
    width: 24px;
    height: 24px;
    border-radius: 50%;
    margin-right: 12px;
  `;const x=document.createElement("img");x.src=chrome.runtime.getURL("icons/icon128.png"),x.style.cssText=`
    width: 24px;
    height: 24px;
    border-radius: 50%;
    margin-right: 12px;
  `;const u=document.createElement("h2");u.textContent="Sign in to Cogniz",u.style.cssText=`
    margin: 0;
    font-size: 18px;
    font-weight: 600;
  `,i.appendChild(u);const d=document.createElement("p");d.textContent="Please sign in to access your memories and enhance your conversations!",d.style.cssText=`
    margin-bottom: 24px;
    color: #D4D4D8;
    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: white;
    color: black;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s;
  `;const f=document.createElement("span");f.textContent="Sign in with Cogniz",a.appendChild(x),a.appendChild(f),a.addEventListener("mouseenter",()=>{a.style.backgroundColor="#f5f5f5"}),a.addEventListener("mouseleave",()=>{a.style.backgroundColor="white"}),a.addEventListener("click",()=>{_e(),document.body.removeChild(n)}),t.appendChild(i),t.appendChild(d),t.appendChild(a),n.appendChild(t),n.appendChild(r),n.addEventListener("click",h=>{h.target===n&&document.body.removeChild(n)}),document.body.appendChild(n)}Oe();
