// 查找祖先组件
export default function findAncestorComponent(current, name, maxLevel = Infinity) {
  let level = 1
  let parent = current?.$parent
  if (!parent) {
    return
  }

  const isMatch = (component) => {
    const componentName = component.$options.name
    if (!componentName) {
      return false
    }
    return typeof name === 'string' ? componentName === name : name(component)
  }

  while (parent && !isMatch(parent)) {
    parent = parent.$parent
    level++
    if (level > maxLevel) return
  }
  return parent
}
