'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var base64ToBlob = _interopDefault(require('./base64ToBlob'));
var consts = require('./consts');

/**
 * 下载文件到本地，并指定文件名
 * @param {File|Blob|string} source Base64或dataURI数据
 * @param {*} fileName 下载文件名
 */
function downloadFile(source, fileName) {
  if (consts.RE_BASE64.test(source)) {
    source = base64ToBlob(source);
  }

  if (source instanceof Blob) {
    var aLink = document.createElement('a');
    var event = document.createEvent('HTMLEvents');
    event.initEvent('click', true, true);
    aLink.download = fileName;
    aLink.href = URL.createObjectURL(source);
    aLink.click();
  }
}

module.exports = downloadFile;
