import { InobyModule } from "@lib/inoby-module";
import * as $ from "jquery";

class ProductArchiveModule extends InobyModule {
  run() {
    $(".products-filter-init").on("click", () => {
      // toggle active class
      const $sb = $("#shop-sidebar");
      $sb.toggleClass("active");

      if ($sb.hasClass("active")) {
        $("input[name=disable_auto_apply_filter]").val("true");
        $sb.attr("data-top", $sb.css("top"));
        $sb.css("top", "");
      } else {
        $("input[name=disable_auto_apply_filter]").val("false");
        $sb.css("top", $sb.attr("data-top") ? $sb.attr("data-top") : "");
      }

      // reset filter settings in RC
      window.rootcommerce.App.initFilterSettings();

      // toggle body class
      $("body").toggleClass("filter-opened");
    });

    // init "Filter" btn
    $(".filter-trigger").on("click", (e) => {
      e.preventDefault();
      $(document.body).trigger("rc.refresh_products");
      $(".products-filter-init").trigger("click");
    });
  }
}

new ProductArchiveModule().runOnReady();
