/**
 * 2015-2024 Chronopost SAS
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License 3.0 (AFL-3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to licence@chronopost.fr so we can send you a copy immediately.
 *
 * @author    Chronopost SAS <contact@chronopost.fr>
 * @copyright 2015-2024 Chronopost SAS
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 */
function chrAdminAjaxRequest(action, data, type = 'POST') {
  return new Promise((resolve, reject) => {
    $.ajax({
      type: type,
      cache: false,
      url: Chronopost.admin_ajax_endpoint,
      data: {
        ajax: true,
        controller: 'AdminChronopostAjax',
        action: action,
        ajaxData: data,
        token: Chronopost.csrf_token,
      },
      success: function (r) {
        resolve(r);
      },
      error: function (r) {
        reject(r);
      }
    });
  });
}

function chrAjaxRequest(data, type = 'POST') {
  return new Promise((resolve, reject) => {
    $.ajax({
      type: type,
      url: Chronopost.ajax_endpoint,
      data: data,
      success: function (r) {
        resolve(r);
      },
      error: function (r) {
        reject(r);
      }
    });
  });
}
