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

class Checkout extends InobyModule {
  run() {
    this.markActivePaymentMethod();
    this.updateCheckoutSteps();
    this.CartCrossSellProductsSlieder();
  }

  private markActivePaymentMethod() {
    $("body").on("updated_checkout", function () {
      var $input = $(".wc_payment_method input"); // caching our input
      $input.filter(":checked").parent().addClass("is-checked"); // filtering out the checked one and adding .is-active to the parten li on inital page load/refresh

      $input.on("change", function () {
        // removing and adding our .is-active whnever there is a change
        $input.parent().removeClass("is-checked");
        $(this).parent().addClass("is-checked");
      });
    });
  }

  private updateCheckoutSteps() {
    const activeStep = $(".woocommerce .tabs .tab.active").data("tab");
    if (activeStep) {
      this.setCheckoutStep("", activeStep);
    }
    $(document.body).on("tab.change.before", (_, { oldTab, newTab }) => this.setCheckoutStep(oldTab, newTab));
  }

  private setCheckoutStep(prev: string, next: string) {
    $(".woocommerce .checkout-form-wrp")
      .removeClass("checkout-" + prev)
      .addClass("checkout-" + next);
  }

  private CartCrossSellProductsSlieder() {
    const $container = $("[id^=cart-cross-sell]");
    $container.addClass("keen-slider");
    KeenSliderHelper.registerSlider($container, {
      selector: ".content-product-wrap",
    });
  }
}

new Checkout().runOnReady();
