
from __future__ import annotations
from typing import Dict, List
from bs4 import BeautifulSoup
from shared.utils import normalize_text
from ...html_utils import absolutize_fragment
from ..registry import extractor
from ..helpers.desc import (
    split_description_container, find_description_container,
    pair_title_content_from_magento_tabs, scan_headings_for_sections,
    additional_attributes_table,
)
from ..helpers.text import clean_title, is_blacklisted_heading

@extractor
def ex_description_sections(soup: BeautifulSoup, url: str) -> Dict:
    description_html = None
    sections: List[Dict] = []
    desc_el = find_description_container(soup)
    if desc_el:
        open_html, sections_from_desc = split_description_container(desc_el)
        description_html = open_html or None
        sections.extend(sections_from_desc)

    existing = {s["title"].lower() for s in sections}
    for t, html_fragment in (pair_title_content_from_magento_tabs(soup) or scan_headings_for_sections(soup)):
        low = t.lower()
        if "product description" in low or low == "description" or "details" in low:
            if not description_html and html_fragment:
                description_html = absolutize_fragment(html_fragment)
            continue
        if t.lower() not in existing and normalize_text(BeautifulSoup(html_fragment, "lxml").get_text()):
            if not is_blacklisted_heading(t):
                sections.append({"title": clean_title(t), "html": absolutize_fragment(html_fragment)})
                existing.add(t.lower())
    addl = additional_attributes_table(soup)
    if addl and "additional information" not in existing and not is_blacklisted_heading("additional information"):
        sections.append({"title": "Additional Information", "html": addl})
    out = {"sections": sections}
    if description_html:
        out["description_html"] = description_html
    return out

