'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var rainbowToCos_api = require('../rainbow-to-cos/api.js');
require('../rainbow/rainbow-admin.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/rainbow-user.js');
require('../rainbow/sdk.js');
require('../json/json-parse.js');
require('../list/flat.js');
require('../rainbow-to-cos/helper/helper.js');
require('../nodejs/path.js');
require('../tencent-cloud/cos/link.js');
require('../rainbow-to-cos/helper/value-type.js');
require('../string/string.js');
require('../fs/fs-util.js');
require('../fs/fs.js');
require('../nodejs/fs.js');
require('../nodejs/os.js');
require('../time/time.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);

function getHistoryModeConfigDiff(_ref) {
  var secretInfo = _ref.secretInfo,
    appName = _ref.appName,
    key = _ref.key,
    fetchRainbowConfigOptions = _ref.fetchRainbowConfigOptions;
  return new Promise(function (resolve, reject) {
    rainbowToCos_api.fetchLatestOneRainbowData({
      secretInfo: secretInfo,
      appName: appName,
      key: key,
      fetchRainbowConfigOptions: fetchRainbowConfigOptions
    }).then(function (res) {
      var equal = res.equal,
        config = res.config,
        originConfig = res.originConfig;
      var _findDiff = findDiff(config, originConfig),
        addedMap = _findDiff.addedMap,
        deletedMap = _findDiff.deletedMap,
        parsed = _findDiff.parsed,
        originParsed = _findDiff.originParsed;
      if (equal) {
        resolve({
          equal: equal,
          addedMap: addedMap,
          deletedMap: deletedMap,
          parsed: parsed,
          originParsed: originParsed
        });
        return;
      }
      resolve({
        equal: equal,
        addedMap: addedMap,
        deletedMap: deletedMap,
        parsed: parsed,
        originParsed: originParsed
      });
    })["catch"](function (err) {
      reject(err);
    });
  });
}
var parsedConfig = function parsedConfig(obj) {
  return Object.keys(obj).reduce(function (acc, domain) {
    var list = obj[domain];
    var names = list.map(function (item) {
      return item.packageName;
    }).filter(function (item) {
      return item;
    });
    acc[domain] = names;
    return acc;
  }, {});
};
var getDiff = function getDiff(current, another) {
  var targetMap = {};
  Object.keys(current).forEach(function (domain) {
    var list = current[domain];
    list.forEach(function (name) {
      var _a;
      var exist = (_a = another[domain]) === null || _a === void 0 ? void 0 : _a.includes(name);
      if (!exist) {
        targetMap = Object.assign(Object.assign({}, targetMap), _defineProperty__default["default"]({}, domain, [].concat(_toConsumableArray__default["default"](targetMap[domain] || []), [name])));
      }
    });
  });
  return targetMap;
};
function findDiff(config, originConfig) {
  var parsed = parsedConfig(config.data);
  var originParsed = parsedConfig(originConfig.data);
  var addedMap = getDiff(parsed, originParsed);
  var deletedMap = getDiff(originParsed, parsed);
  return {
    addedMap: addedMap,
    deletedMap: deletedMap,
    parsed: parsed,
    originParsed: originParsed
  };
}

exports.getHistoryModeConfigDiff = getHistoryModeConfigDiff;
