import itertools


def sample_chains(chain_keys, exp, idx):
    """
    Returns the `idx`th item in the chain of all chain keys to be sampled.
    Args:
        chain_keys (list): keys in the experimental configuration
            that are to be used in the full chain
        exp (dict): experimental configuration
        idx (int): index of the current sample
    Returns:
        dict: sampled point in the cartesian space (with keys = chain_keys)
    """
    conf = {}
    chain_values = [[(key, value) for value in exp[key]["from"]] for key in chain_keys]
    chain = list(itertools.chain(*chain_values))
    k, v = chain[idx % len(chain)]
    conf[k] = v
    if exp[k].get("normalized"):
        conf["normalization_folder"] = k
    return conf