import{i as v,b as p,f as m,a as c,d as f,t as b}from"./vendor-lit-B2r-dKc0.js";import{a as x}from"./vendor-mini-lit-CpUQub-j.js";import{C as w,b as _}from"./vendor-lucide-B9Yl1Gez.js";import"./vendor-marked-_iteVaMm.js";var g=Object.defineProperty,y=Object.getOwnPropertyDescriptor,d=(e,t,r,n)=>{for(var o=n>1?void 0:n?y(t,r):t,i=e.length-1,l;i>=0;i--)(l=e[i])&&(o=(n?l(t,r,o):l(o))||o);return n&&o&&g(t,r,o),o};let s=null;function k(){return s&&s.isConnected||(s=document.createElement("annotation-popover"),document.body.appendChild(s)),s}function z(e){const t=k();t.referenceRect=e.referenceRect,t.selectedText=e.selectedText,t.mode=e.mode,t.existingComment=e.existingComment??"",t.onSubmit=e.onSubmit,t.onCancel=e.onCancel,t.onDelete=e.onDelete??null,t.open=!0}function D(){s&&s.isConnected&&(s.open=!1)}let a=class extends v{constructor(){super(...arguments),this.open=!1,this.referenceRect=null,this.selectedText="",this.mode="popover",this.existingComment="",this.onSubmit=null,this.onCancel=null,this.onDelete=null,this._touchStartY=0,this._vpResizeHandler=null,this._outsideClickHandler=null,this._copied=!1,this._copyText=async e=>{if(e.preventDefault(),e.stopPropagation(),!!this.selectedText){try{await navigator.clipboard.writeText(this.selectedText)}catch{const t=document.createElement("textarea");t.value=this.selectedText,t.style.position="fixed",t.style.opacity="0",document.body.appendChild(t),t.select();try{document.execCommand("copy")}catch{}t.remove()}this._copied=!0,this.requestUpdate(),this._copyResetTimer!=null&&clearTimeout(this._copyResetTimer),this._copyResetTimer=window.setTimeout(()=>{this._copied=!1,this.requestUpdate(),this._copyResetTimer=void 0},1500)}}}updated(e){const t=e.has("open"),r=e.has("referenceRect");t&&this.open?(this.mode==="popover"?(this._reposition(),this._attachOutsideClickHandler()):(this.style.left="",this.style.top="",this._attachViewportListener()),this._textarea&&this.existingComment&&(this._textarea.value=this.existingComment),requestAnimationFrame(()=>{this._textarea?.focus()})):this.open&&r&&this.mode==="popover"&&this._reposition(),t&&!this.open&&(this._detachViewportListener(),this._detachOutsideClickHandler())}disconnectedCallback(){super.disconnectedCallback(),this._detachViewportListener(),this._detachOutsideClickHandler()}_reposition(){if(!this.open||this.mode!=="popover"||!this.referenceRect)return;const e=this.referenceRect,t=8,r=window.innerWidth,n=window.innerHeight;let o=this.offsetWidth,i=this.offsetHeight;o===0&&(o=326),i===0&&(i=200);const h=n-e.bottom-t<i&&e.top>i+t?Math.max(t,e.top-i-4):Math.min(e.bottom+4,n-i-t),u=Math.min(Math.max(t,e.left),r-o-t);this.style.left=`${u}px`,this.style.top=`${h}px`,(o===326||i===200)&&requestAnimationFrame(()=>{this.open&&this.offsetWidth>0&&this._reposition()})}_attachOutsideClickHandler(){this._outsideClickHandler||(this._outsideClickHandler=e=>{e.composedPath().includes(this)||this._cancel()},setTimeout(()=>{this._outsideClickHandler&&document.addEventListener("mousedown",this._outsideClickHandler,!0)},0))}_detachOutsideClickHandler(){this._outsideClickHandler&&(document.removeEventListener("mousedown",this._outsideClickHandler,!0),this._outsideClickHandler=null)}_attachViewportListener(){!window.visualViewport||this._vpResizeHandler||(this._vpResizeHandler=()=>{if(this.mode!=="bottom-sheet"||!this.open)return;const e=window.innerHeight-window.visualViewport.height;this.style.bottom=`${e}px`},window.visualViewport.addEventListener("resize",this._vpResizeHandler),this._vpResizeHandler())}_detachViewportListener(){this._vpResizeHandler&&window.visualViewport&&window.visualViewport.removeEventListener("resize",this._vpResizeHandler),this._vpResizeHandler=null,this.mode==="bottom-sheet"&&(this.style.bottom="0")}_onKeyDown(e){e.key==="Escape"?(e.preventDefault(),e.stopPropagation(),this._cancel()):e.key==="Enter"&&!e.shiftKey&&(e.preventDefault(),this._submit())}_onTouchStart(e){this._touchStartY=e.touches[0].clientY}_onTouchMove(e){e.preventDefault()}_onTouchEnd(e){e.changedTouches[0].clientY-this._touchStartY>50&&this._cancel()}_submit(){const e=this._textarea?.value?.trim();if(!e)return;const t=this.onSubmit;this._reset(),t?.(e)}_cancel(){const e=this.onCancel;this._reset(),e?.()}_delete(){const e=this.onDelete;this._reset(),e?.()}_reset(){this._textarea&&(this._textarea.value=""),this.existingComment="",this.open=!1,this.onSubmit=null,this.onCancel=null,this.onDelete=null,this._copied=!1}render(){if(!this.open)return p``;const e=this.selectedText.length>80?this.selectedText.slice(0,77)+"...":this.selectedText,t=p`<button
      class="review-popover-copy ${this._copied?"copied":""}"
      title="Copy selected text"
      @click=${this._copyText}
    >${x(this._copied?w:_,"xs")}<span>${this._copied?"Copied":"Copy"}</span></button>`,r=p`
      <div class="review-popover-quote-row">
        <div class="review-popover-quote">${e}</div>
        ${t}
      </div>
    `,n=this.existingComment!=="",o=n?"Save":"Add",i=n&&this.onDelete?p`<button
          class="review-popover-delete"
          data-testid="annotation-delete"
          title="Delete comment"
          @click=${this._delete}
        >Delete</button>`:"";return this.mode==="bottom-sheet"?p`
        <div class="review-popover review-popover--sheet" @keydown=${this._onKeyDown}>
          <div class="review-sheet-handle"
            @touchstart=${this._onTouchStart}
            @touchmove=${this._onTouchMove}
            @touchend=${this._onTouchEnd}>
            <div class="review-sheet-handle-pill"></div>
          </div>
          ${r}
          <textarea placeholder="Add your comment..." @keydown=${this._onKeyDown}></textarea>
          <div class="review-popover-actions">
            ${i}
            <button class="review-popover-cancel" @click=${this._cancel}>Cancel</button>
            <button class="review-popover-submit" @click=${this._submit}>${o}</button>
          </div>
        </div>
      `:p`
      <div class="review-popover" @keydown=${this._onKeyDown}>
        ${r}
        <textarea placeholder="Add your comment..." @keydown=${this._onKeyDown}></textarea>
        <div class="review-popover-actions">
          ${i}
          <button class="review-popover-cancel" @click=${this._cancel}>Cancel</button>
          <button class="review-popover-submit" @click=${this._submit}>${o}</button>
        </div>
      </div>
    `}};a.styles=m`
    :host {
      position: fixed;
      z-index: 1000;
      display: none;
      /* Initial top/left will be overridden in updated() based on the
         reference rect, with viewport-clamp math (mirrors the
         renderRolePickerDropdown popover in src/app/sidebar.ts). */
      top: 0;
      left: 0;
    }
    :host([open]) {
      display: block;
    }

    /* Bottom-sheet host docks to bottom of viewport. */
    :host([mode="bottom-sheet"]) {
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
      top: auto;
    }
    :host([mode="bottom-sheet"][open]) {
      display: block;
    }

    .review-popover {
      background: var(--background, #fff);
      color: var(--foreground, #1a1a1a);
      border: 1px solid var(--border, #ddd);
      border-radius: 8px;
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
      padding: 12px;
      width: 300px;
      font-family: inherit;
    }
    .review-popover-quote-row {
      display: flex;
      align-items: stretch;
      gap: 6px;
      margin-bottom: 8px;
    }
    .review-popover-quote {
      flex: 1;
      min-width: 0; /* allow ellipsis inside flex child */
      font-size: 12px;
      color: var(--muted-foreground, #888);
      padding: 4px 8px;
      border-left: 3px solid var(--primary, #6366f1);
      background: var(--muted, rgba(0, 0, 0, 0.04));
      border-radius: 0 4px 4px 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .review-popover-copy {
      flex: none;
      display: inline-flex;
      align-items: center;
      gap: 4px;
      padding: 0 10px;
      font-size: 11px;
      font-weight: 500;
      color: var(--muted-foreground, #888);
      background: var(--background, #fff);
      border: 1px solid var(--border, #ddd);
      border-radius: 4px;
      cursor: pointer;
      font-family: inherit;
      white-space: nowrap;
      transition: color 120ms, border-color 120ms;
    }
    .review-popover-copy svg {
      width: 12px;
      height: 12px;
    }
    .review-popover-copy:hover {
      color: var(--foreground, #1a1a1a);
      border-color: var(--foreground, #1a1a1a);
    }
    .review-popover-copy.copied {
      color: var(--primary, #6366f1);
      border-color: var(--primary, #6366f1);
    }
    textarea {
      width: 100%;
      min-height: 60px;
      padding: 8px;
      border: 1px solid var(--border, #ddd);
      border-radius: 4px;
      background: var(--background, #fff);
      color: var(--foreground, #1a1a1a);
      font-family: inherit;
      font-size: 13px;
      resize: vertical;
      box-sizing: border-box;
    }
    textarea:focus {
      outline: none;
      border-color: var(--primary, #6366f1);
      box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
    }
    .review-popover-actions {
      display: flex;
      justify-content: flex-end;
      gap: 8px;
      margin-top: 8px;
    }
    button {
      padding: 4px 12px;
      border-radius: 4px;
      font-size: 13px;
      cursor: pointer;
      border: 1px solid var(--border, #ddd);
      font-family: inherit;
    }
    .review-popover-cancel {
      background: var(--background, #fff);
      color: var(--foreground, #1a1a1a);
    }
    .review-popover-cancel:hover {
      background: var(--muted, rgba(0, 0, 0, 0.04));
    }
    .review-popover-submit {
      background: var(--primary, #6366f1);
      color: white;
      border-color: var(--primary, #6366f1);
    }
    .review-popover-submit:hover {
      opacity: 0.9;
    }
    .review-popover-delete {
      margin-right: auto;
      background: transparent;
      color: var(--negative, #dc2626);
      border-color: color-mix(in oklch, var(--negative, #dc2626) 40%, transparent);
    }
    .review-popover-delete:hover {
      background: color-mix(in oklch, var(--negative, #dc2626) 10%, transparent);
      color: var(--negative, #dc2626);
      border-color: var(--negative, #dc2626);
    }

    /* Bottom-sheet variant. */
    .review-popover--sheet {
      width: 100%;
      max-height: 50vh;
      border-radius: 12px 12px 0 0;
      box-sizing: border-box;
      animation: slide-up 200ms ease-out;
    }
    @keyframes slide-up {
      from { transform: translateY(100%); }
      to { transform: translateY(0); }
    }
    .review-sheet-handle {
      display: flex;
      justify-content: center;
      padding: 8px 0 4px;
      cursor: grab;
      touch-action: none;
    }
    .review-sheet-handle-pill {
      width: 40px;
      height: 4px;
      border-radius: 2px;
      background: var(--border, #ddd);
    }
    :host([mode="bottom-sheet"]) .review-popover-cancel,
    :host([mode="bottom-sheet"]) .review-popover-submit {
      min-height: 44px;
      font-size: 15px;
    }
    :host([mode="bottom-sheet"]) textarea {
      min-height: 80px;
      font-size: 15px;
    }
  `;d([c({type:Boolean,reflect:!0})],a.prototype,"open",2);d([c({attribute:!1})],a.prototype,"referenceRect",2);d([c({type:String})],a.prototype,"selectedText",2);d([c({type:String,reflect:!0})],a.prototype,"mode",2);d([c({type:String})],a.prototype,"existingComment",2);d([f("textarea")],a.prototype,"_textarea",2);a=d([b("annotation-popover")],a);export{a as AnnotationPopover,D as closeAnnotationPopover,z as openAnnotationPopover};
