import { action, computed, observable } from 'mobx';
export abstract class Entity {
  @observable private readonly _id: number | string | undefined;

  protected constructor(id: string) {
    this._id = id;
  }

  get id(): number | string | undefined {
    return this._id;
  }
}

export abstract class EntityStore<TEntity extends Entity> {
  entities = observable.map<string | number, TEntity>();

  @observable loading = false;

  @observable loadingCard = false;

  @observable total = 0;

  @observable totalGroups = 0;

  @computed
  get list(): any {
    return [...this.entities.values()];
  }

  @action record(id: string | number): any {
    return this.entities.get(id);
  }

  @action delListRecord(id: any): void {
    this.entities.delete(id);
  }

  @action addListRecord(data: any): void {
    this.entities.set(`${data.id}`, data);
  }

  @action editListRecord(data: any): void {
    const rec = this.record(data.id);
    Object.assign(rec, data);
    this.entities.set(rec.id, rec);
  }

  @action
  protected setTotal(value: number): void {
    this.total = value;
  }

  @action
  protected setTotalGroups(value: number): void {
    this.totalGroups = value;
  }

  @action
  setLoading(value: boolean): void {
    this.loading = value;
  }

  @action
  setLoadingCard(value: boolean): void {
    this.loadingCard = value;
  }

  get getLoading(): boolean {
    return this.loading;
  }

  get getLoadingCard(): boolean {
    return this.loadingCard;
  }
}
