import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { writeFileSync } from '../fs/fs.mjs';
import { getFs } from '../nodejs/fs.mjs';
import { uploadCOSFile } from '../tencent-cloud/cos/cos.mjs';
import { getSavePath, getSaveFileName } from './helper/helper.mjs';
import '../nodejs/crypto.mjs';
import '../nodejs/os.mjs';
import '../tencent-cloud/cos/helper.mjs';
import '../bite/format-bite.mjs';
import '../third-party/cos-nodejs-sdk-v5.mjs';
import '../string/string.mjs';
import '../fs/fs-util.mjs';
import '@babel/runtime/helpers/toConsumableArray';
import '../nodejs/path.mjs';
import '../time/time.mjs';
import '../tencent-cloud/cos/link.mjs';
import './helper/value-type.mjs';

function pushCOSFiles(list) {
  var files = list.map(function (item) {
    return {
      key: item.cosKey,
      path: item.savePath
    };
  });
  return files;
}
function getCOSKeyAndSavePath(_ref) {
  var configList = _ref.configList,
    secretInfo = _ref.secretInfo,
    appName = _ref.appName,
    cosDir = _ref.cosDir;
  var _ref2 = secretInfo || {},
    groupName = _ref2.groupName,
    envName = _ref2.envName;
  return configList.map(function (config) {
    var key = config.key,
      valueType = config.valueType;
    var savePath = getSavePath({
      appName: appName,
      groupName: groupName,
      envName: envName,
      key: key,
      valueType: valueType
    });
    var saveFileName = getSaveFileName({
      appName: appName,
      groupName: groupName,
      envName: envName,
      key: key,
      valueType: valueType
    });
    return Object.assign(Object.assign({}, config), {
      cosKey: "".concat(cosDir, "/").concat(saveFileName),
      savePath: savePath
    });
  });
}
function removeTmpFiles(cosKeyList) {
  var fs = getFs();
  cosKeyList.forEach(function (item) {
    fs.unlinkSync(item.savePath);
  });
}
/**
 * 同步 Rainbow 配置到腾讯云 COS
 * 将远程配置列表保存为本地文件，然后上传到 COS，最后清理临时文件
 * @param configList - 远程配置列表
 * @param secretInfo - 密钥信息（包含 groupName、envName 等）
 * @param appName - 应用名称
 * @param cosInfo - COS 配置信息
 * @param cosInfo.secretId - 腾讯云 SecretId
 * @param cosInfo.secretKey - 腾讯云 SecretKey
 * @param cosInfo.bucket - COS 存储桶名称
 * @param cosInfo.region - COS 区域
 * @param cosInfo.dir - COS 目录路径
 * @returns {Promise<void>}
 * @example
 * ```ts
 * syncRainbowToCOS({
 *   configList: [
 *     { key: 'config1', value: 'value1', valueType: 'string' }
 *   ],
 *   secretInfo: {
 *     groupName: 'my-group',
 *     envName: 'production'
 *   },
 *   appName: 'my-app',
 *   cosInfo: {
 *     secretId: 'xxx',
 *     secretKey: 'xxx',
 *     bucket: 'my-bucket',
 *     region: 'ap-guangzhou',
 *     dir: 'configs'
 *   }
 * }).then(() => {
 *   console.log('同步完成');
 * });
 * ```
 */
function syncRainbowToCOS(_ref3) {
  var configList = _ref3.configList,
    secretInfo = _ref3.secretInfo,
    appName = _ref3.appName,
    cosInfo = _ref3.cosInfo;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var cosKeyList, files;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          cosKeyList = getCOSKeyAndSavePath({
            configList: configList,
            secretInfo: secretInfo,
            appName: appName,
            cosDir: cosInfo.dir
          });
          cosKeyList.forEach(function (item) {
            writeFileSync(item.savePath, item.value);
          });
          files = pushCOSFiles(cosKeyList);
          _context.next = 1;
          return uploadCOSFile(Object.assign({
            files: files
          }, cosInfo));
        case 1:
          _context.next = 2;
          return removeTmpFiles(cosKeyList);
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

export { syncRainbowToCOS };
