text: !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).Octomments=t()}(this,function(){"use strict";function c(e){return function(e){if(Array.isArray(e))return e}(e)||t(e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function a(e){return function(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}(e)||t(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function t(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function s(){return!0===function(){var e="test";try{return localStorage.setItem(e,e),localStorage.removeItem(e),!0}catch(e){return!1}}()?localStorage:{setItem:function(){},getITem:function(){},removeItem:function(){}}}function u(e,t){t=t||window.location.href,e=e.replace(/[\[\]]/g,"\\$&");var n=new RegExp("[?&]".concat(e,"(=([^&#]*)|&|#|$)")).exec(t);return n?n[2]?decodeURIComponent(n[2].replace(/\+/g," ")):"":null}function f(t){return["code","error","error_description","error_uri","t"].forEach(function(e){t=t.replace(new RegExp("[?&]".concat(e,"=[^&]+")),"")}),t}function p(e){return{id:e.id,url:e.html_url,author:{login:e.user.login,avatarUrl:e.user.avatar_url,url:e.user.html_url},body:e.body_html,createdAt:e.created_at,updatedAt:e.updated_at}}var l="ERROR",m="USER_LOADING",g="USER_NONE",d="USER_LOADED",n="COMMENTS_LOADING",w="COMMENTS_LOADED",h="COMMENT_SAVING",v="COMMENT_SAVED",E="OCTOMMENTS_USER",y=[l,n,w,h,m,g,d,v];function b(i,e){var c=i.notify,t=i.options,a=i.error,s=t.endpoints,u=t.issueNumber,f=t.github,l=!!s,m=new Error("Error getting comments for issue #".concat(u,".")),r=new Error("Issue #".concat(u," doesn't exists."));function d(e){console.error(e),a(m,2)}function h(n){return function(e,t){if(t)a(m,2);else if(e.ok)n(e);else{if(404===e.status)return a(r,1);n(e)}}}c(n),function r(e){var o=0<arguments.length&&void 0!==e?e:1,t="https://api.github.com/repos/".concat(f.owner,"/").concat(f.repo,"/issues/").concat(u,"/comments?page=").concat(o);fetch(t,{headers:i.getHeaders()}).then(h(function(e){if(!e.ok)return 401===e.status?(i.logout(!1),c(g),void r(o)):403===e.status?l?void fetch("".concat(s.issue,"?owner=").concat(f.owner,"&repo=").concat(f.repo,"&number=").concat(u)).then(h(function(e){e.ok?e.json().then(function(e){var t=e.issue.comments;i.data={comments:i.data.comments.concat(t),pagination:null},c(w,t,null)}).catch(function(e){console.error(e),a(new Error("Error parsing the API response"),3)}):a(m,2)})).catch(d):a(new Error("Rate limit exceeded."),4):a(m,2);var t=e.headers.get("Link"),n=null;t&&(n=function(e){var t=e.split(","),n={};for(var r in t){var o=t[r],i={};i.name=o.match(/rel=\"([^\"]*)/)[1],i.url=o.match(/<([^>]*)/)[1],i.page=parseInt(o.match(/page=(\d+).*$/)[1],10),n[i.name]=i}return n}(t)),e.json().then(function(e){var t=e.map(p);i.data={comments:i.data.comments.concat(t),pagination:n},c(w,t,n)}).catch(d)}))}(e)}function S(n,e){var r=n.notify,o=n.error,t=n.options,i=t.issueNumber,c=t.github,a=new Error("Adding a new comment failed.");r(h);var s,u="https://api.github.com/repos/".concat(c.owner,"/").concat(c.repo,"/issues/").concat(i,"/comments");function f(e){console.error(e),o(a,10)}fetch(u,{method:"POST",headers:n.getHeaders(),body:JSON.stringify({body:e})}).then((s=function(e){r(v,[p(e)])},function(e,t){return t?o(a,8):e.ok?void e.json().then(function(e){e?s(e):o(new Error("Parsing new-comment response failed."),10)}).catch(f):401===e.status?(n.logout(!1),r(g),o(new Error("Not authorized. Log in again."),9)):403===e.status?o(new Error("Rate limit exceeded."),4):o(a,8)})).catch(f)}function r(o){if(!o)throw new Error("Octomments options required.");if(!o.github||!o.github.owner||!o.github.repo)throw new Error("`options.github` is missing or incomplete.");if(!o.issueNumber)throw new Error("`options.issueNumber` is missing.");o.endpoints||(o.endpoints={issue:"https://ocs.now.sh/octomments/issue",token:"https://ocs.now.sh/octomments/token"}),o.debug&&console.log("Octomments started with: ",JSON.stringify(o,null,2));var i={},n={user:null,data:{comments:[],pagination:null},options:o,LS:s(),error:function(e,t){n.notify(l,e,t)},notify:function(e){for(var t=arguments.length,n=new Array(1<t?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];o.debug&&console.log(e,n),i[e]&&i[e].forEach(function(e){return e.apply(void 0,n)})}};if(n.initUser=function(){return function(n){function r(){return history.replaceState({},document.title,f(location.href))}var o=n.notify,i=n.error,e=n.generateNewCommentURL(),t=n.LS.getItem(E);if(t)try{n.user=JSON.parse(t),o(d,n.user,!1)}catch(e){console.error(e),i(new Error("Corrupted data in local storage."),5)}else if(u("t")){var c=u("t");n.user={token:c},o(m),fetch("https://api.github.com/user",{headers:n.getHeaders()}).then(function(e,t){t||!e.ok?(t&&console.error(t),r(),i(new Error("Problem getting user's info."),6)):e.json().then(function(e){n.user={token:n.user.token,login:e.login,avatarUrl:e.avatar_url,url:e.html_url,name:e.name},r(),n.LS.setItem(E,JSON.stringify(n.user)),o(d,n.user,!0)}).catch(function(e){console.error(e),i(new Error("Problem parsing access token response."),7)})}).catch(function(e){console.error(e),i(new Error("Problem getting access token."),6)})}else o(g,e)}(n)},n.initComments=function(){return b(n)},n.logout=function(){var e=!(0<arguments.length&&void 0!==arguments[0])||arguments[0];n.user=null,n.LS.removeItem(E),e&&location.reload()},n.add=function(e){if(!n.user)throw new Error("No user logged in.");S(n,e)},n.off=function(e){return delete i[e],this},n.on=function(e,t){return i[e]||(i[e]=[]),i[e].push(t),this},n.init=function(){n.initUser(),n.initComments()},n.page=function(e){b(n,e)},n.generateNewCommentURL=function(){return e=o.endpoints.token,t=f(window.location.href),"".concat(e,"?redirect=").concat(encodeURI(t));var e,t},n.getHeaders=function(){var e={"Content-Type":"application/json",Accept:"application/vnd.github.v3.html+json"};return n.user&&n.user.token&&(e.Authorization="token ".concat(n.user.token)),e},y.forEach(function(e){return n[e]=e}),o.renderer){var e=c(o.renderer),t=e[0],r=e.slice(1);t.apply(void 0,[n].concat(a(r)))}return n}return y.forEach(function(e){return r[e]=e}),r.version="1.0.4",r});