//@ts-nocheck
import { observable, action, toJS } from 'mobx';
import { Api } from '../services/api';
import { Alert } from 'react-native';
import { myLog } from '../utils/log';

export enum Types {
  Province,
  District,
  Ward,
  Reason,
  Job,
  Bank,
}

class SelectorStore {
  @observable isLoading = false;
  @observable isError = false;
  @observable data = [];

  @action
  async getData(params, type: Types, onSuccess?, onFailure?) {
    this.isLoading = true;
    this.data = [];
    let response = {};
    switch (type) {
      case Types.Province:
        response = await Api.getInstance().getProvinces(params);
        break;
      case Types.District:
        response = await Api.getInstance().getDistricts(params);

        break;
      case Types.Ward:
        response = await Api.getInstance().getWards(params);

        break;
      case Types.Reason:
        response = await Api.getInstance().loanPurpose(params);

        break;
      case Types.Job:
        response = await Api.getInstance().getJobs(params);

        break;
      case Types.Bank:
        response = await Api.getInstance().getBanks(params);

        break;
    }
    // myLog(response)
    if (response.kind === 'ok') {
      this.isLoading = false;
      this.isError = false;
      switch (type) {
        case Types.Reason:
          this.data = response.data.data.map((item) => ({
            ...item,
            ...{ name: item.text },
          }));
          break;
        default:
          this.data = response.data.data;
          break;
      }
      myLog(response.data.data);
      if (onSuccess) {
        onSuccess(response.data.data);
      }
    } else {
      this.isLoading = false;
      this.isError = false;
      Alert.alert('Lỗi', response.Messages);
    }
  }
}

export default SelectorStore;
