import { action, makeAutoObservable, observable, toJS } from 'mobx';
import { RootStore } from 'stores/RootStore';
import { TStorageRecord, TStoreType } from './types';
import { TUseColumns } from '../../types/default';

export class StorageUiStore {
  constructor(private readonly rootStore: RootStore) {
    makeAutoObservable(this);
  }

  @observable checkedColumns: any = [];

  get storageList(): TStoreType[] {
    return this.rootStore.storageStore.list.map((x: any) => {
      return {
        ...x._listRecord,
      };
    });
  }

  get storageRecord(): TStorageRecord | undefined {
    return toJS(this.rootStore.storageStore.storageRecord);
  }

  get countStorage(): number {
    return this.rootStore.storageStore.total;
  }

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

  loadStorageList = (page: number, pageSize: number, sort: string): void => {
    this.rootStore.storageStore.fetchStorageList(page, pageSize, sort);
  };

  loadStorageRecord = (id: number): void => {
    this.rootStore.storageStore.fetchStorageRecord(id);
  };

  downloadStorageItem = async (id: number[]): Promise<any> => {
    await this.rootStore.storageStore.downloadStorageItem(id);
  };

  deleteStorageItem = async (id: number[]): Promise<void> => {
    await this.rootStore.storageStore.deleteStorageItem(id);
  };

  get getCheckedColumns(): TUseColumns {
    return toJS(this.checkedColumns);
  }

  @action setCheckedColumns = (value: any): any =>
    (this.checkedColumns = value);
}
