'use strict';

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

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../../tslib.es6-0d92ef81.js');
var tencentCloud_cos_helper = require('./helper.js');
require('../../bite/format-bite.js');
require('../../third-party/cos-nodejs-sdk-v5.js');

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

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

/**
 * 解除腾讯云 COS 图片封禁
 * - 当图片因内容审核被封禁后，可调用此方法解除封禁
 * - 底层通过 putObjectCopy 设置 `x-cos-forbid-state: 0` 来解除封禁状态
 *
 * @param {object} config 配置信息
 * @param {string} config.secretId 腾讯云 SecretId
 * @param {string} config.secretKey 腾讯云 SecretKey
 * @param {string} config.bucket COS 存储桶名称（如 'my-bucket-1250000000'）
 * @param {string} config.region COS 存储桶所在区域（如 'ap-guangzhou'）
 * @param {string} config.key 被封禁的对象键（Object Key），即图片在存储桶中的路径
 * @returns {Promise<object>} 解封结果
 * @throws 当参数不全时会抛出错误
 * @example
 * ```ts
 * // 解除单张图片的封禁
 * await unfreezeCosImage({
 *   secretId: 'your-secret-id',
 *   secretKey: 'your-secret-key',
 *   bucket: 'my-bucket-1250000000',
 *   region: 'ap-guangzhou',
 *   key: 'images/photo.jpg',
 * });
 * ```
 */
function unfreezeCosImage(_ref) {
  var secretId = _ref.secretId,
    secretKey = _ref.secretKey,
    bucket = _ref.bucket,
    region = _ref.region,
    key = _ref.key;
  return new Promise(function (resolve, reject) {
    if (!secretId || !secretKey || !bucket || !region || !key) {
      reject(new Error('[unfreezeCosImage] 参数不全'));
      return;
    }
    var cos = tencentCloud_cos_helper.getCOSInstance(secretId, secretKey);
    cos.putObjectCopy({
      Bucket: bucket,
      Region: region,
      Key: key,
      CopySource: "".concat(bucket, ".cos.").concat(region, ".myqcloud.com/").concat(key),
      MetadataDirective: 'Replaced',
      Headers: {
        'x-cos-forbid-state': '0'
      }
    }, function (err, data) {
      if (err) {
        reject(err);
        return;
      }
      resolve(data);
    });
  });
}
/**
 * 批量解除腾讯云 COS 图片封禁
 * - 对多张被封禁的图片并行执行解封操作
 * - 底层通过 Promise.allSettled 并发调用 unfreezeCosImage
 *
 * @param {object} config 配置信息
 * @param {string} config.secretId 腾讯云 SecretId
 * @param {string} config.secretKey 腾讯云 SecretKey
 * @param {string} config.bucket COS 存储桶名称（如 'my-bucket-1250000000'）
 * @param {string} config.region COS 存储桶所在区域（如 'ap-guangzhou'）
 * @param {Array<string>} config.keys 被封禁的对象键列表
 * @returns {Promise<Array<object>>} 每张图片的解封结果数组，包含 key、success、data/error 字段
 * @example
 * ```ts
 * const results = await batchUnfreezeCosImages({
 *   secretId: 'your-secret-id',
 *   secretKey: 'your-secret-key',
 *   bucket: 'my-bucket-1250000000',
 *   region: 'ap-guangzhou',
 *   keys: ['images/photo1.jpg', 'images/photo2.jpg'],
 * });
 *
 * results.forEach(r => {
 *   if (r.success) {
 *     console.log(`${r.key} 解封成功`);
 *   } else {
 *     console.error(`${r.key} 解封失败:`, r.error);
 *   }
 * });
 * ```
 */
function batchUnfreezeCosImages(_ref2) {
  var secretId = _ref2.secretId,
    secretKey = _ref2.secretKey,
    bucket = _ref2.bucket,
    region = _ref2.region,
    keys = _ref2.keys;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var results;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return Promise.allSettled(keys.map(function (key) {
            return unfreezeCosImage({
              secretId: secretId,
              secretKey: secretKey,
              bucket: bucket,
              region: region,
              key: key
            });
          }));
        case 1:
          results = _context.sent;
          return _context.abrupt("return", results.map(function (result, index) {
            if (result.status === 'fulfilled') {
              return {
                key: keys[index],
                success: true,
                data: result.value
              };
            }
            return {
              key: keys[index],
              success: false,
              error: result.reason
            };
          }));
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

exports.batchUnfreezeCosImages = batchUnfreezeCosImages;
exports.unfreezeCosImage = unfreezeCosImage;
