 function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }


export function dateFilter(
  tx,
  afterDate = new Date(0),
  beforeDate = new Date()
) {
  return (
    tx.date * 1000 >= afterDate.valueOf() &&
    tx.date * 1000 < beforeDate.valueOf()
  )
}

export function searchStringFilter(
  ai,
  tx,
  searchString
) {
  const currencyState = ai.props.state.currency

  if (searchString == null || searchString === '') return true

  // Sanitize search string
  let cleanString = searchString.toLowerCase().replace('.', '').replace(',', '')
  // Remove leading zeroes
  for (let i = 0; i < cleanString.length; i++) {
    if (cleanString[i] !== '0') {
      cleanString = cleanString.substring(i)
      break
    }
  }

  function checkNullTypeAndIndex(value) {
    if (
      value == null ||
      (typeof value !== 'string' && typeof value !== 'number')
    )
      return false
    if (
      !value
        .toString()
        .toLowerCase()
        .replace('.', '')
        .replace(',', '')
        .includes(cleanString)
    )
      return false
    return true
  }

  if (checkNullTypeAndIndex(tx.nativeAmount)) return true
  if (tx.metadata != null) {
    const {
      category = '',
      name = '',
      notes = '',
      exchangeAmount = {}
    } = tx.metadata
    const txCurrencyWalletState =
      tx.walletId != null ? currencyState.wallets[tx.walletId] : undefined
    if (
      checkNullTypeAndIndex(category) ||
      checkNullTypeAndIndex(name) ||
      checkNullTypeAndIndex(notes) ||
      (txCurrencyWalletState != null &&
        checkNullTypeAndIndex(exchangeAmount[txCurrencyWalletState.fiat]))
    )
      return true
  }
  if (tx.swapData != null) {
    const { displayName = '', pluginId = '' } = tx.swapData.plugin
    if (checkNullTypeAndIndex(displayName) || checkNullTypeAndIndex(pluginId))
      return true
  }
  const action = _nullishCoalesce(tx.savedAction, () => ( tx.chainAction))

  if (action != null) {
    if (action.actionType === 'swap') {
      const { pluginId: destPluginId } = action.toAsset
      const { pluginId: sourcePluginId } = action.fromAsset
      const { displayName, supportEmail } = action.swapInfo
      if (
        checkNullTypeAndIndex(sourcePluginId) ||
        checkNullTypeAndIndex(destPluginId) ||
        checkNullTypeAndIndex(displayName) ||
        checkNullTypeAndIndex(supportEmail)
      )
        return true
    }
  }
  if (tx.spendTargets != null) {
    for (const target of tx.spendTargets) {
      const { publicAddress = '', memo = '' } = target
      if (checkNullTypeAndIndex(publicAddress) || checkNullTypeAndIndex(memo))
        return true
    }
  }
  if (tx.ourReceiveAddresses.length > 0) {
    for (const address of tx.ourReceiveAddresses) {
      if (checkNullTypeAndIndex(address)) return true
    }
  }
  if (checkNullTypeAndIndex(tx.txid)) return true
  return false
}
