export function currentUser(elValue) {
  return elValue ? elValue['currentUser'] : null
}

export function currentUserName(elValue) {
  return elValue ? elValue['currentUserName'] : null
}

export function currentLoginName(elValue) {
  return elValue ? elValue['currentLoginName'] : null
}

export function currentOrg(elValue) {
  return elValue ? elValue['currentOrg'] : null
}

export function currentOrgName(elValue) {
  return elValue ? elValue['currentOrgName'] : null
}

export function currentDept(elValue) {
  return elValue ? elValue['currentDept'] : null
}

export function currentDeptName(elValue) {
  return elValue ? elValue['currentDeptName'] : null
}

export function currentDate(elValue) {
  return elValue ? elValue['currentDate'] : null
}

export function currentTime(elValue) {
  return elValue ? elValue['currentTime'] : null
}

export function getParentValue(parentValue, key) {
  return parentValue ? parentValue[key] : null
}

export function elValue(elValue, value) {
  let text = value.replace(/\$\{currentUser\}/g, currentUser(elValue) || '${currentUser}')
    .replace(/\$\{currentUserName\}/g, currentUserName(elValue) || '${currentUserName}')
    .replace(/\$\{currentLoginName\}/g, currentLoginName(elValue) || '${currentLoginName}')
    .replace(/\$\{currentOrg\}/g, currentOrg(elValue) || '${currentOrg}')
    .replace(/\$\{currentOrgName\}/g, currentOrgName(elValue) || '${currentOrgName}')
    .replace(/\$\{currentDept\}/g, currentDept(elValue) || '${currentDept}')
    .replace(/\$\{currentDeptName\}/g, currentDeptName(elValue) || '${currentDeptName}')
    .replace(/\$\{currentDate\}/g, currentDate(elValue) || '${currentDate}')
    .replace(/\$\{currentTime\}/g, currentTime(elValue) || '${currentTime}')

  // 自定义变量替换
  const parentArr = text.match(/\$\{parent\.[^\}]+\}/g)
  if (parentArr && parentArr.length > 0) {
    for (let i = 0; i < parentArr.length; i++) {
      const pattern = parentArr[i];
      const tt = pattern.match(/[^\$\{\}]+/g)
      const elKey = tt[0];
      text = text.replace(pattern, getParentValue(elValue, elKey) || pattern)
    }
  }

  return text;
}
