import {
  model,
  Model,
  modelAction,
  ModelAutoTypeCheckingMode,
  setGlobalConfig,
  tProp,
  types,
} from 'mobx-keystone';
import Report from './Report';

setGlobalConfig({
  modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
});

@model('keystone/Reports/ReportsList')
export default class ReportsList extends Model({
  reports: tProp(types.array(types.model<Report>(Report)), () => []),
}) {
  @modelAction
  addReport(report: Report) {
    this.reports.push(report);
  }

  @modelAction
  removeLog(report: any) {
    const index = this.reports.indexOf(report);
    if (index >= 0) {
      this.reports.splice(index, 1);
    }
  }

  @modelAction
  removeReportsByIds(arrayOfIds: any) {
    if (typeof arrayOfIds !== undefined) {
      arrayOfIds.map((id: string) => {
        return this.removeLog(
          this.reports.find((x: { id: string }) => x.id === id)
        );
      });
    }
  }

  get allKeystone() {
    return this.reports;
  }

  get all() {
    const all = [];
    for (let i = 0; i < this.reports.length; i += 1) {
      all.push(this.reports[i]);
    }
    return all;
  }
}
