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

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

  const requestUrl = getAjaxActionUrl('load_'+listName);
  const ajax = axios.post(requestUrl);
  if (callback) {
    ajax.then(response => callback(response)).catch(err => {
      console.error('Dreamfox WDD: AJAX error loading ' + listName, err);
    });
  }
}
export default apiLoadList;


export const apiSaveSettings = (data, callback, section) => {
  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)).catch(err => {
      console.error('Dreamfox WDD: AJAX error saving ' + section + ' settings', err);
    });
  }
}

export const apiLoadSettings = (callback, section) => {
  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)).catch(err => {
      console.error('Dreamfox WDD: AJAX error loading ' + section + ' settings', err);
    });
  }
}