import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { fetchLatestOneRainbowData } from '../rainbow-to-cos/api.mjs';
import '../fs/fs-util.mjs';
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 '../rainbow/rainbow-admin.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 '../rainbow/rainbow-user.mjs';
import '../list/flat.mjs';
import '@babel/runtime/helpers/typeof';
import '../rainbow/sdk.mjs';
import '../json/json-parse.mjs';
import '../rainbow-to-cos/helper/helper.mjs';
import '../string/string.mjs';
import '../tencent-cloud/cos/link.mjs';
import '../rainbow-to-cos/helper/value-type.mjs';

function getHistoryModeConfigDiff(_ref) {
  var secretInfo = _ref.secretInfo,
    appName = _ref.appName,
    key = _ref.key,
    fetchRainbowConfigOptions = _ref.fetchRainbowConfigOptions;
  return new Promise(function (resolve, reject) {
    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({}, domain, [].concat(_toConsumableArray(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
  };
}

export { getHistoryModeConfigDiff };
