import QRCode from 'qrcode';
import request from "../../../../core/utils/request";
const base64 = 'data:image/svg+xml;base64,'; // eslint-disable-next-line import/prefer-default-export

export const valueToBase64 = value => {
  const qrcodeConfig = {
    type: 'svg'
  };
  return new Promise((resolve, reject) => {
    QRCode.toString(value, qrcodeConfig, (err, url) => {
      if (err) return reject(err);
      resolve(base64 + Buffer(url).toString('base64'));
    });
  });
};
export const getQrcodeUrl = (value = '') => {
  return new Promise(resolve => {
    if (value.includes('http')) return resolve(value);
    if (value.includes('data:image')) return resolve(value);
    valueToBase64(value).then(resolve);
  });
};
export const Ajax = (apiUrl = '', data = {}) => {
  const opts = {
    url: apiUrl,
    method: 'post',
    data
  };
  return new Promise(async (resolve, reject) => {
    const [error, res] = await request(opts);
    error ? reject(error) : resolve(res);
  });
};