text: "use strict";const elementHeader=document.querySelector(".header"),elementForm=document.querySelector("#formSearch"),elementInput=document.querySelector("#inputSearch"),elementButton=document.querySelector("#buttonSearch"),elementContainResults=document.querySelector("#resultsContainer"),elementResultsList=document.querySelector("#resultList"),elementFavList=document.querySelector("#favouriteList"),elementButtonReset=document.querySelector("#btnReset"),elementeValidation=document.querySelector("#textAlertForm"),elemntSectionfav=document.querySelector("#sectionfavList"),urlBase="https://api.tvmaze.com/search/shows?q=",imgDefault="https://via.placeholder.com/210x295/ffffff/666666/? text=TV.";let favouriteSerie=[];const conectSearchHandler=()=>{const e=elementInput.value.toLowerCase();fetch(urlBase+e).then(e=>e.json()).then(e=>{console.log(e),fillInput(),showSearchResults(e),transitionHeader()})},fillInput=()=>{""===elementInput.value?(console.log("rellenar campos"),elementInput.classList.add("input-no-valid"),elementInput.setAttribute("placeholder","Nombre de la serie")):elementInput.classList.remove("input-no-valid")};function setLocalStorage(){localStorage.setItem("favouriteSerie",JSON.stringify(favouriteSerie))}function getLocalStorage(){let e=localStorage.getItem("favouriteSerie",favouriteSerie);null===e?elemntSectionfav.classList.add("hidden"):(elemntSectionfav.classList.remove("hidden"),favouriteSerie=JSON.parse(e),paintFavSeries(favouriteSerie))}const showSearchResults=e=>{for(let t=0;t<e.length;t++){const n=document.createElement("li");n.classList.add("liSearch"),n.setAttribute("id",e[t].show.id);const r=document.createElement("img");null==e[t].show.image?r.src=imgDefault:r.src=e[t].show.image.medium,r.setAttribute("class","imgSerie");const i=document.createElement("span");i.setAttribute("class","spanSerie");const o=document.createTextNode(e[t].show.name);i.appendChild(o),n.appendChild(r),n.appendChild(i),elementResultsList.appendChild(n),lisenFavorites()}},lisenFavorites=()=>{const e=document.querySelectorAll(".liSearch");for(const t of e)t.addEventListener("click",checkFavList)};function checkFavList(){const e=event.currentTarget,t=e.querySelector(".spanSerie"),n=e.querySelector(".imgSerie");if(elemntSectionfav.classList.remove("hidden"),e.classList.toggle("selected"),e.classList.contains("selected")){const r={name:t.innerHTML,image:n.src,id:e.id};favouriteSerie.push(r),localStorage.setItem("favouriteSerie",JSON.stringify(favouriteSerie)),paintFavSeries(favouriteSerie)}else if(!e.classList.contains("selected")){favouriteSerie=JSON.parse(localStorage.getItem("favouriteSerie"));const t=findIndex(e.id,favouriteSerie);favouriteSerie.splice(t,1),localStorage.setItem("favouriteSerie",JSON.stringify(favouriteSerie)),paintFavSeries(favouriteSerie)}console.log(favouriteSerie)}function findIndex(e,t){for(let n=0;n<t.length;n++)if(t[n].id===e)return t.indexOf(t[n])}const paintFavSeries=e=>{elementFavList.innerHTML="";for(let t of e){const e=document.createElement("li");e.setAttribute("id",t.id),e.classList.add("favLi");const n=document.createElement("div"),r=document.createElement("span");r.setAttribute("class","spanfav");const i=document.createElement("img"),o=document.createElement("button");o.setAttribute("type","button"),o.setAttribute("class","btnRemove"),o.classList.add("buttonRemove"),o.innerHTML="x",i.classList.add("imgStyle"),i.src=t.image,r.innerHTML=t.name,e.appendChild(o),n.appendChild(r),n.appendChild(i),e.appendChild(n),elementFavList.appendChild(e),o.addEventListener("click",selectButtonRemoveFav)}},selectButtonRemoveFav=()=>{const e=document.querySelectorAll(".buttonRemove");for(const t of e)t.addEventListener("click",removeFavorite)},removeFavorite=e=>{const t=e.currentTarget.closest("li").id,n=findRemoveIndex(t,favouriteSerie);favouriteSerie.splice(n,1),e.currentTarget.parentElement.remove(e.target),setLocalStorage(),console.log(favouriteSerie,"me estan borrando")},findRemoveIndex=(e,t)=>{for(let n=0;n<t.length;n++)if(t[n].id===e)return console.log("indexfav"),t.indexOf(t[n])},removeSearch=()=>{""!==elementResultsList.innerHTML&&(elementResultsList.innerHTML="")},enterKeySearchHandler=e=>{e.preventDefault(),conectSearchHandler()};console.log(favouriteSerie);const resetLocalStorage=()=>{localStorage.clear(),elementFavList.innerHTML="",favouriteSerie=[],elemntSectionfav.classList.add("hidden")},transitionHeader=()=>{elementHeader.classList.remove("transition"),elementHeader.classList.add("notransition")};window.addEventListener("load",getLocalStorage),elementButtonReset.addEventListener("click",resetLocalStorage),elementForm.addEventListener("submit",enterKeySearchHandler),elementInput.addEventListener("change",removeSearch),elementButton.addEventListener("click",conectSearchHandler);