import {BasePageData} from './BasePageData';

class TouchedControlClass {
  constructor(
    public propertyName: string,
    public disabled: boolean,
    public touched: boolean
  ) {}
}

export class TouchingControl {
  public controls: TouchedControlClass[] = [];

  constructor(public pageData: BasePageData) {}

  addControl(propName: string) {
    const control = this.controls.find((i) => i.propertyName === propName);
    if (control) {
      control.disabled = false;
      return;
    }

    const newData = new TouchedControlClass(propName, false, false);
    this.controls.push(newData);
  }
  disabledControl = <S>(propertyName: keyof S) => {
    const control = this.controls.find((i) => i.propertyName === propertyName);
    if (control) {
      control.disabled = true;
    }
  };
  removeAllControls = () => {
    this.controls = [];
  };

  setAllTouched() {
    this.controls.forEach((i) => (i.touched = true));
  }

  setTouched<S>(propertyName: keyof S) {
    const control = this.controls.find((i) => i.propertyName === propertyName);
    if (control) {
      control.touched = true;
    }
  }
  clearTouched<S>(propertyName: keyof S) {
    const control = this.controls.find((i) => i.propertyName === propertyName);
    if (control) {
      control.touched = false;
    }
  }
  resetAllTouched() {
    this.controls.forEach((i) => (i.touched = false));
  }

  isTouched<S>(propertyName: keyof S) {
    const control = this.controls.find((i) => i.propertyName === propertyName);
    if (control) {
      return control.touched;
    }
    return false;
  }
}
