'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);

function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
 * 从 Blob 对象下载文件。创建一个临时链接并触发浏览器下载
 * @param blob - Blob 数据
 * @param fileName - 下载的文件名
 * @example
 * ```ts
 * const blob = new Blob(['Hello World'], { type: 'text/plain' });
 * downloadFileFromBlob({
 *   blob,
 *   fileName: 'hello.txt'
 * });
 * ```
 */
function downloadFileFromBlob(_ref) {
  var blob = _ref.blob,
    fileName = _ref.fileName;
  var url = window.URL.createObjectURL(new Blob([blob]));
  var link = document.createElement('a');
  link.href = url;
  link.download = fileName;
  link.style.visibility = 'hidden';
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}
/**
 * 批量下载文件为 ZIP 压缩包。将多个文件打包成 ZIP 并触发下载
 * @param fileList - 文件列表，每个文件包含 content 和 name
 * @param zipName - ZIP 文件名
 * @param saveAs - FileSaver.js 的 saveAs 函数
 * @param JSZip - JSZip 库实例
 * @returns Promise\<boolean\> - 成功返回 true，失败返回 false
 * @example
 * ```ts
 * import JSZip from 'jszip';
 * import { saveAs } from 'file-saver';
 *
 * downloadFilesToZip({
 *   fileList: [
 *     { content: 'file1 content', name: 'file1.txt' },
 *     { content: 'file2 content', name: 'file2.txt' }
 *   ],
 *   zipName: 'files.zip',
 *   saveAs,
 *   JSZip
 * }).then(success => {
 *   console.log('下载结果:', success);
 * });
 * ```
 */
function downloadFilesToZip(_ref2) {
  var fileList = _ref2.fileList,
    zipName = _ref2.zipName,
    saveAs = _ref2.saveAs,
    JSZip = _ref2.JSZip;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var zip, _iterator, _step, file, content, _t;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.prev = 0;
          zip = new JSZip(); // 异步下载每个文件并添加到zip中
          _iterator = _createForOfIteratorHelper(fileList);
          try {
            for (_iterator.s(); !(_step = _iterator.n()).done;) {
              file = _step.value;
              zip.file("".concat(file.name), file.content);
            }
            // 生成ZIP文件并触发下载
          } catch (err) {
            _iterator.e(err);
          } finally {
            _iterator.f();
          }
          _context.next = 1;
          return zip.generateAsync({
            type: 'blob'
          });
        case 1:
          content = _context.sent;
          saveAs(content, zipName);
          return _context.abrupt("return", true);
        case 2:
          _context.prev = 2;
          _t = _context["catch"](0);
          console.error('批量下载失败:', _t);
          return _context.abrupt("return", false);
        case 3:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[0, 2]]);
  }));
}

exports.downloadFileFromBlob = downloadFileFromBlob;
exports.downloadFilesToZip = downloadFilesToZip;
