import { saveJsonToLog } from '../fs/fs-util.mjs';
import { queryGroupInfo } from '../rainbow/rainbow-admin.mjs';
import { fetchRainbowConfig } from '../rainbow/rainbow-user.mjs';
import { getSaveFileName, readCOSConfig } from './helper/helper.mjs';
import '@babel/runtime/helpers/toConsumableArray';
import '../fs/fs.mjs';
import '../nodejs/crypto.mjs';
import '../nodejs/fs.mjs';
import '../nodejs/os.mjs';
import '../nodejs/path.mjs';
import '../time/time.mjs';
import '@babel/runtime/regenerator';
import '../tslib.es6-848120af.js';
import '../rainbow/helper/helper.mjs';
import '../rainbow/helper/rainbow-base-request.mjs';
import 'axios';
import '../rainbow/helper/rainbow-signature.mjs';
import '../list/flat.mjs';
import '@babel/runtime/helpers/typeof';
import '../rainbow/sdk.mjs';
import '../json/json-parse.mjs';
import '../string/string.mjs';
import '../tencent-cloud/cos/link.mjs';
import './helper/value-type.mjs';

function fetchLatestRainbowData(_ref) {
  var secretInfo = _ref.secretInfo,
    appName = _ref.appName;
  return new Promise(function (resolve) {
    var _ref2 = secretInfo || {},
      groupName = _ref2.groupName,
      envName = _ref2.envName;
    var saveFileName = getSaveFileName({
      appName: appName,
      groupName: groupName,
      envName: envName,
      key: 'all_data',
      valueType: 4
    });
    var originConfig = readCOSConfig(saveFileName);
    queryGroupInfo({
      secretInfo: secretInfo
    }).then(function () {
      var res = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      var configList = res.map(function (config) {
        return {
          key: config.key,
          value: config.value,
          valueType: config.value_type
        };
      });
      var equal = JSON.stringify(configList, null, 0) === JSON.stringify(originConfig, null, 0);
      console.log("[fetchLatestRainbowData] \u4E03\u5F69\u77F3\u914D\u7F6E\u662F\u5426\u6709\u66F4\u65B0 ".concat(appName, "\xB7").concat(groupName, "\xB7").concat(envName, ": ").concat(!equal));
      saveJsonToLog(configList, saveFileName);
      resolve({
        config: configList,
        originConfig: originConfig,
        equal: equal
      });
    });
  });
}
function fetchLatestOneRainbowData(_ref3) {
  var secretInfo = _ref3.secretInfo,
    appName = _ref3.appName,
    key = _ref3.key,
    _ref3$valueType = _ref3.valueType,
    valueType = _ref3$valueType === void 0 ? 4 : _ref3$valueType,
    fetchRainbowConfigOptions = _ref3.fetchRainbowConfigOptions;
  return new Promise(function (resolve, reject) {
    var _ref4 = secretInfo || {},
      groupName = _ref4.groupName,
      envName = _ref4.envName;
    var saveFileName = getSaveFileName({
      appName: appName,
      groupName: groupName,
      envName: envName,
      key: key,
      valueType: valueType
    });
    var originConfig = readCOSConfig(saveFileName);
    fetchRainbowConfig(key, secretInfo, fetchRainbowConfigOptions).then(function (res) {
      var config = res;
      try {
        config = JSON.parse(res);
      } catch (err) {}
      var equal = JSON.stringify(config, null, 0) === JSON.stringify(originConfig, null, 0);
      console.log("[fetchLatestOneRainbowData] \u662F\u5426\u6709\u66F4\u65B0 ".concat(appName, "\xB7").concat(groupName, "\xB7").concat(envName, ".").concat(key, ": ").concat(!equal));
      saveJsonToLog(config, saveFileName);
      resolve({
        config: config,
        originConfig: originConfig,
        equal: equal
      });
    })["catch"](function (err) {
      reject(err);
    });
  });
}

export { fetchLatestOneRainbowData, fetchLatestRainbowData };
