import pickle

import orjson

from src.config import Configuration
from src.internals.serializers.compressors import compressors

unsafe_dumper = orjson.dumps
unsafe_loader = orjson.loads

safe_dumper = pickle.dumps
safe_loader = pickle.loads

compression_choice = Configuration().redis["compression"]
assert Configuration().redis["compression"] in compressors

if compression_choice != "raw":
    compressor, decompressor = compressors[compression_choice]

    base_unsafe_dumper = unsafe_dumper
    base_unsafe_loader = unsafe_loader

    base_safe_dumper = safe_dumper
    base_safe_loader = safe_loader

    def unsafe_dumper(data):
        return compressor(base_unsafe_dumper(data))

    def unsafe_loader(data):
        return base_unsafe_loader(decompressor(data))

    def safe_dumper(data):
        return compressor(base_safe_dumper(data))

    def safe_loader(data):
        return base_safe_loader(decompressor(data))
