import { WhiteSpace } from "@/utils/string";

export function numericKeyDownHandler(event: KeyboardEvent) {
  if (this.allowedChars.includes(event.key) || event.ctrlKey || event.metaKey) {
    return true;
  }

  event.preventDefault();
}

export function numericPasteHandler(event: ClipboardEvent) {
  const clipboard = WhiteSpace.remove(
    (event.clipboardData as any).getData("text").replace(/\s/g, "")
  );

  for (let i = 0; i < clipboard.length; i++) {
    if (!this.allowedChars.includes(clipboard[i])) {
      return false;
    }
  }

  return true;
}

export function numericChangeHandler(event: Event) {
  const target = event.target as HTMLInputElement;

  if (WhiteSpace.check(target.value)) {
    target.value = WhiteSpace.remove(target.value);
    target.dispatchEvent(new CustomEvent("input"));
  }
}
