import json


def global_extent(tiles):
    """
    Compute the global raster extent from a list of tiles
    Args:
        tiles: list of config files loaded from json files
    Returns:
         (min_x,max_x,min_y,max_y) tuple
    """
    min_x = None
    max_x = None
    min_y = None
    max_y = None

    # First loop is to compute global extent
    for tile in tiles:
        with open(tile, "r") as file:

            tile_cfg = json.load(file)

            size_x = tile_cfg["roi"]["x"]
            size_y = tile_cfg["roi"]["y"]
            size_w = tile_cfg["roi"]["w"]
            size_h = tile_cfg["roi"]["h"]

            if min_x is None or size_x < min_x:
                min_x = size_x
            if min_y is None or size_y < min_y:
                min_y = size_y
            if max_x is None or size_x + size_w > max_x:
                max_x = size_x + size_w
            if max_y is None or size_y + size_h > max_y:
                max_y = size_y + size_h

    return (min_x, max_x, min_y, max_y)