'use strict';

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

/**
 * 获取镜像名称
 * 根据项目名、子项目名和分支生成镜像名称
 * @param projectName - 项目名称
 * @param subProjectName - 子项目名称
 * @param branch - 分支名称
 * @returns 镜像名称
 * @example
 * ```ts
 * const imageName = getImageName({
 *   projectName: 'my-project',
 *   subProjectName: 'sub-project',
 *   branch: 'release'
 * });
 * // 'my-project.sub-project'
 * ```
 */
function getImageName(_ref) {
  var projectName = _ref.projectName,
    subProjectName = _ref.subProjectName,
    branch = _ref.branch;
  var innerParse = function innerParse(str) {
    return str.replace(/\//g, '.');
  };
  var prefix = "".concat(innerParse(projectName), ".").concat(innerParse(subProjectName));
  if (branch === 'release') {
    return prefix;
  }
  var result = "".concat(prefix, ".").concat(innerParse(branch));
  return result;
}
/**
 * 解析灰度发布配置
 * 将嵌套的配置对象转换为平面的映射表
 * @param config - 原始配置对象
 * @returns 解析后的全局灰度发布配置
 * @example
 * ```ts
 * const config = {
 *   'project1': {
 *     'sub1': {
 *       'release': { grayVersion: '1.0.0', grayPercent: '50', cookieId: 'xxx' }
 *     }
 *   }
 * };
 * const parsed = parseGrayPublishConfig(config);
 * // { 'project1.sub1': { grayVersion: '1.0.0', ... } }
 * ```
 */
function parseGrayPublishConfig() {
  var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var map = {};
  Object.keys(config).forEach(function (projectName) {
    var projectMap = config[projectName];
    Object.keys(projectMap).forEach(function (subProjectName) {
      var subProjectMap = projectMap[subProjectName];
      Object.keys(subProjectMap).forEach(function (branch) {
        var coreConfig = subProjectMap[branch];
        var curPackageName = getImageName({
          projectName: projectName,
          subProjectName: subProjectName,
          branch: branch
        });
        map[curPackageName] = coreConfig;
      });
    });
  });
  return map;
}
/**
 * 获取当前项目使用的灰度配置
 * 根据子项目名称查找匹配的灰度发布配置
 * @param fullSubProjectName - 完整的子项目名称
 * @param globalGrayPublishConfig - 全局灰度发布配置
 * @returns 匹配的灰度配置列表
 * @example
 * ```ts
 * const grayList = getCurrentProjectUseGray(
 *   'my-project.sub-project',
 *   globalGrayPublishConfig
 * );
 * // [
 * //   {
 * //     fullSubProjectName: 'my-project.sub-project',
 * //     packageName: 'my-project.sub-project.dev',
 * //     parsedBranch: '.dev',
 * //     cookieId: 'xxx'
 * //   }
 * // ]
 * ```
 */
function getCurrentProjectUseGray(fullSubProjectName, globalGrayPublishConfig) {
  var keys = Object.keys(globalGrayPublishConfig);
  var list = keys.filter(function (key) {
    return key === fullSubProjectName || key.startsWith("".concat(fullSubProjectName, "."));
  }).filter(function (packageName) {
    var info = globalGrayPublishConfig[packageName];
    return !!(info.grayVersion && info.grayPercent && info.cookieId);
  }).map(function (packageName) {
    var parsedBranch = packageName.replace(fullSubProjectName, '');
    return {
      fullSubProjectName: fullSubProjectName,
      packageName: packageName,
      parsedBranch: parsedBranch,
      cookieId: globalGrayPublishConfig[packageName].cookieId || ''
    };
  });
  return list;
}

exports.getCurrentProjectUseGray = getCurrentProjectUseGray;
exports.getImageName = getImageName;
exports.parseGrayPublishConfig = parseGrayPublishConfig;
