import { purgeUrlCache } from './purge-url-cache.mjs';
import { uploadCOSFile } from '../cos/cos.mjs';
import { createPurgeTask } from '../eo/purge.mjs';
import '@babel/runtime/regenerator';
import '../../tslib.es6-848120af.js';
import '../base/base.mjs';
import 'axios';
import '../../nodejs/crypto.mjs';
import './config.mjs';
import '../cos/helper.mjs';
import '../../bite/format-bite.mjs';
import '../../third-party/cos-nodejs-sdk-v5.mjs';

/**
 * 上传文件到 COS 并刷新 CDN 缓存（一体化操作）
 * 先上传文件到 COS，然后自动刷新对应的 CDN URL 缓存，支持 CDN 和 EdgeOne 两种刷新方式
 * @param {object} config 配置信息
 * @param {string} config.secretId 腾讯云 secretId
 * @param {string} config.secretKey 腾讯云 secretKey
 * @param {string} config.bucket COS bucket
 * @param {string} config.region COS region
 * @param {Array<object>} config.files 文件列表，每项包含 key、path、url
 * @param {string} [config.area='mainland'] CDN 刷新区域
 * @param {boolean} [config.useEO] 是否使用 EdgeOne 刷新（默认使用 CDN）
 * @param {object} [config.eoOptions] EdgeOne 刷新配置
 * @returns {Promise<{uploadResult: any, purgeResult: any, code: 1|2}>} code=2 表示上传和刷新均成功，code=1 表示上传成功但刷新失败
 * @example
 * ```ts
 * const result = await uploadCOSFileAndPurgeUrlCache({
 *   secretId: 'xxx',
 *   secretKey: 'xxx',
 *   bucket: 'my-bucket-1250000000',
 *   region: 'ap-guangzhou',
 *   files: [{
 *     key: 'static/app.js',
 *     path: '/dist/app.js',
 *     url: 'https://cdn.example.com/static/app.js',
 *   }],
 * });
 * ```
 */
function uploadCOSFileAndPurgeUrlCache(_ref) {
  var secretId = _ref.secretId,
    secretKey = _ref.secretKey,
    bucket = _ref.bucket,
    region = _ref.region,
    files = _ref.files,
    area = _ref.area,
    useEO = _ref.useEO,
    eoOptions = _ref.eoOptions;
  return new Promise(function (resolve, reject) {
    uploadCOSFile({
      files: files,
      secretId: secretId,
      secretKey: secretKey,
      bucket: bucket,
      region: region
    }).then(function (uploadRes) {
      var doPurge = useEO ? function () {
        return createPurgeTask({
          secretId: secretId,
          secretKey: secretKey,
          targets: files.map(function (item) {
            return item.url;
          }),
          zoneId: (eoOptions === null || eoOptions === void 0 ? void 0 : eoOptions.zoneId) || '',
          method: eoOptions === null || eoOptions === void 0 ? void 0 : eoOptions.method,
          type: eoOptions === null || eoOptions === void 0 ? void 0 : eoOptions.type
        });
      } : function () {
        return purgeUrlCache({
          secretId: secretId,
          secretKey: secretKey,
          urls: files.map(function (item) {
            return item.url;
          }),
          area: area
        });
      };
      doPurge().then(function (purgeRes) {
        resolve({
          uploadResult: (uploadRes === null || uploadRes === void 0 ? void 0 : uploadRes.data) || uploadRes,
          purgeResult: (purgeRes === null || purgeRes === void 0 ? void 0 : purgeRes.data) || purgeRes,
          code: 2
        });
      })["catch"](function (err) {
        console.warn('[purgeUrlCache] err: ', err);
        resolve({
          uploadResult: (uploadRes === null || uploadRes === void 0 ? void 0 : uploadRes.data) || uploadRes,
          purgeResult: err,
          code: 1
        });
      });
    })["catch"](function (err) {
      console.warn('[uploadCOSFile] err: ', err);
      reject(err);
    });
  });
}

export { uploadCOSFileAndPurgeUrlCache };
