import apis from '@/api';
import { valueObj } from './constants/index.js';

const valueFunctions = {
  [valueObj.c]: apis.mdsFindAllCptysi18n(),
  [valueObj.i]: apis.mdsFindAllIssuerCptysi18n(),
  [valueObj.b]: apis.mdsFindAllbonds(),
  [valueObj.o]: apis.mdsFindAllbonds(),
  [valueObj.g]: apis.mdsFindAllGuarantees(),
};

export const queryMdsFindAllCptysi18n = async(val) => {
  console.log('fun', valueFunctions[val]);
  return await valueFunctions[val].then(res => {
    if (res.success) {
      const { cpty } = res.data;
      return cpty.map(item => ({ label: item.localName, value: item.code }));
    }
  });
};

export const queryMdsFindAllIssuerCptysi18n = async() => {
  return await apis.mdsFindAllIssuerCptysi18n().then(res => {
    if (res.success) {
      const { cpty } = res.data;
      return cpty.map(item => ({ label: item.localName, value: item.code }));
    }
  }).catch(() => {
    return [];
  });
};

export const queryMdsFindAllbonds = async() => {
  return await apis.mdsFindAllbonds().then(res => {
    if (res.success) {
      const { cpty } = res.data;
      return cpty.map(item => ({ label: item.localName, value: item.code }));
    }
  }).catch(() => {
    return [];
  });
};

export const queryMdsFindAllCountries = async() => {
  return await apis.mdsFindAllCountries().then(res => {
    if (res.success) {
      const { cpty } = res.data;
      return cpty.map(item => ({ label: item.localName, value: item.code }));
    }
  }).catch(() => {
    return [];
  });
};

export const queryMdsFindAllGuarantees = async() => {
  return await apis.mdsFindAllGuarantees().then(res => {
    if (res.success) {
      const { cpty } = res.data;
      return cpty.map(item => ({ label: item.localName, value: item.code }));
    }
  }).catch(() => {
    return [];
  });
};
