'use strict';

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

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var fs_fs = require('../fs/fs.js');
var nodejs_fs = require('../nodejs/fs.js');
var rainbowToCos_helper_helper = require('./helper/helper.js');
var tencentCloud_cos_cos = require('../tencent-cloud/cos/cos.js');
require('../nodejs/crypto.js');
require('../nodejs/os.js');
require('../nodejs/path.js');
require('../tencent-cloud/cos/link.js');
require('./helper/value-type.js');
require('../string/string.js');
require('../fs/fs-util.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../time/time.js');
require('../tencent-cloud/cos/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);

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 = rainbowToCos_helper_helper.getSavePath({
      appName: appName,
      groupName: groupName,
      envName: envName,
      key: key,
      valueType: valueType
    });
    var saveFileName = rainbowToCos_helper_helper.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 = nodejs_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 tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var cosKeyList, files;
    return _regeneratorRuntime__default["default"].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) {
            fs_fs.writeFileSync(item.savePath, item.value);
          });
          files = pushCOSFiles(cosKeyList);
          _context.next = 1;
          return tencentCloud_cos_cos.uploadCOSFile(Object.assign({
            files: files
          }, cosInfo));
        case 1:
          _context.next = 2;
          return removeTmpFiles(cosKeyList);
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

exports.syncRainbowToCOS = syncRainbowToCOS;
