import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../../tslib.es6-848120af.js';
import { deleteCOSMultipleObject, getCOSBucketList } from './cos.mjs';
import './helper.mjs';
import '../../bite/format-bite.mjs';
import '../../third-party/cos-nodejs-sdk-v5.mjs';

function deleteCOSLongAgoObject(_ref) {
  var secretId = _ref.secretId,
    secretKey = _ref.secretKey,
    bucket = _ref.bucket,
    region = _ref.region,
    prefix = _ref.prefix,
    _ref$keepNumber = _ref.keepNumber,
    keepNumber = _ref$keepNumber === void 0 ? 1 : _ref$keepNumber;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var list, reg, toDeleteKeys;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return getCOSBucketList({
            secretId: secretId,
            secretKey: secretKey,
            bucket: bucket,
            region: region,
            prefix: prefix
          });
        case 1:
          list = _context.sent;
          reg = new RegExp("^".concat(prefix, "[^/]*$")); // 排除文件夹、子文件夹中内容
          list = list.filter(function (item) {
            return !item.Key.endsWith('/') && reg.test(item.Key);
          });
          list.sort(function (a, b) {
            return new Date(b.LastModified).getTime() - new Date(a.LastModified).getTime();
          });
          toDeleteKeys = list.slice(keepNumber).reduce(function (acc, item) {
            acc.push(item.Key);
            return acc;
          }, []);
          console.log('[deleteCOSLongAgoObject] toDeleteKeys: ', toDeleteKeys);
          if (toDeleteKeys === null || toDeleteKeys === void 0 ? void 0 : toDeleteKeys.length) {
            _context.next = 2;
            break;
          }
          return _context.abrupt("return");
        case 2:
          _context.next = 3;
          return deleteCOSMultipleObject({
            secretId: secretId,
            secretKey: secretKey,
            bucket: bucket,
            region: region,
            keys: toDeleteKeys
          });
        case 3:
          return _context.abrupt("return", _context.sent);
        case 4:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
function deleteCOSEmptyFolder(_ref2) {
  var secretId = _ref2.secretId,
    secretKey = _ref2.secretKey,
    bucket = _ref2.bucket,
    region = _ref2.region,
    prefix = _ref2.prefix;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var list, keys, emptyKeys;
    return _regeneratorRuntime.wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 1;
          return getCOSBucketList({
            secretId: secretId,
            secretKey: secretKey,
            bucket: bucket,
            region: region,
            prefix: prefix
          });
        case 1:
          list = _context2.sent;
          keys = list.map(function (item) {
            return item.Key;
          });
          emptyKeys = keys.filter(function (item) {
            return item.endsWith('/') && !keys.find(function (key) {
              return new RegExp("^".concat(item, ".+")).test(key);
            });
          });
          console.log('[deleteCOSEmptyFolder] emptyKeys: ', emptyKeys);
          _context2.next = 2;
          return deleteCOSMultipleObject({
            secretId: secretId,
            secretKey: secretKey,
            bucket: bucket,
            region: region,
            keys: emptyKeys
          });
        case 2:
          return _context2.abrupt("return", _context2.sent);
        case 3:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}

export { deleteCOSEmptyFolder, deleteCOSLongAgoObject };
