import JSZip from 'jszip';
import { saveAs } from 'file-saver';
import ImageBase64 from 'fetch-base64-in-browser';

/**
 *
 * @param {Array} listFile
 * @param {string} fileName
 * @returns {Promise}
 */
export const zipFileBase64 = async (listFile = [{ fileName: '', base64: '' }], fileName = '') => {
  const zipFile = new JSZip();

  listFile.forEach(({ fileName, base64 }) => {
    zipFile.file(fileName, base64, {
      base64: true,
    });
  });

  const content = await zipFile.generateAsync({
    type: 'blob',
  });

  return saveAs(content, fileName);
};

export const getBase64Url = async (url) => {
  const b64 = new ImageBase64(url);
  const str = await b64.fetchAsData();
  return str.replace(/^(.*?)base64,/, '');
};

export { saveAs };
