{"size": 918, "ext": "js", "max_stars_repo_path": "react-Leonidas/Dominando-array/forEach/js/search.js", "max_stars_repo_name": "jhonnyagudelo/todosLosCursos", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "react-Leonidas/Dominando-array/forEach/js/search.js", "max_issues_repo_name": "jhonnyagudelo/todosLosCursos", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "react-Leonidas/Dominando-array/forEach/js/search.js", "max_forks_repo_name": "jhonnyagudelo/todosLosCursos", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.95, "max_line_length": 67, "alphanum_fraction": 0.6644880174}
import movies from "./movies.js";
import render from "./render.js";

const search = window["search-form"];

search.addEventListener("submit", function (event) {
  event.preventDefault();
  const formData = new FormData(this);
  const title = formData.get("title");
  //const movies = filterByTitle(title);
  const movies = searchMovie(title);
  console.log(movies);
  if (movies) {
    return render(movies);
  } else {
    return alert("No encontramos tu pelicula");
  }
});

function filterByTitle(title) {
  return movies.filter((movie) => {
    return movie.title.toLowerCase().includes(title.toLowerCase());
  });
}

function findById(id) {
  return movies.find((movie) => {
    //return movies.filter((movie) => {
    return movie.id === parseInt(id, 10);
  });
}

function searchMovie(query) {
  if (isNaN(query)) {
    return filterByTitle(query);
  }
  return [findById(query)];
  //return findById(query);
}
