export const getMetadataByKey = (episode, key, firstItem = true) => {
  const filteredItems = episode.metadata.length ? episode.metadata.filter(
    meta => meta.key.includes(key)
  ) : null

  if (filteredItems) {
    return firstItem ? filteredItems[0].value : filteredItems
  }

  return null
}

export const getPostByKey = (posts, key) => {
  const filteredPostItems = posts.elements.length ? posts.elements.filter(post => post.title === key) : []
  return filteredPostItems.length ? filteredPostItems[0].body : null
}

export const getResourcesByKey = (resources, key, lookup = 'title') => {
  const filteredResources = resources.elements.length ? resources.elements.filter(resource => resource[lookup].includes(key)) : []

  if (!filteredResources.length) return null

  return filteredResources
}

export const getPeertubeIDfromUrl = (string) => string && string.includes('https://tv.undersco.re') ? string.split('/').pop() : string

export const getEpisodeCode = (episode) => {
  const metaItems = getMetadataByKey(episode, 'mz:plain:', false)
  if (metaItems && metaItems.length) {
    // TODO
  }

  return 'TODO'
}