import json
import io
import gzip
import base64


def get_coriolis_url(loadout_event: dict) -> str:
    """Create url from loadout event that imports ship build into coriolis.io"""

    def encode_loadout_event(loadout_event_: dict) -> str:
        string = json.dumps(loadout_event_, ensure_ascii=False, sort_keys=True, separators=(',', ':')).encode('utf-8')
        out = io.BytesIO()

        with gzip.GzipFile(fileobj=out, mode="w") as f:
            f.write(string)

        return base64.urlsafe_b64encode(out.getvalue()).decode().replace("=", "%3D")

    return f"https://coriolis.io/import?data={encode_loadout_event(loadout_event)}"