import re

_DURATION_RE = re.compile(r'(?:(\d+)h\s*)?(?:(\d+)m\s*)?(?:(\d+)s)?')


def parse_duration(text):
    if not isinstance(text, str):
        raise ValueError('duration must be a string')

    match = _DURATION_RE.fullmatch(text.strip())
    if not match:
        raise ValueError('invalid duration: %r' % (text,))

    h, m, s = match.groups()
    if h is None and m is None and s is None:
        raise ValueError('duration must contain at least one component')

    return int(h or 0) * 3600 + int(m or 0) * 60 + int(s or 0)
