(function (global, $) {
  var Browsr = function (target) {
    return new Browsr.init(target);
  };

  let found = false,
    book = {
      found: false,
    },
    chapter = {
      found: false,
    },
    page = {
      found: false,
    };

  Browsr.prototype = {
    match(value, type = "book") {
      $(value.classList).map((_, value) => {
        if (type == "book") {
          if (value.startsWith(type + "-")) {
            book.class = value;
          }
        } else if (type == "chapter") {
          if (value.startsWith(type + "-")) {
            chapter.class = value;
          }
        }
      });
    },
    checkPage(value) {
      found = false;
      $(value.classList).map((_, value) => {
        if (value.startsWith("page-")) {
          if (page.class == value) {
            found = true;
            page.found = true;
          }
        }
      });
    },
    checkChapter(value) {
      found = false;
      $(value.classList).map((_, value) => {
        if (value.startsWith("chapter-")) {
          if (chapter.class == value) {
            found = true;
            chapter.found = true;
          }
        }
      });
    },
    checkBook(value) {
      found = false;
      $(value.classList).map((_, value) => {
        if (value.startsWith("book-")) {
          if (book.class == value) {
            found = true;
            book.found = true;
          }
        }
      });
    },
    setPageUrl(index) {
      if (!this.pathify()[index]) {
        $(page.links[0].classList).map((_, value) => {
          if (value.startsWith("page-")) {
            page.url = value.slice(5);
          }
        });
      } else {
        page.url = this.pathify()[index];
      }
    },
    setChapterUrl(index) {
      if (!this.pathify()[index]) {
        $(chapter.links[0].classList).map((_, value) => {
          if (value.startsWith("chapter-")) {
            chapter.url = value.slice(8);
          }
        });
      } else {
        chapter.url = this.pathify()[index];
      }
    },
    nunify(value) {
      gsap.set(value, { display: "none" });
    },
    shadify(value) {
      gsap.set(value, { background: "rgba(0,0,0,0.05)" });
    },
    clearify(value, attr = "background") {
      gsap.set(value, { clearProps: attr });
    },
    bookify(value) {
      return $(".block.browse-book", ".wrap.browse-" + value);
    },
    linkify(value) {
      return $(".link.browse-" + value, ".wrap.browse-" + value);
    },
    classify(value, type) {
      return type + "-" + value;
    },
    pathify() {
      return window.location.pathname.split("/").map((value) => {
        if (value != "") {
          return value;
        }
      });
    },
    setup(library) {
      chapter.links = this.linkify("chapter");
      page.links = this.linkify("page");
      if (library) {
        book.url = this.pathify()[1];
        this.setChapterUrl(2);
        this.setPageUrl(3);
        book.links = this.linkify("book");
        book.class = this.classify(book.url, "book");
      } else {
        this.setChapterUrl(1);
        this.setPageUrl(2);
      }
      chapter.books = this.bookify("chapter");
      chapter.class = this.classify(chapter.url, "chapter");
      page.books = this.bookify("page");
      page.class = this.classify(page.url, "page");
    },
    start(library) {
      this.setup(library);

      if (library) {
        if (book.url) {
          book.links.map((_, value) => {
            this.checkBook(value);
            if (found) {
              this.shadify(value);
            }
          });
        }
      }

      if (library) {
        chapter.books.map((_, value) => {
          this.checkBook(value);
          if (found) {
            chapter.books.map((_, value) => {
              $(value.children).map((_, value) => {
                this.checkChapter(value);
                if (found) {
                  this.shadify(value);
                }
              });
            });
          } else {
            this.nunify(value);
          }
        });
      } else {
        chapter.books.map((_, value) => {
          $(value.children).map((_, value) => {
            this.checkChapter(value);
            if (found) {
              this.shadify(value);
            }
          });
        });
      }

      if (library) {
        page.books.map((_, value) => {
          this.checkBook(value);

          if (found) {
            page.books.map((_, value) => {
              $(value.children).map((_, value) => {
                this.checkChapter(value);

                if (found) {
                  $(value.children).map((_, value) => {
                    this.checkPage(value);

                    if (found) {
                      this.shadify(value);
                    }
                  });
                } else {
                  this.nunify(value);
                }
              });
            });
          } else {
            this.nunify(value);
          }
        });
      } else {
        page.books.map((_, value) => {
          $(value.children).map((_, value) => {
            this.checkChapter(value);

            if (found) {
              $(value.children).map((_, value) => {
                this.checkPage(value);

                if (found) {
                  this.shadify(value);
                }
              });
            } else {
              this.nunify(value);
            }
          });
        });
      }

      return this;
    },
    respond(library) {
      if (library) {
        book.links.hover((e) => {
          this.match(e.target);
          this.clearify(book.links);
          this.shadify(e.target);

          chapter.books.map((_, value) => {
            this.nunify(value);
            this.clearify(value.children);
            this.checkBook(value);

            if (found) {
              this.clearify(value, "display");
              this.shadify(value.children[0]);
            }
          });

          page.books.map((_, value) => {
            this.nunify(value);
            this.checkBook(value);

            if (found) {
              this.clearify(value, "display");

              $(value.children).map((index, value) => {
                this.nunify(value);
                this.clearify(value.children);

                if (index == 0) {
                  this.clearify(value, "display");
                  this.shadify(value.children[0]);
                }
              });
            }
          });
        });

        chapter.links.hover((e) => {
          this.match(e.target, "chapter");
          this.clearify(chapter.links);
          this.shadify(e.target);

          page.books.map((_, value) => {
            this.nunify(value);
            this.checkBook(value);

            if (found) {
              this.clearify(value, "display");

              $(value.children).map((_, value) => {
                this.nunify(value);
                this.clearify(value.children);
                this.checkChapter(value);

                if (found) {
                  this.clearify(value, "display");
                  this.shadify(value.children[0]);
                }
              });
            }
          });
        });
        page.links.hover((e) => {
          this.clearify(page.links);
          this.shadify(e.target);
        });
      } else {
        chapter.links.hover((e) => {
          this.match(e.target, "chapter");
          this.clearify(chapter.links);
          this.shadify(e.target);

          page.books.map((_, value) => {
            $(value.children).map((_, value) => {
              this.nunify(value);
              this.clearify(value.children);
              this.checkChapter(value);

              if (found) {
                this.clearify(value, "display");
                this.shadify(value.children[0]);
              }
            });
          });
        });
        page.links.hover((e) => {
          this.clearify(page.links);
          this.shadify(e.target);
        });
      }

      return this;
    },
  };

  Browsr.init = function (library) {
    var self = this;
    self.target = document;
    if (library == null) {
      library = false;
    }
    this.start(library).respond(library);
  };

  Browsr.init.prototype = Browsr.prototype;

  global.Browsr = Browsr;
})(window, $);