import BookSection from "./js/main/index.js";
import ButtonScroll from "./js/scroll/index.js";

const input = document.getElementById("search");
const books = document.getElementById("books");
const content = document.getElementById("content");
const searchButton = document.getElementById("searchButton");
const fortuneButton = document.getElementById("fortuneButton");

const booksSection = new BookSection({
  url: "../api/books/search",
  label: "books",
});

const click = () => {
  const query = document.getElementById("search").value;
  if (query) {
    booksSection.update({ query });
  }
};

const fortuneClick = () => {
  booksSection.update({ isByRandom: true });
};

searchButton.addEventListener("click", click);
fortuneButton.addEventListener("click", fortuneClick);
input.addEventListener("input", function (e) {
  if (e.target.value && e.target.value.length !== 0) {
    searchButton.classList.add("visible");
  } else {
    searchButton.classList.remove("visible");
  }
});
input.addEventListener("keypress", function (event) {
  if (event.key === "Enter") {
    click();
  }
});
books.append(booksSection.element);

const scroll = new ButtonScroll();
content.append(scroll.element);
