/**
 * 向上找到最近的指定组件
 * @param {*} context 执行上下文
 * @param {*} cmpName 要查找的组件名
 */
export function findComponentUpward (context, cmpName) {
  let parent = context.$parent
  let name = parent.$options.name

  while (parent && (!name || name !== cmpName)) {
    parent = parent.$parent
    if (parent) name = parent.$options.name
  }
  
  return parent
}

/**
 * 向上找到所有指定的组件
 * @param {*} context 执行上下文
 * @param {*} cmpName 组件名
 */
export function findComponentsUpward (context, cmpName) {
  let parents = []
  const parent = context.$parent

  if (parent) {
    if (parent.$options.name === cmpName) parents.push(parent)
    return parents.concat(findComponentsUpward(parent, cmpName))
  } else {
    return []
  }
}

/**
 * 向下找到最近的指定组件
 * @param {*} context 执行上下文
 * @param {*} cmpName 组件名
 */
export function findComponentDownward (context, cmpName) {
  const children = context.$children
  let child = null

  if (children.length) {
    for (const item of children) {
      const name = item.$options.name
      if (name === cmpName) {
        child = item
        break;
      } else {
        child = findComponentDownward(item, cmpName)
        if (child) break;
      }
    }
  }
  return child
}


/**
 * 向下找到所有指定的组件-findComponentsDownward
 * @param {*} context 执行上下文
 * @param {*} cmpName 要查找的组件名
 */
export function findComponentsDownward (context, cmpName) {
  return context.$children.reduce((components, child) => {
    if (child.$options.name === cmpName) components.push(child)

    const foundChildren = findComponentsDownward(child, cmpName)
    return components.concat(foundChildren)
  }, [])
}

/**
 * 找到指定组件的兄弟组件
 * @param {*} context 执行上下文
 * @param {*} cmpName 组件名
 * @param {*} exceptMe 是否排除自身，默认true
 */
export function findBrothersComponents (context, cmpName, exceptMe = true) {
  let res = context.$parent.$children.filter(item => {
    return item.$options.name === cmpName
  })
  
  let index = res.findIndex(item => item._uid === context._uid)
  if (exceptMe && index >= 0) res.splice(index, 1)
  return res
}

/**
 * 格式化时间
 * @param {String|Number} oldDate 传入的日期
 * @param {String|Null} fmt 格式：YYYY-MM-DD hh:mm:ss
 * formatDate(time, 'YYYY-MM-DD hh:mm:ss')
 */
export function formatDate (oldDate, fmt) {
  let date = new Date()
  if (oldDate || typeof oldDate === 'string' || typeof oldDate === 'number') {
    date = new Date(oldDate)
  }

  const len2 = (num) => {
    return num < 10 ? '0' + num : '' + num
  }

  const obj = {
    YYYY: date.getFullYear(),
    YY: (date.getFullYear() + '').substring(2, 4) * 1,
    MM: len2(date.getMonth() + 1),
    DD: len2(date.getDate()),
    hh: len2(date.getHours()),
    mm: len2(date.getMinutes()),
    ss: len2(date.getSeconds())
  }

  if (fmt) {
    if (/YYYY/.test(fmt)) {
      fmt = fmt.replace('YYYY', obj.YYYY)
    } else {
      fmt = fmt.replace('YY', obj.YY)
    }
    fmt = fmt.replace('MM', obj.MM)
    fmt = fmt.replace('DD', obj.DD)
    fmt = fmt.replace('hh', obj.hh)
    fmt = fmt.replace('hh', obj.hh)
    fmt = fmt.replace('mm', obj.mm)
    fmt = fmt.replace('mm', obj.mm)
    fmt = fmt.replace('ss', obj.ss)
  } else {
    fmt = `${obj.YYYY}-${obj.MM}-${obj.DD}`
  }
  return fmt
}
