import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { getCrypto } from '../nodejs/crypto.mjs';
import { getFs } from '../nodejs/fs.mjs';
import { getRequest } from '../nodejs/request.mjs';

/**
 * node环境下，保存base64图片到文件
 * @param {object} config 输入配置
 * @param {string} config.imgUrl base64图片Url
 * @param {string} config.savePath 保存路径，最好是绝对路径
 * @returns {Promise<string>} 去掉前缀的base64图片地址
 *
 * @example
 *
 * saveBase64ImgToFile({
 *   imgUrl: 'xx',
 *   savePath: '/test.png'
 * }).then((base64Data) => {
 *   console.log(base64Data)
 * })
 *
 */
function saveBase64ImgToFile(_ref) {
  var imgUrl = _ref.imgUrl,
    savePath = _ref.savePath;
  return new Promise(function (resolve, reject) {
    var fs = getFs();
    var base64Data = imgUrl.replace(/^data:image\/\w+;base64,/, '');
    var dataBuffer = Buffer.from(base64Data, 'base64');
    fs.writeFile(savePath, dataBuffer, function (err) {
      if (err) {
        reject(err);
      } else {
        resolve(base64Data);
      }
    });
  });
}
/**
 * node环境下，本地图片转为base64
 * @param {string} savePath 本地图片保存路径
 * @returns {string} base64图片地址
 *
 * @example
 *
 * const base64str = turnLocalImg2Base64('/temp.png')
 *
 */
function turnLocalImg2Base64(imgPath) {
  var fs = getFs();
  var bitmap = fs.readFileSync(imgPath);
  // base64编码
  var base64str = Buffer.from(bitmap, 'binary').toString('base64');
  return base64str;
}
/**
 * node环境下，保存网络图片到本地
 * @param {object} config 输入配置
 * @param {string} config.imgUrl 网络图片地址
 * @param {string} config.savePath 本地图片保存路径，建议绝对路径
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 *
 * saveRemoteImgToLocal({
 *   imgUrl: 'xx',
 *   savePath: './test.png'
 * }).then(() => {
 *
 * })
 */
function saveRemoteImgToLocal(_ref2) {
  var imgUrl = _ref2.imgUrl,
    savePath = _ref2.savePath;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var fs, request;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          fs = getFs();
          request = getRequest();
          _context.next = 1;
          return request(imgUrl).pipe(fs.createWriteStream(savePath));
        case 1:
          return _context.abrupt("return", _context.sent);
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
/**
 * 获取图片md5
 * @param {object} options 配置信息
 * @param {string} options.savePath 本地图片地址，建议绝对路径
 * @returns {Promise<string>} 图片md5值
 *
 * @example
 * getImgMd5({
 *  savePath: '/test.png'
 * }).then(md5 => {
 *   console.log(md5)
 * })
 * ```
 */
function getImgMd5(_ref3) {
  var savePath = _ref3.savePath;
  var fs = getFs();
  var crypto = getCrypto();
  return new Promise(function (resolve, reject) {
    fs.readFile(savePath, function (err, data) {
      if (err) {
        reject();
      }
      var md5Value = crypto.createHash('md5').update(data, 'utf8').digest('hex');
      resolve(md5Value);
    });
  });
}

export { getImgMd5, saveBase64ImgToFile, saveRemoteImgToLocal, turnLocalImg2Base64 };
