/**
 * 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 'bootstrap/dist/js/bootstrap.min';
import "jquery-modal";
import "jquery-offcanvas/dist/jquery.offcanvas.min.css";
import "tooltipster";
import "tooltipster/dist/css/tooltipster.bundle.min.css";
import "tooltipster/dist/css/plugins/tooltipster/sideTip/themes/tooltipster-sideTip-shadow.min.css";
import touchspin from "bootstrap-touchspin";
import "jquery-touchswipe";
import "./selectors";
import "./tabs.js";

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];
}

$(() => {
  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',
  });

  $('#product-list-top-filters').offcanvas({
    duration: 100,
    effect: 'slide-in-over',
    overlay: true,
    classes: {
      element: 'absolute top-0 z-50',
    },
  });

  $('#show-filters').on('click.offcanvas', () => {
    $('#product-list-top-filters').offcanvas('show');
  });

  $('#hide-filters').on('click.offcanvas', () => {
    $('#product-list-top-filters').offcanvas('hide');
  });

  $('#product-list-top-filters').on('shown.offcanvas', () => {
    $('#product-list-top-filters').show();
  });

  $('#product-list-top-filters').on('hide.offcanvas', () => {
    $('#product-list-top-filters').hide();
  });

  function ThAccordion() {}

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

  $.fn.thaccordion = function () {
    const selectors = {
      root: '.th-accordion',
      item: '.th-accordion-item',
      trigger: '.th-accordion-item-trigger',
      svg: '.th-accordion-item-trigger-svg',
      content: '.th-accordion-item-content',
    };
    let selection = null;
    const items = this.find(selectors.item);
    const rotateClass = 'rotate-90 duration-500';

    function collapseAll() {
      items.each(function () {
        $(this).find(selectors.content).hide(500);
        $(this).find(selectors.svg).removeClass(rotateClass);
      });
    }
    function close() {
      $(selection).find(selectors.content).hide(500);
      $(selection).find(selectors.svg).removeClass('rotate-90');
    }

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

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

    collapseAll();

    items.each(function () {
      const self = this;
      $(this)
        .find(selectors.trigger)
        .on('click', () => {
          if (selection === self) {
            if (isOpen(selection)) {
              close();
            } else {
              open();
            }
          } else {
            selection = self;
            if (!isOpen(selection)) {
              open();
            }
          }
        });
    });
  };

  $('.th-accordion').thaccordion();
  $('.tooltip').tooltipster({
    theme: 'tooltipster-shadow',
    triggerOpen: {
      click: true,
      mouseenter: true,
      touchstart: true,
    },
    delayTouch: [0, 2000],
  });

  $('.toggle-main-menu').on('click', () => {
    $('.mobile-main-menu').toggle();
  });
});