import requests


def getSTCNItem(
        itemLocations: list,
        holdingArchive: str = "Amsterdam, Rijksmuseum Research Library",
        ENDPOINT: str = "http://data.bibliotheken.nl/sparql"):
    """
    Find the STCN item for a given itemLocation and holdingArchive.

    Args:
        itemLocations (list): The itemLocations of the item to search for.
        holdingArchive (str, optional): The holdingArchive of the item to search for.
        ENDPOINT (str, optional): The SPARQL endpoint to use.
    
    Returns:
        List of dicts with with items    
    """

    books = []
    for itemLocation in itemLocations:

        q = f"""
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX schema: <http://schema.org/>

        SELECT * WHERE {{
        ?book a schema:IndividualProduct ;
                schema:itemLocation ?shelfmark ;
                schema:holdingArchive ?archive .
        
        FILTER(?shelfmark = "{itemLocation}")
        FILTER(CONTAINS(?archive, "{holdingArchive}"))
        }}
        """

        headers = {"Accept": "application/sparql-results+json"}
        params = {"query": q}
        r = requests.get(ENDPOINT, headers=headers, params=params)
        data = r.json()

        for r in data["results"]["bindings"]:
            record = dict()
            for k, v in r.items():
                record[k] = v["value"]
            books.append(record)

    return books