/**
 * Copyright since 2007 PrestaShop SA and Contributors
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License 3.0 (AFL-3.0)
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to https://devdocs.prestashop.com/ for more information.
 *
 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
 * @copyright Since 2007 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 */
/* eslint-disable */
import "jquery-offcanvas/dist/jquery.offcanvas.min.css";
import touchspin from "bootstrap-touchspin";
import "jquery-touchswipe";
import "./selectors";

import "./responsive";
import "./checkout";
import "./customer";
import "./listing";
import "./product";
import "./cart";
import "./facets";

import prestashop from "prestashop";
import EventEmitter from "events";
import DropDown from "./components/drop-down";
import Form from "./components/form";
import usePasswordPolicy from "./components/usePasswordPolicy";
import ProductMinitature from "./components/product-miniature";
import ProductSelect from "./components/product-select";
import TopMenu from "./components/top-menu";

import "./components/block-cart";
import $ from "jquery";
import jo from "jquery-offcanvas";
jo(window, $);
touchspin(window, $);
/* eslint-enable */

// "inherit" EventEmitter
// eslint-disable-next-line
for (const i in EventEmitter.prototype) {
  prestashop[i] = EventEmitter.prototype[i];
}

$(function () {
  const dropDownEl = $(".js-dropdown");
  const form = new Form();
  const topMenuEl = $('.js-top-menu ul[data-depth="0"]');
  const dropDown = new DropDown(dropDownEl);
  const topMenu = new TopMenu(topMenuEl);
  const productMinitature = new ProductMinitature();
  const productSelect = new ProductSelect();
  dropDown.init();
  form.init();
  topMenu.init();
  productMinitature.init();
  productSelect.init();
  usePasswordPolicy(".field-password-policy");

  $('.carousel[data-touch="true"]').swipe({
    swipe(event, direction) {
      if (direction === "left") {
        $(this).carousel("next");
      }
      if (direction === "right") {
        $(this).carousel("prev");
      }
    },
    allowPageScroll: "vertical",
  });

  // $("#products_top_sidebar").offcanvas({
  //   effect: "slide-in-over",
  //   overlay: true,
  //   classes: {
  //     element: "absolute top-0 z-50",
  //   },
  // });
  //
  // $("#show_filters").on("click", function () {
  //   $("#products_top_sidebar").offcanvas("toggle");
  // });

  function ThAccordion() {}

  $.extend(ThAccordion.prototype, {
    init() {},
  });

  $.fn["thaccordion"] = function () {
    var selectors = {
      root: ".th-accordion",
      item: ".th-accordion-item",
      trigger: ".th-accordion-item-trigger",
      content: ".th-accordion-item-content",
    };
    let selection = null;
    let items = this.find(selectors.item);

    function collapseAll() {
      items.each(function () {
        $(this).find(selectors.content).hide(500);
      });
    }

    function open() {
      collapseAll();
      $(selection).find(selectors.content).show(500);
    }

    function isOpen(item) {
      return $(item).find(selectors.content).is(":visible");
    }

    collapseAll();

    items.each(function () {
      var self = this;
      $(this)
        .find(selectors.trigger)
        .on("click", function () {
          if (selection === self) {
            selection = null;
          } else {
            selection = self;
            if (!isOpen(selection)) {
              open();
            }
          }
        });
    });
  };

  $(".th-accordion").thaccordion();
});