export const getValue = (obj, variable, defaultValue = false) => {
  let isset = true
  if (typeof obj !== 'undefined') {
    let parts = variable.split('.')
    for (var i = 0; i < parts.length; i++) {
      if (isset !== false) {
        let variables = parts[i].match(/([a-zA-Z0-9-_]+)/g) || [parts[i]]
        variables.forEach(v => {
          if (isset !== false && typeof obj !== 'undefined' && obj !== null && typeof obj[v] !== 'undefined') {
            obj = obj[v]
          } else {
            isset = false
          }
        })
      }
    }
  } else {
    isset = false
  }
  return isset === true ? obj : defaultValue
}

export const isSet = (obj, variable) => {
  let isset = getValue(obj, variable)
  return isset !== false
}

export const isNull = (obj, variable) => {
  let isnull = false
  if (typeof obj !== 'undefined') {
    if (typeof variable !== 'undefined') {
      let parts = variable.split('.')
      for (var i = 0; i < parts.length; i++) {
        if (isnull === false) {
          let variables = parts[i].match(/(?=\S*[_-])([a-zA-Z-_]+)/g) || [parts[i]]
          variables.forEach(v => {
            if (isnull === false && typeof obj[v] !== 'undefined' && obj[v] !== null) {
              obj = obj[v]
            } else {
              isnull = true
            }
          })
        }
      }
    } else {
      if (obj === null) {
        isnull = true
      }
    }
  } else {
    isnull = true
  }
  return isnull
}

export const isEmpty = (obj, variable) => {
  let isempty = false
  if (typeof obj === 'undefined' || obj === null || obj === '' || obj === [] || obj === 0 || obj === false) {
    isempty = true
  }
  if (isempty === false) {
    if (typeof variable === 'string') {
      let parts = variable.split('.')
      for (var i = 0; i < parts.length; i++) {
        if (isempty === false) {
          let variables = parts[i].match(/(?=\S*[_-])([a-zA-Z-_]+)/g) || [parts[i]]
          variables.forEach(v => {
            if (
              isempty === false &&
              typeof obj[v] !== 'undefined' &&
              obj[v] !== null &&
              obj[v] !== '' &&
              obj[v] !== [] &&
              obj[v] !== {} &&
              obj !== 0 &&
              obj !== false
            ) {
              obj = obj[v]
            } else {
              isempty = true
            }
          })
        }
      }
    }
  } else {
    isempty = true
  }
  return isempty
}
