/**
 * 参数遍历
 * @param params
 * @param isMatch 匹配方法
 * @param onMatch 匹配触发
 * @returns
 */
export function renderParams(
  params: any,
  isMatch: (params: any) => boolean,
  onMatch: (params: any) => any
) {
  if (!params) return params;
  if (isMatch(params)) return onMatch(params);
  if (Array.isArray(params)) {
    params.forEach(
      (item, i) => (params[i] = renderParams(item, isMatch, onMatch))
    );
  } else if (typeof params === "object") {
    for (const key in params) {
      params[key] = renderParams(params[key], isMatch, onMatch);
    }
  }
  return params;
}

/**
 * 参数遍历，支持结束回调
 */
export function renderParamsWithFinal(
  params: any,
  isMatch: (params: any) => boolean,
  onMatch: (params: any) => any,
  onFinal: (matched: boolean, res?: any) => any
) {
  let matched = false;
  const res = renderParams(params, isMatch, (...params) => {
    matched = true;
    onMatch(...params);
  });
  onFinal(matched, res);
  return res;
}
