import { BaseSelfControl } from './BaseSelfControl';
import { EnumValidateState } from '../enums';
import { ValidationChain, ValidationEventArgs } from '../chainOfResponsibility';

export class SelfList<VALUE, JSON = void> extends BaseSelfControl<
  VALUE[],
  JSON[],
  string,
  HTMLDivElement
> {
  public onlyLastLevel: boolean = false;
  private validateNormal(eventArgs: ValidationEventArgs) { }
  private validateRequired = (eventArgs: ValidationEventArgs) => {
    if (this.required) {
      if (!this.value || this.value.length === 0) {
        eventArgs.cancel = true;
        eventArgs.state = EnumValidateState.empty;
        eventArgs.error = 'هیچ کدی انتخاب نشده است';
      }
    }
  };

  isValueEmpty = () => {
    return this.#value instanceof Array && this.#value.length > 0;
  }
  isValueNotEmpty = () => {
    return !this.isValueEmpty();
  }

  public validate = () => {
    var validationChain = new ValidationChain();

    validationChain.validators.add(this.validateNormal);
    validationChain.validators.add(this.validateRequired);

    this.validation = validationChain.validate();
  };

  public cleaningClassInitializer = () => {
    this.hasChange = false;
    this.defaultValue = undefined;
    this.initializeListener = false;
    this.initializeProperties = false;
    this.validation = true;
  };

  public refreshHasChange = () => {
    if (this.showHasChangeFlag) {
      this.hasChange = this.defaultValue !== this.#value.toString();
    }
  };
  public restartDefaultValue = () => {
    this.defaultValue = this.value.toString();
    this.refreshHasChange();
  };

  #value: VALUE[];
  public get value() {
    return this.#value;
  }
  public set value(value: VALUE[]) { }

  public setValue = (ids: VALUE[]) => {
    this.#value = ids;
    this.refreshHasChange();
  };


  constructor() {
    super();
    this.#value = [];
    this.restartDefaultValue();
  }

  deserialize = (json: JSON[]) => {

  }

  static empty<VALUE>(): SelfList<VALUE> {
    return new SelfList<VALUE>();
  }
}
