from .s32_region_util import calculate_boundary, inflate_boundary
from .s34_sa_cal import *
from .s34_sa import get_all_service_area_ids
from .batch_exe import execute_batch_command
from .database import ChangeSet

def generate_service_area(name: str, inflate_delta: float = 0.5) -> ChangeSet:
    cs = ChangeSet()

    for id in get_all_service_area_ids(name):
        cs.delete({'type': 'service_area', 'id': id})

    sass = calculate_service_area(name)

    time_index = 0
    for sas in sass:
        for source, nodes in sas.items():
            boundary = calculate_boundary(name, nodes)
            boundary = inflate_boundary(name, boundary, inflate_delta)
            cs.add({ 'type': 'service_area', 'id': f"SA_{source}_{time_index}", 'boundary': boundary, 'time_index': time_index, 'source': source, 'nodes': nodes })
        time_index += 1

    return execute_batch_command(name, cs)
