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

class CounterComponent extends InobyModule {
  public run() {
    const $counters = $("section.component-counter .counter");
    $counters.each((_, counter) => {
      const $number = $(counter).find(".number");
      const targetNumber = parseInt($number.data("target") || "0", 10);
      let currentNumber = 0;
      const increment = targetNumber / (2000 / 50);

      const updateCounter = () => {
        currentNumber += increment;
        if (currentNumber >= targetNumber) {
          $number.text(targetNumber.toString());
        } else {
          $number.text(Math.floor(currentNumber).toString());
          setTimeout(updateCounter, 50);
        }
      };

      updateCounter();
    });
  }
}

new CounterComponent().runOnReady();
