import { hyphenate } from '../../string/string.mjs';
import { getJsonLogDir, readJsonLog } from '../../fs/fs-util.mjs';
import { getPath } from '../../nodejs/path.mjs';
import { getCosUrlLink } from '../../tencent-cloud/cos/link.mjs';
import { RAINBOW_VALUE_TYPE_MAP } from './value-type.mjs';
import '@babel/runtime/helpers/toConsumableArray';
import '../../fs/fs.mjs';
import '../../nodejs/crypto.mjs';
import '../../nodejs/fs.mjs';
import '../../nodejs/os.mjs';
import '../../time/time.mjs';

function getSaveFileName(_ref) {
  var appName = _ref.appName,
    groupName = _ref.groupName,
    envName = _ref.envName,
    key = _ref.key,
    valueType = _ref.valueType;
  var _a;
  var ext = ((_a = RAINBOW_VALUE_TYPE_MAP[valueType]) === null || _a === void 0 ? void 0 : _a.ext) || 'txt';
  return "".concat(hyphenate(appName), "__").concat(hyphenate(groupName), "__").concat(hyphenate(envName), "__").concat(key, ".").concat(ext);
}
function getSavePath(_ref2) {
  var appName = _ref2.appName,
    groupName = _ref2.groupName,
    envName = _ref2.envName,
    key = _ref2.key,
    valueType = _ref2.valueType;
  return getPath().resolve(getJsonLogDir(), getSaveFileName({
    appName: appName,
    groupName: groupName,
    envName: envName,
    key: key,
    valueType: valueType
  }));
}
function getCOSFilePath(_ref3) {
  var appName = _ref3.appName,
    groupName = _ref3.groupName,
    envName = _ref3.envName,
    key = _ref3.key,
    valueType = _ref3.valueType,
    cosInfo = _ref3.cosInfo;
  var saveFileName = getSaveFileName({
    appName: appName,
    groupName: groupName,
    envName: envName,
    key: key,
    valueType: valueType
  });
  var bucket = cosInfo.bucket,
    region = cosInfo.region,
    dir = cosInfo.dir;
  return getCosUrlLink({
    bucket: bucket,
    region: region,
    dir: dir,
    fileName: saveFileName
  });
}
var readCOSConfig = function readCOSConfig(saveFileName) {
  var content = [];
  try {
    content = JSON.parse(readJsonLog(saveFileName, '[]'));
  } catch (err) {
    console.log('[readCOSConfig] err: \n', err);
  }
  return content;
};

export { getCOSFilePath, getSaveFileName, getSavePath, readCOSConfig };
