import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
  connect() {
    let usersTable = document.getElementById("users-table");
    if (!usersTable) return;
    let toggleAllCheckbox = usersTable.querySelector(
      "thead input[type='checkbox']",
    );
    let checkboxes = [
      ...usersTable.querySelectorAll("tbody input[type='checkbox']"),
    ];

    toggleAllCheckbox.addEventListener("change", (event) => {
      checkboxes.forEach((checkbox) => {
        checkbox.checked = event.target.checked;
      });
    });
    checkboxes.forEach((checkbox) => {
      checkbox.addEventListener("change", () => {
        let allChecked = checkboxes.every((checkbox) => checkbox.checked);
        let someChecked = checkboxes.some((checkbox) => checkbox.checked);
        toggleAllCheckbox.checked = someChecked;
        toggleAllCheckbox.indeterminate = someChecked && !allChecked;
      });
    });
  }
}
