import { Component, Inject, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';

@Component({
    selector: 'app-specific-value',
    templateUrl: './specific-value.component.html',
    styleUrl: './specific-value.component.css',
    standalone: false
})
export class SpecificValueComponent implements OnInit {
  valueInputControl = new FormControl('');

  constructor(@Inject(MAT_DIALOG_DATA) private _data: any) { }

  ngOnInit(): void {
    if(this._data != undefined && this._data.value != undefined){
      let valueNum = parseInt(this._data.value);
      if(!isNaN(valueNum)){
        this.valueInputControl.setValue(`${valueNum}`);
      }
    }
  }

  validateNumber(value: string){
    let validChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    let valueArr = value.split('');
    let validVal = ''

    valueArr.forEach(char => {
      if(validChars.includes(char)){
        validVal += char;
      }
    });

    this.valueInputControl.setValue(validVal);
  }
}
