var e=/^([\w-_]+)\/([\w-_.]+)$/i;const t=function(){const t=Object.fromEntries(new URL(location.href).searchParams);let n=null,s=null;if("issue-term"in t){if(n=t["issue-term"],void 0!==n){if(""===n)throw new Error("When issue-term is specified, it cannot be blank.");if(-1!==["title","url","pathname","og:title"].indexOf(n)){if(!t[n])throw new Error(`Unable to find "${n}" metadata.`);n=t[n]}}}else{if(!("issue-number"in t))throw new Error('"issue-term" or "issue-number" must be specified.');if(s=+t["issue-number"],s.toString(10)!==t["issue-number"])throw new Error(`issue-number is invalid. "${t["issue-number"]}`)}if(!("repo"in t))throw new Error('"repo" is required.');if(!("origin"in t))throw new Error('"origin" is required.');const r=e.exec(t.repo);if(null===r)throw new Error(`Invalid repo: "${t.repo}"`);return{owner:r[1],repo:r[2],issueTerm:n,issueNumber:s,origin:t.origin,url:t.url,title:t.title,description:t.description,label:t.label,theme:t.theme||"github-light",session:t.session}}(),n={value:null};function s(e){return`https://api.utteranc.es/authorize?${new URLSearchParams({redirect_uri:e})}`}const r=["+1","-1","laugh","hooray","confused","heart","rocket","eyes"];let i,o;function a(e,t){(t=t||{}).mode="cors",t.cache="no-cache";const s=new Request("https://api.github.com/"+e,t);return s.headers.set("Accept","application/vnd.github.v3+json"),null!==n.value&&s.headers.set("Authorization",`token ${n.value}`),s}const l={standard:{limit:Number.MAX_VALUE,remaining:Number.MAX_VALUE,reset:0},search:{limit:Number.MAX_VALUE,remaining:Number.MAX_VALUE,reset:0}};function c(e){return fetch(e).then((t=>(401===t.status&&(n.value=null),403===t.status&&t.json().then((e=>{"Resource not accessible by integration"===e.message&&window.dispatchEvent(new CustomEvent("not-installed"))})),function(e){const t=e.headers.get("X-RateLimit-Limit"),n=e.headers.get("X-RateLimit-Remaining"),s=e.headers.get("X-RateLimit-Reset"),r=/\/search\//.test(e.url),i=r?l.search:l.standard;if(i.limit=+t,i.remaining=+n,i.reset=+s,403===e.status&&0===i.remaining){const e=new Date(0);e.setUTCSeconds(i.reset);const t=Math.round((e.getTime()-(new Date).getTime())/1e3/60),n=r?"search API":"non-search APIs";console.warn(`Rate limit exceeded for ${n}. Resets in ${t} minute${1===t?"":"s"}.`)}}(t),"GET"===e.method&&-1!==[401,403].indexOf(t.status)&&e.headers.has("Authorization")?(e.headers.delete("Authorization"),c(e)):t)))}function h(e,t=!1){const n=a(`repos/${i}/${o}/contents/${e}?ref=master`);return t&&n.headers.set("accept","application/vnd.github.VERSION.html"),c(n).then((n=>{if(404===n.status)throw new Error(`Repo "${i}/${o}" does not have a file named "${e}" in the "master" branch.`);if(!n.ok)throw new Error(`Error fetching ${e}.`);return t?n.text():n.json()})).then((e=>{if(t)return e;const{content:n}=e,s=(r=(r=n).replace(/\s/g,""),decodeURIComponent(escape(atob(r))));var r;return JSON.parse(s)}))}function u(e,t){const n=function(e,t){const n=a(`repos/${i}/${o}/issues/${e}/comments?page=${t}&per_page=25`);return n.headers.set("Accept","application/vnd.github.VERSION.html+json,application/vnd.github.v3+json"),n}(e,t);return c(n).then((e=>{if(!e.ok)throw new Error("Error fetching comments.");return e.json()}))}const d=[1e3,"second",6e4,"minute",36e5,"hour",864e5,"day",6048e5,"week",23328e5,"month"],m={month:"short",day:"numeric",year:"numeric"};let p;let g=-1;function v(){const e=document.body.scrollHeight;if(e===g)return;g=e;const t={type:"resize",height:e};parent.postMessage(t,p)}let f=0;function b(){const e=Date.now();e-f>50&&(f=e,setTimeout(v,50))}const w={"+1":"Thumbs Up","-1":"Thumbs Down",laugh:"Laugh",hooray:"Hooray",confused:"Confused",heart:"Heart",rocket:"Rocket",eyes:"Eyes"},y={"+1":"👍","-1":"👎",laugh:"️😂",hooray:"️🎉",confused:"😕",heart:"❤️",rocket:"🚀",eyes:"👀"};function $(e,t,n,s){return`\n `}function E(e){addEventListener("click",(async t=>{const n=t.target instanceof HTMLElement&&t.target.closest("button");if(!n)return;if(!n.hasAttribute("reaction"))return;if(t.preventDefault(),!e)return;n.disabled=!0;const s=n.closest("details");s&&(s.open=!1);const r=n.formAction,i=n.value,{deleted:o}=await async function(e,t){const n=a(e=e.replace("https://api.github.com/",""),{method:"POST",body:JSON.stringify({content:t})});n.headers.set("Accept","application/vnd.github.v3+json");const s=await c(n),r=s.ok?await s.json():null;if(201===s.status)return{reaction:r,deleted:!1};if(200!==s.status)throw new Error('expected "201 reaction created" or "200 reaction already exists"');const i=a(`${e}/${r.id}`,{method:"DELETE"});return i.headers.set("Accept","application/vnd.github.v3+json"),await c(i),{reaction:r,deleted:!0}}(r,i),l=`button[reaction][formaction="${r}"][value="${i}"],[reaction-count][reaction-url="${r}"]`,h=Array.from(document.querySelectorAll(l)),u=o?-1:1;for(const e of h)e.setAttribute("reaction-count",(parseInt(e.getAttribute("reaction-count"),10)+u).toString());n.disabled=!1,b()}),!0)}function x(e,t){const n=t=>$(e,t,!1,0)+`${w[t]}`;return`\n \n ${A}\n
\n `);const r=s.nextElementSibling,i=r.lastElementChild,o=i.lastElementChild;return i.onclick=n,{setBusy(){o.textContent="Loading...",i.disabled=!0},remove(){i.onclick=null,r.remove()}}}renderCount(){this.countAnchor.textContent=`${this.count} Comment${1===this.count?"":"s"}`}}let _;function M(){return _||(_=h("utterances.json").then((e=>(Array.isArray(e.origins)||(e.origins=[]),e)),(()=>({origins:[t.origin]})))),_}const j=`data:image/svg+xml;base64,${btoa('')}`;class R{constructor(e,n){this.user=e,this.submit=n,this.submitting=!1,this.renderTimeout=0,this.handleInput=()=>{M();const e=this.textarea.value,t=/^\s*$/.test(e);this.submitButton.disabled=t,this.textarea.scrollHeight<450&&this.textarea.offsetHeightfunction(e){return c(a("markdown",{method:"POST",body:JSON.stringify({text:e,mode:"gfm",context:`${i}/${o}`})})).then((e=>e.text()))}(e).then((e=>this.preview.innerHTML=e)).then((()=>S(this.preview))).then(b)),500))},this.handleSubmit=async e=>{e.preventDefault(),this.submitting||(this.submitting=!0,this.textarea.disabled=!0,this.submitButton.disabled=!0,await this.submit(this.textarea.value).catch((()=>0)),this.submitting=!1,this.textarea.disabled=!this.user,this.textarea.value="",this.submitButton.disabled=!1,this.handleClick({...e,target:this.form.querySelector(".tabnav-tab.tab-write")}),this.preview.textContent="Nothing to preview")},this.handleClick=({target:e})=>{if(!(e instanceof HTMLButtonElement&&e.classList.contains("tabnav-tab")))return;if("true"===e.getAttribute("aria-selected"))return;this.form.querySelector('.tabnav-tab[aria-selected="true"]').setAttribute("aria-selected","false"),e.setAttribute("aria-selected","true");const t=e.classList.contains("tab-preview");this.textarea.style.display=t?"none":"",this.preview.style.display=t?"":"none",b()},this.handleKeyDown=({which:e,ctrlKey:t})=>{13===e&&t&&!this.submitButton.disabled&&this.form.dispatchEvent(new CustomEvent("submit"))},this.element=document.createElement("article"),this.element.classList.add("timeline-comment"),this.element.innerHTML=`\n \n \n \n `,this.avatarAnchor=this.element.firstElementChild,this.avatar=this.avatarAnchor.firstElementChild,this.form=this.avatarAnchor.nextElementSibling,this.textarea=this.form.firstElementChild.nextElementSibling.firstElementChild,this.preview=this.form.firstElementChild.nextElementSibling.lastElementChild,this.signInAnchor=this.form.lastElementChild.lastElementChild,this.submitButton=this.signInAnchor.previousElementSibling,this.setUser(e),this.submitButton.disabled=!0,this.textarea.addEventListener("input",this.handleInput),this.form.addEventListener("submit",this.handleSubmit),this.form.addEventListener("click",this.handleClick),this.form.addEventListener("keydown",this.handleKeyDown),function(e){const t=()=>{removeEventListener("mousemove",b),removeEventListener("mouseup",t)},n=()=>{addEventListener("mousemove",b),addEventListener("mouseup",t)};e.addEventListener("mousedown",n)}(this.textarea)}setUser(e){this.user=e,this.submitButton.hidden=!e,this.signInAnchor.hidden=!!e,e?(this.avatarAnchor.href=e.html_url,this.avatar.alt="@"+e.login,this.avatar.src=e.avatar_url+"?v=3&s=88",this.textarea.disabled=!1,this.textarea.placeholder="Leave a comment"):(this.avatarAnchor.removeAttribute("href"),this.avatar.alt="@anonymous",this.avatar.src=j,this.textarea.disabled=!0,this.textarea.placeholder="Sign in to comment")}clear(){this.textarea.value=""}}var O;function H(){return null!==t.issueNumber?(e=t.issueNumber,c(a(`repos/${i}/${o}/issues/${e}`)).then((e=>{if(!e.ok)throw new Error("Error fetching issue via issue number.");return e.json()}))):function(e){const t=`"${e}" type:issue in:title repo:${i}/${o}`;return c(a(`search/issues?q=${encodeURIComponent(t)}&sort=created&order=asc`)).then((e=>{if(!e.ok)throw new Error("Error fetching issue via search.");return e.json()})).then((n=>{if(0===n.total_count)return null;n.total_count>1&&console.warn(`Multiple issues match "${t}".`),e=e.toLowerCase();for(const t of n.items)if(-1!==t.title.toLowerCase().indexOf(e))return t;return console.warn(`Issue search results do not contain an issue with title matching "${e}". Using first result.`),n.items[0]}))}(t.issueTerm);var e}i=(O=t).owner,o=O.repo,async function(){await async function(){if(n.value)return n.value;if(!t.session)return null;const e=await fetch("https://api.utteranc.es/token",{method:"POST",mode:"cors",credentials:"include",headers:{"content-type":"application/json"},body:JSON.stringify(t.session)});if(e.ok){const t=await e.json();return n.value=t,t}return null}();let[e,s]=await Promise.all([H(),null===n.value?Promise.resolve(null):c(a("user")).then((e=>e.ok?e.json():null)),(r=t.theme,l=t.origin,new Promise((e=>{const t=document.createElement("link");t.rel="stylesheet",t.setAttribute("crossorigin","anonymous"),t.onload=e,t.href=`/stylesheets/themes/${r}/utterances.css`,document.head.appendChild(t),addEventListener("message",(e=>{e.origin===l&&"set-theme"===e.data.type&&(t.href=`/stylesheets/themes/${e.data.theme}/utterances.css`)}))})))]);var r,l;!function(e){p=e,addEventListener("resize",b),addEventListener("load",b)}(t.origin);const h=new T(s,e);if(document.body.appendChild(h.element),e&&e.comments>0&&async function(e,t){const n=e=>{for(const n of e)t.insertComment(n,!1)},s=Math.ceil(e.comments/25),r=[u(e.number,1)];s>1&&r.push(u(e.number,s));s>2&&e.comments%25<3&&e.comments%25!=0&&r.push(u(e.number,s-1));const i=await Promise.all(r);for(const e of i)n(e);let o=s-r.length,a=2;const l=s=>{if(0===o)return;const r=async()=>{c.setBusy();const t=await u(e.number,a);c.remove(),n(t),o--,a++,l(t)},i=s.pop(),c=t.insertPageLoader(i,25*o,r)};l(i[0])}(e,h),b(),e&&e.locked)return;E(!!s);const d=new R(s,(async s=>{await async function(){const{origins:e}=await M(),{origin:n,owner:s,repo:r}=t;if(-1!==e.indexOf(n))return;throw document.querySelector(".timeline").lastElementChild.insertAdjacentHTML("beforebegin",`\n
\n Error: ${n} is not permitted to post to ${s}/${r}.\n Confirm this is the correct repo for this site's comments. If you own this repo,\n \n update the utterances.json\n \n to include ${n} in the list of origins.
\n Suggested configuration: \n
${JSON.stringify({origins:[n]},null,2)}
\n
`),b(),new Error("Origin not permitted.")}(),e||(e=await function(e,t,s,r,a){const l=`https://api.utteranc.es/repos/${i}/${o}/issues${a?`?label=${encodeURIComponent(a)}`:""}`,c=new Request(l,{method:"POST",body:JSON.stringify({title:e,body:`# ${s}\n\n${r}\n\n[${t}](${t})`})});return c.headers.set("Accept","application/vnd.github.v3+json"),c.headers.set("Authorization",`token ${n.value}`),fetch(c).then((e=>{if(!e.ok)throw new Error("Error creating comments container issue");return e.json()}))}(t.issueTerm,t.url,t.title,t.description||"",t.label),h.setIssue(e));const r=await function(e,t){const n=a(`repos/${i}/${o}/issues/${e}/comments`,{method:"POST",body:JSON.stringify({body:t})});return n.headers.set("Accept","application/vnd.github.VERSION.html+json,application/vnd.github.v3+json"),c(n).then((e=>{if(!e.ok)throw new Error("Error posting comment.");return e.json()}))}(e.number,s);h.insertComment(r,!0),d.clear()}));h.element.appendChild(d.element)}(),addEventListener("not-installed",(function e(){removeEventListener("not-installed",e),document.querySelector(".timeline").insertAdjacentHTML("afterbegin",`\n
\n Error: utterances is not installed on ${t.owner}/${t.repo}.\n If you own this repo,\n install the app.\n Read more about this change in\n the PR.\n