import { Controller } from "stimulus";
export default class extends Controller {
  static targets = ["input"];

  connect() {
    this.inputTargets.forEach((i) => {
      if (i.checked) {
        this.doSelectPaymentMethod(i.dataset.paymentmethodId);
      }
    });
  }

  selectPaymentMethod(event) {
    this.doSelectPaymentMethod(event.target.dataset.paymentmethodId);

    const stripeCardSelector =
      this.application.getControllerForElementAndIdentifier(
        document
          .getElementById(event.target.dataset.paymentmethodId)
          .querySelector('[data-controller="stripe-cards"]'),
        "stripe-cards"
      );
    stripeCardSelector?.initSelectedCard();
  }

  doSelectPaymentMethod(paymentMethodContainerId) {
    Array.from(
      document.getElementsByClassName("paymentmethod-container")
    ).forEach((e) => {
      if (e.id === paymentMethodContainerId) {
        e.style.display = "block";
        this.removeDisabledAttributeOnInput(e);
      } else {
        e.style.display = "none";
        this.addDisabledAttributeOnInput(e);
      }
    });
  }

  getFormElementsArray(container) {
    return Array.from(container.querySelectorAll("input, select, textarea"));
  }

  addDisabledAttributeOnInput(container) {
    this.getFormElementsArray(container).forEach((i) => {
      i.disabled = true;
    });
  }

  removeDisabledAttributeOnInput(container) {
    this.getFormElementsArray(container).forEach((i) => {
      i.disabled = false;
    });
  }
}

@@ -143,32 +143,36 @@
 rgets.forEach((i
+nput
 ) =%3E %7B%0A      if 
@@ -173,16 +173,20 @@
    if (i
+nput
 .checked
@@ -194,39 +194,34 @@
  %7B%0A        this.
-doSelec
+se
 tPaymentMethod(i
@@ -220,16 +220,20 @@
 Method(i
+nput
 .dataset
@@ -312,23 +312,18 @@
    this.
-doSelec
+se
 tPayment
@@ -691,15 +691,10 @@
 %0A%0A  
-doSelec
+se
 tPay
@@ -828,17 +828,25 @@
 orEach((
-e
+container
 ) =%3E %7B%0A 
@@ -854,13 +854,33 @@
     
-if (e
+const enabled = container
 .id 
@@ -899,32 +899,51 @@
 ethodContainerId
+%0A%0A      if (enabled
 ) %7B%0A        e.st
@@ -930,33 +930,41 @@
 bled) %7B%0A        
-e
+container
 .style.display =
@@ -990,40 +990,46 @@
 his.
-removeDisabledAttributeOnInput(e
+toggleFieldsEnabled(container, enabled
 );%0A 
@@ -1050,17 +1050,25 @@
         
-e
+container
 .style.d
@@ -1101,219 +1101,110 @@
 his.
-addDisabledAttributeOnInput(e);%0A      %7D%0A    %7D);%0A  %7D%0A%0A  getFormElementsArray(container) %7B%0A    return Array.from(container.querySelectorAll(%22input, select, textarea%22));%0A  %7D%0A%0A  addDisabledAttributeOnInput(container
+toggleFieldsEnabled(container, enabled);%0A      %7D%0A    %7D);%0A  %7D%0A%0A  toggleFieldsEnabled(container, enabled
 ) %7B%0A
@@ -1204,35 +1204,35 @@
 led) %7B%0A    this.
-get
+sub
 FormElementsArra
@@ -1219,37 +1219,32 @@
 .subFormElements
-Array
 (container).forE
@@ -1240,33 +1240,37 @@
 ainer).forEach((
-i
+field
 ) =%3E %7B%0A      i.d
@@ -1258,33 +1258,37 @@
 eld) =%3E %7B%0A      
-i
+field
 .disabled = true
@@ -1287,12 +1287,16 @@
 d = 
-true
+!enabled
 ;%0A  
@@ -1312,141 +1312,112 @@
 %0A%0A  
-removeDisabledAttributeOnInput(container) %7B%0A    this.getFormElementsArray(container).forEach((i) =%3E %7B%0A      i.disabled = false;%0A    %7D
+subFormElements(container) %7B%0A    return Array.from(container.querySelectorAll(%22input, select, textarea%22)
 );%0A 

Simplify payment method controller