import axios from 'axios';
import Configuration from '../Configuration';
import { getAjaxActionUrl } from '../utils';

const apiLoadList = (callback, listName, nonceList = '123456') => {
  if (Configuration.getInstance().is_debug) {
    const config = Configuration.getInstance();
    const argData = {}
    argData[listName] = config[listName];
    callback({data: argData});
    return;
  }

  console.log('API: Load ' + listName);
  const requestUrl = getAjaxActionUrl('load_'+listName);
  const ajax = axios.post(requestUrl, {
    nonce: nonceList,
  })
  if (callback) {
    ajax.then(response => callback(response));
  }
}
export default apiLoadList;


export const apiSaveSettings = (data, callback, section) => {
  console.log('API: save '+section+' settings...')
  if (Configuration.getInstance().is_debug) {
    callback('This is callback from debug')
    return
  }
  const requestUrl = getAjaxActionUrl('save_'+section+'_settings');
  const ajax = axios.post(requestUrl, {
    data: data,
  })
  if (callback) {
    ajax.then(response => callback(response));
  }
}

export const apiLoadSettings = (callback, section) => {
  console.log('API: load settings...')
  if (Configuration.getInstance().is_debug) {
    let config = Configuration.getInstance()
    callback(config[section+'Settings'])
    return
  }
  const requestUrl = getAjaxActionUrl('load_'+section+'_settings');
  const ajax = axios.post(requestUrl, {})
  if (callback) {
    ajax.then(response => callback(response.data));
  }
}