from rr.utils import fix_json, fix_richtext

from ocyan.main.loading import get_ocyan_class

from .utils import uuid

convert_item_value = get_ocyan_class(
    "roadrunner_bs5.convert.item", "convert_item_value"
)


def bs4_to_bs5(
    json_content,
    image_keys=(
        "image",
        "product",
        "category",
        "template",
    ),
    richtext_keys=(
        "block_text",
        "tab_content",
        "html",
        "caption",
        "card_content",
        "modal_content",
        "panel_content",
        "media_content",
        "well_content",
        "body",
        "content",
        "text",
        "paragraph",
    ),
):
    """
    Wrap the bs4 rows in a row object.
    """
    result = []
    for container in json_content:
        if container["type"] in ["fixed_width", "full_width"]:
            rows = container["value"]
            container["value"] = {
                "row": [
                    {"type": "item", "value": convert_item_value(kv), "id": uuid()}
                    for kv in rows
                ],
                "styling": {
                    "margin": "",
                    "padding": "",
                    "border": "",
                    "border_radius": "",
                    "background_color": "",
                    "color": "",
                    "font_size": None,
                    "text_align": "",
                    "block_classes": "",
                },
                "id": uuid(),
            }
        bs5_container_with_real_richtext = fix_richtext(
            container, richtext_keys=richtext_keys
        )
        bs5_container_with_correct_image_ids = fix_json(
            bs5_container_with_real_richtext, image_keys=image_keys
        )
        result.append(bs5_container_with_correct_image_ids)

    return result
