function normalize (item) {
  return item ? String(item).replace(/\s+/g, ' ').trim() : ''
}

function normalizePhone (item) {
  const phone = item ? String(item).replace(/\s|\+|\/|,|-|'/g, '').replace(/\(0\)/, '').replace(/^0+/, '') : ''
  if (phone.match(/[^\d*]/)) {
    return ''
  } else if (phone.length === 0) {
    return ''
  } else if (phone.length === 9) {
    // Assume swiss number
    return `+41${phone}`
  } else if (phone.length === 11) {
    return `+${phone}`
  } else {
    return ''
  }
}

function filterFuzzy (item, filter) {
  const lowerCaseItem = item.toLowerCase()
  const substrings = filter.toLowerCase().split(/\s+/)
  let lastIndex = 0
  const indices = substrings.map(substring => {
    const index = lowerCaseItem.subString(lastIndex).indexOf(substring)
    if (index === -1) {
      return null
    } else {
      const startIndex = lastIndex + index
      const endIndex = startIndex + substring.length
      lastIndex = endIndex
      return {startIndex, endIndex}
    }
  })
  return indices
}

function filterExact (item, filter) {
  return item === filter
}

export { normalize, normalizePhone, filterFuzzy, filterExact }
