import { BaseSelfControl } from './BaseSelfControl';
import { EnumValidateState } from '../enums';
import { ValidationChain, ValidationEventArgs } from '../chainOfResponsibility';

export class SelfListItem extends BaseSelfControl<
  string,
  string,
  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.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: string;
  public get value() {
    return this.#value;
  }
  public set value(value: string) { }

  public setValue = (ids: string) => {
    this.#value = ids;
    this.refreshHasChange();
  };


  constructor(value: string) {
    super();
    this.#value = value;
    this.restartDefaultValue();
  }

  deserialize = (json: string) => {

  }

  static empty(value?: string): SelfListItem {
    return new SelfListItem(value || '');
  }
}
