from typing import Dict


def parse_links_header(link_header: str) -> Dict[str, str]:
  """
  Parses the `Link` HTTP header and returns a map of the links. Logic from
  [PageLinks.java](https://github.com/eclipse/egit-github/blob/master/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/PageLinks.java#L43-75).
  """

  links = {}

  for link in link_header.split(','):
    segments = link.split(';')
    if len(segments) < 2:
      continue
    link_part = segments[0].strip()
    if not link_part.startswith('<') or not link_part.endswith('>'):
      continue
    link_part = link_part[1:-1]
    for rel in (x.strip().split('=') for x in segments[1:]):
      if len(rel) < 2 or rel[0] != 'rel':
        continue
      rel_value = rel[1]
      if rel_value.startswith('"') and rel_value.endswith('"'):
        rel_value = rel_value[1:-1]
      links[rel_value] = link_part

  return links