from __future__ import annotations
import re
from html.parser import HTMLParser
from typing import Any


class _Tables(HTMLParser):
    def __init__(self, source: str) -> None:
        super().__init__(convert_charrefs=True)
        self.source = source
        self.offsets = [0]
        self.offsets.extend((match.end() for match in re.finditer("\n", source)))
        self.tables: list[list[dict[str, Any]]] = []
        self.table: list[dict[str, Any]] | None
        self.row: list[str] | None
        self.cell: list[str] | None
        self.cell_tag: str | None
        self.cell_parts: list[tuple[str, int]] | None
        self.row_spans: list[list[int] | None] | None
        self.table = self.row = self.cell = None
        self.cell_tag = self.cell_parts = self.row_spans = None
        self.row_start: int | None = None
        self.depth = 0
        self.invalid = False

    def position(self) -> int:
        (line, column) = self.getpos()
        return self.offsets[line - 1] + column

    def handle_starttag(
        self, tag: str, attrs: list[tuple[str, str | None]] | dict[str, str | None]
    ) -> None:
        if tag == "table":
            self.depth += 1
            if self.depth != 1:
                self.invalid = True
            else:
                self.table = []
        if not self.depth:
            return
        attrs = dict(attrs)
        if (
            "hidden" in attrs
            or (attrs.get("aria-hidden") or "").lower() == "true"
            or re.search(
                "(?:display\\s*:\\s*none|visibility\\s*:\\s*hidden)",
                attrs.get("style") or "",
                re.IGNORECASE,
            )
            or (tag in ("script", "style"))
        ):
            self.invalid = True
        if tag == "tr":
            if self.row is not None or self.cell is not None:
                self.invalid = True
            (self.row, self.row_start, self.row_spans) = ([], self.position(), [])
        elif tag in ("td", "th"):
            if self.row is None or self.cell is not None:
                self.invalid = True
            if attrs.get("rowspan", "1") != "1" or attrs.get("colspan", "1") != "1":
                self.invalid = True
            (self.cell, self.cell_tag, self.cell_parts) = ([], tag, [])
        elif tag in ("br", "p", "div", "ul", "ol", "li") and self.cell is not None:
            self.cell.append(" ")

    def handle_data(self, data: str) -> None:
        if self.cell is not None:
            self.cell.append(data)
            self.cell_parts.append((data, self.position()))
        elif self.row is not None and data.strip():
            self.invalid = True

    def handle_endtag(self, tag: str) -> None:
        if not self.depth:
            return
        if tag in ("td", "th"):
            if self.cell is None or self.row is None or tag != self.cell_tag:
                self.invalid = True
            else:
                value = re.sub("\\s+", " ", "".join(self.cell)).strip()
                self.row.append(value)
                matches = [
                    position + data.find(value)
                    for (data, position) in self.cell_parts
                    if value
                    and data.count(value) == 1
                    and self.source.startswith(value, position + data.find(value))
                ]
                self.row_spans.append(
                    [matches[0], matches[0] + len(value)] if len(matches) == 1 else None
                )
            self.cell = self.cell_tag = self.cell_parts = None
        elif tag == "tr":
            if self.row is None or self.cell is not None or self.table is None:
                self.invalid = True
            else:
                end = self.source.find(">", self.position()) + 1
                self.table.append(
                    {
                        "cells": self.row,
                        "line": self.source[self.row_start : end],
                        "offset": self.row_start,
                        "spans": self.row_spans,
                    }
                )
            self.row = self.row_start = self.row_spans = None
        elif tag == "table":
            if self.depth == 1:
                if self.row is not None or self.cell is not None:
                    self.invalid = True
                self.tables.append(self.table)
                self.table = None
            self.depth -= 1
        elif tag in ("p", "div", "ul", "ol", "li") and self.cell is not None:
            self.cell.append(" ")


def tables(source: str) -> list[list[dict[str, Any]]] | None:
    parser = _Tables(source)
    try:
        parser.feed(source)
        parser.close()
    except (ValueError, TypeError):
        return None
    if (
        parser.invalid
        or parser.depth
        or parser.row is not None
        or (parser.cell is not None)
    ):
        return None
    return parser.tables
