from pathlib import Path


def get_item_sessions_pairs(files: list) -> list:
    """Function retrieves items, sessions and metadata part of a file.

    Parameters
    ----------
    files : list

    Returns
    -------
    list
        (item-sessions map file path, session-items map file path, file metadata)

    """

    metas = list()
    items = list()
    sessions = list()

    for ffile in files:
        fname = Path(ffile).stem
        # Get ID
        id_meta = fname[1+fname.find('_'):fname.rfind('.')]
        if id_meta in metas:
            pass
        else:
            metas.append(id_meta)
            for _f in files:
                items_prefix = 'items_' + id_meta
                sessions_prefix = 'sessions_' + id_meta
                if items_prefix in _f:
                    items.append(_f)
                elif sessions_prefix in _f:
                    sessions.append(_f)
            # Sanity check
            if len(metas) != len(items) != len(sessions):
                raise ValueError(f'Missing files, number of sessions: {len(sessions)}, '
                                 f'number of items: {len(items)}')

    output = list(zip(items, sessions, metas))
    return output