import g,{useCallback as m,useEffect as b,useMemo as E,useRef as y}from"react";import{safeLoad as w,Kind as v}from"yaml-ast-parser";import{Helmet as N}from"@dr.pogodin/react-helmet";import{Source as P}from"@redocly/openapi-core";import{isBrowser as h}from"../../../utils/env/is-browser";import{usePageSharedData as k}from"../../providers/page-data/hooks";import{usePostMessage as S}from"../../providers/post-message/use-post-message";const f="___selected",A=10,C=h()&&window.location.search.includes("review-comments=true");function L(n){const[s="",i=""]=n.split("#"),[r,e]=i.split("-").map(o=>o.slice(1));return{fileName:s,fromLine:Number(r),toLine:Number(e)||Number(r)}}function O(n,s){Array.from(document.getElementsByClassName(f)).forEach(r=>{r.classList.remove(f)});let i;if(s)i=document.querySelectorAll(`[data-pointer="${s}"]`);else if(n){const{fileName:r,fromLine:e,toLine:o}=L(n);i=Array.from(document.querySelectorAll("[data-source]")).find(a=>{const d=a.attributes.getNamedItem("data-source")?.value||"",{fileName:p,fromLine:c,toLine:t}=L(d);return p===r&&e<=t&&o>=c})}if(i){let r=i,e=r.offsetTop;if(i instanceof NodeList){r=i[0],i.forEach(a=>{a.classList.add(f)});const l=window.pageYOffset||document.documentElement.scrollTop;e=r.getBoundingClientRect().top+l}r.classList.add(f);const o=document.querySelector("nav")?.offsetHeight||0;window.scrollTo({top:e-o-A,behavior:"smooth"})}}function T(n){return decodeURIComponent(n.replace(/~1/g,"/").replace(/~0/g,"~"))}function $(n){return n.substr(2).split("/").map(T)}function M(n,s,i){const r=$(s);if(n===void 0)return;let e=n;for(const o of r)if(e.kind===v.MAP){const l=e.mappings.find(a=>a.key.value===o);if(!l||(e=l,!l?.value))break;e=l.value}else if(e.kind===v.SEQ){const l=e.items[parseInt(o,10)];if(!l)break;e=l}if(i){const o=e.parent;return!o||o.kind===v.SEQ?e:o.kind===v.MAPPING?o.key:e}else return e}function D(n,s,i){let r=1,e=1,o={line:1,col:1};for(let a=0;a<i-1;a++){if(a===s-1&&(o={line:r,col:e+1}),n[a]===`
`){r++,e=1,a===s-1&&(o={line:r,col:e}),n[a+1]==="\r"&&a++;continue}e++}const l=s===i?{...o}:{line:r,col:e+1};return{start:o,end:l}}function I(n){if(n.pointer===void 0)return n;const{source:s,pointer:i,reportOnKey:r}=n,e=s.getAst(w),o=M(e,i,!!r);return{...n,pointer:void 0,...D(s.body,o?.startPosition??1,o?.endPosition??1)}}function Q(){const n=k("openAPIDocsStore"),s=E(()=>!n||!n.source?{file:"",source:null}:{source:new P(n.source.absolutePath,n.source.content),file:n.source.relativePath},[n]),{sendMessage:i}=S(),r=m(c=>{const t=c.attributes["data-pointer"].value;if(!s.source)return{file:""};const u=I({pointer:`#${t}`,source:s.source});return{file:`${s.file}#L${u.start.line}${u.end?`-L${u.end.line}`:""}`,pointer:t}},[s]),e=m(c=>({file:c.attributes["data-source"].value}),[]),o=m(c=>{c.preventDefault();const t=p(c);let u={file:""};t.attributes["data-source"]&&(u=e(t)),t.attributes["data-pointer"]&&(u=r(t)),i({type:"add-review-comment",...u})},[r,e,i]),l=m(c=>{c.target.classList.remove(f),i({type:"review-hover-lines",file:""}),c.target.removeEventListener("mouseleave",l),c.target.removeEventListener("click",o)},[o,i]),a=m(c=>{const t=p(c);if(t&&t!==d.current){t.classList.add(f);let u={file:""};t.attributes["data-source"]&&(u=e(t)),t.attributes["data-pointer"]&&(u=r(t)),i({type:"review-hover-lines",...u}),t.addEventListener("mouseleave",l),t.addEventListener("click",o)}d.current=t},[o,l,e,r,i]),d=y(null);b(()=>{if(C)return document.body.addEventListener("mousemove",a),()=>{document.body.removeEventListener("mousemove",a)}},[a]);function p(c){let t=c.target;for(;t&&!t.attributes["data-source"]&&!t.attributes["data-pointer"];)t=t.parentElement;return t}return g.createElement(N,null,g.createElement("style",null,`
          .${f} {
            outline: 2px solid #1677ff;
            cursor: pointer;
          }
        `))}export{Q as ReviewComments,O as scrollToElement};
