'use strict';

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

var rainbow_rainbowAdmin = require('../rainbow/rainbow-admin.js');
var rainbow_rainbowUser = require('../rainbow/rainbow-user.js');
var rainbowToCos_helper_helper = require('./helper/helper.js');
var fs_fsUtil = require('../fs/fs-util.js');
require('@babel/runtime/regenerator');
require('../tslib.es6-0d92ef81.js');
require('../rainbow/helper/helper.js');
require('../rainbow/helper/rainbow-base-request.js');
require('axios');
require('../rainbow/helper/rainbow-signature.js');
require('../nodejs/crypto.js');
require('../rainbow/sdk.js');
require('../json/json-parse.js');
require('../list/flat.js');
require('../nodejs/path.js');
require('../tencent-cloud/cos/link.js');
require('./helper/value-type.js');
require('../string/string.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../fs/fs.js');
require('../nodejs/fs.js');
require('../nodejs/os.js');
require('../time/time.js');

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 = rainbowToCos_helper_helper.getSaveFileName({
      appName: appName,
      groupName: groupName,
      envName: envName,
      key: 'all_data',
      valueType: 4
    });
    var originConfig = rainbowToCos_helper_helper.readCOSConfig(saveFileName);
    rainbow_rainbowAdmin.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));
      fs_fsUtil.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 = rainbowToCos_helper_helper.getSaveFileName({
      appName: appName,
      groupName: groupName,
      envName: envName,
      key: key,
      valueType: valueType
    });
    var originConfig = rainbowToCos_helper_helper.readCOSConfig(saveFileName);
    rainbow_rainbowUser.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));
      fs_fsUtil.saveJsonToLog(config, saveFileName);
      resolve({
        config: config,
        originConfig: originConfig,
        equal: equal
      });
    })["catch"](function (err) {
      reject(err);
    });
  });
}

exports.fetchLatestOneRainbowData = fetchLatestOneRainbowData;
exports.fetchLatestRainbowData = fetchLatestRainbowData;
