const t=(e,a)=>{let n=(e.document||e.ownerDocument).querySelectorAll(a),s=t.length;for(;--s>=0&&n.item(s)!==e;);return s>-1},e={noop:()=>{},$(t,e=null){if("string"!=typeof t)return t;e=null!==e&&1===e.nodeType?e:document;let a=[];return t.match(/^#([\w]+$)/)?a.push(document.getElementById(t.split("#")[1])):a=[].slice.apply(e.querySelectorAll(t)),a},hasElement:t=>e.$(t).length>0,scrollToTop(){e.$("header")[0].scrollIntoView(!0)},hide(t){"string"==typeof t&&(t=e.$(t)),Array.isArray(t)?t.forEach(t=>t.setAttribute("hidden","hidden")):t.setAttribute("hidden","hidden")},show(t){"string"==typeof t&&(t=e.$(t)),Array.isArray(t)?t.forEach(t=>t.removeAttribute("hidden")):t.removeAttribute("hidden")},showMessage(t,a){let n=`
\n\t\t\t\t\n\t\t\t\t${a}\n\t\t\t\t\n\t\t\t
`,s=e.$(".message");void 0!==s[0]&&s[0].remove(),e.$("header")[0].insertAdjacentHTML("beforeend",n)},closestParent(e,a){if(void 0!==Element.prototype.closest)return e.closest(a);for(;e!==document.documentElement;){if(t(e,a))return e;e=e.parentElement}return null},url(t){let e="//"+document.location.host;return e+="/"===t.charAt(0)?t:"/"+t,e},throttle(t,e,a){let n=!1;return function(...s){const o=a||this;n||(e.apply(o,s),n=!0,setTimeout((function(){n=!1}),t))}}};function a(t,e,n){e.match(/^([\w\-]+)$/)||e.split(" ").forEach(e=>{a(t,e,n)}),t.addEventListener(e,n,!1)}function n(t){let e=[];return Object.keys(t).forEach(a=>{let n=t[a].toString();a=encodeURIComponent(a),n=encodeURIComponent(n),e.push(`${a}=${n}`)}),e.join("&")}e.on=(t,n,s,o)=>{void 0===o?(o=s,e.$(t).forEach(t=>{a(t,n,o)})):e.$(t).forEach(t=>{!function(t,n,s,o){a(t,s,a=>{e.$(n,t).forEach(t=>{a.target==t&&(o.call(t,a),a.stopPropagation())})})}(t,s,n,o)})},e.ajax=(t,a)=>{const s={data:{},type:"GET",dataType:"",success:e.noop,mimeType:"application/x-www-form-urlencoded",error:e.noop};a={...s,...a};let o=new XMLHttpRequest,r=String(a.type).toUpperCase();"GET"===r&&(t+=t.match(/\?/)?n(a.data):"?"+n(a.data)),o.open(r,t),o.onreadystatechange=()=>{if(4===o.readyState){let t="";t="json"===o.responseType?JSON.parse(o.responseText):o.responseText,o.status>299?a.error.call(null,o.status,t,o.response):a.success.call(null,t,o.status)}},"json"===a.dataType?(a.data=JSON.stringify(a.data),a.mimeType="application/json"):a.data=n(a.data),o.setRequestHeader("Content-Type",a.mimeType),"GET"===r?o.send(null):o.send(a.data)},e.get=(t,a,n=null)=>(null===n&&(n=a,a={}),e.ajax(t,{data:a,success:n})),e.on("header","click",".message",(function(t){e.hide(t.target)})),e.on("form.js-delete","submit",(function(t){!1===confirm("Are you ABSOLUTELY SURE you want to delete this item?")&&(t.preventDefault(),t.stopPropagation())})),e.on(".js-clear-cache","click",(function(){e.get("/cache_purge",()=>{e.showMessage("success","Successfully purged api cache")})})),e.on(".vertical-tabs input","change",(function(t){const e=t.currentTarget.parentElement.getBoundingClientRect().top+window.pageYOffset;window.scrollTo({top:e,behavior:"smooth"})})),e.on(".media-filter","input",(function(t){const a=t.target.value,n=new RegExp(a,"i");""!==a?(e.$("article.media").forEach(t=>{const a=e.$(".name a",t)[0],s=String(a.textContent).trim();n.test(s)?e.show(t):e.hide(t)}),e.$("table.media-wrap tbody tr").forEach(t=>{const a=e.$("td.align-left",t)[0],s=e.$("a",a)[0],o=String(s.textContent).trim(),r=String(a.textContent).trim();n.test(o)||n.test(r)?e.show(t):e.hide(t)})):(e.show("article.media"),e.show("table.media-wrap tbody tr"))})),"serviceWorker"in navigator&&navigator.serviceWorker.register("/sw.js").then(t=>{console.log("Service worker registered",t.scope)}).catch(t=>{console.error("Failed to register service worker",t)}),e.on("main","change",".big-check",t=>{const e=t.target.id;document.getElementById("mal_"+e).checked=!0});e.hasElement(".anime #search")&&e.on("#search","input",e.throttle(250,t=>{const a=encodeURIComponent(t.target.value);""!==a&&(t=>{e.show(".cssload-loader"),e.get(e.url("/anime-collection/search"),{query:t},(t,a)=>{t=JSON.parse(t),e.hide(".cssload-loader"),e.$("#series-list")[0].innerHTML=function(t){const e=[];return t.forEach(t=>{const a=t.attributes,n=a.titles.reduce((t,e)=>t+(e+"
"),[]);e.push(`\n\t\t\t\n\t\t`)}),e.join("")}(t.data)})})(a)})),e.on("body.anime.list","click",".plus-one",t=>{let a=e.closestParent(t.target,"article"),n=parseInt(e.$(".completed_number",a)[0].textContent,10)||0,s=parseInt(e.$(".total_number",a)[0].textContent,10),o=e.$(".name a",a)[0].textContent,r={id:a.dataset.kitsuId,mal_id:a.dataset.malId,data:{progress:n+1}};(isNaN(n)||0===n)&&(r.data.status="current"),isNaN(n)||n+1!==s||(r.data.status="completed"),e.show("#loading-shadow"),e.ajax(e.url("/anime/increment"),{data:r,dataType:"json",type:"POST",success:t=>{const s=JSON.parse(t);if(s.errors)return e.hide("#loading-shadow"),e.showMessage("error",`Failed to update ${o}. `),void e.scrollToTop();"completed"===s.data.attributes.status&&e.hide(a),e.hide("#loading-shadow"),e.showMessage("success","Successfully updated "+o),e.$(".completed_number",a)[0].textContent=++n,e.scrollToTop()},error:()=>{e.hide("#loading-shadow"),e.showMessage("error",`Failed to update ${o}. `),e.scrollToTop()}})});e.hasElement(".manga #search")&&e.on("#search","input",e.throttle(250,t=>{let a=encodeURIComponent(t.target.value);""!==a&&(t=>{e.show(".cssload-loader"),e.get(e.url("/manga/search"),{query:t},(t,a)=>{t=JSON.parse(t),e.hide(".cssload-loader"),e.$("#series-list")[0].innerHTML=function(t){const e=[];return t.forEach(t=>{const a=t.attributes,n=a.titles.reduce((t,e)=>t+(e+"
"),[]);e.push(`\n\t\t\t\n\t\t`)}),e.join("")}(t.data)})})(a)})),e.on(".manga.list","click",".edit-buttons button",t=>{let a=t.target,n=e.closestParent(t.target,"article"),s=a.classList.contains("plus-one-chapter")?"chapter":"volume",o=parseInt(e.$(`.${s}s_read`,n)[0].textContent,10)||0,r=parseInt(e.$(`.${s}_count`,n)[0].textContent,10),i=e.$(".name",n)[0].textContent;isNaN(o)&&(o=0);let l={id:n.dataset.kitsuId,mal_id:n.dataset.malId,data:{progress:o}};(isNaN(o)||0===o)&&(l.data.status="current"),isNaN(o)||o+1!==r||(l.data.status="completed"),l.data.progress=++o,e.show("#loading-shadow"),e.ajax(e.url("/manga/increment"),{data:l,dataType:"json",type:"POST",mimeType:"application/json",success:()=>{"completed"===l.data.status&&e.hide(n),e.hide("#loading-shadow"),e.$(`.${s}s_read`,n)[0].textContent=o,e.showMessage("success","Successfully updated "+i),e.scrollToTop()},error:()=>{e.hide("#loading-shadow"),e.showMessage("error","Failed to update "+i),e.scrollToTop()}})});