from shapely.geometry import MultiLineString, Polygon
from shapely.ops import polygonize, unary_union
from shapely.validation import make_valid

# Your list of line segments
lines = [
    [[236, 1624], [216, 1405]],
    [[216, 1405], [3, 1403]],
    [[216, 1405], [181, 1050]],
    [[181, 1050], [239, 872]],
    [[239, 872], [261, 800]],
    [[95, 857], [239, 872]],
    [[70, 854], [62, 972]],
    [[110, 746], [95, 857]],
    [[95, 857], [70, 854]],
    [[62, 972], [116, 972]],
    [[221, 775], [261, 800]],
    [[462, 927], [386, 1074]],
    [[116, 972], [130, 1065]],
    [[427, 905], [490, 766]],
    [[427, 905], [462, 927]],
    [[386, 1074], [410, 1628]],
    [[616, 1178], [651, 1048]],
    [[710, 841], [651, 1048]],
    [[130, 1065], [82, 1140]],
    [[490, 766], [289, 621]],
    [[608, 1398], [616, 1178]],
    [[764, 1231], [616, 1178]],
    [[766, 501], [710, 841]],
    [[710, 841], [937, 890]],
    [[82, 1140], [3, 1161]],
    [[600, 1633], [608, 1398]],
    [[743, 1407], [608, 1398]],
    [[909, 1255], [764, 1231]],
    [[764, 1231], [743, 1407]],
    [[828, 484], [766, 501]],
    [[766, 501], [463, 589]],
    [[913, 1204], [937, 890]],
    [[1238, 782], [828, 484]],
    [[867, 471], [828, 484]],
    [[463, 589], [325, 543]],
    [[1056, 1264], [913, 1204]],
    [[913, 1204], [909, 1255]],
    [[1252, 770], [1238, 782]],
    [[1221, 746], [867, 471]],
    [[880, 452], [867, 471]],
    [[1019, 1643], [1056, 1264]],
    [[1073, 1102], [1056, 1264]],
    [[1252, 770], [1221, 746]],
    [[1221, 746], [1459, 629]],
    [[1490, 1123], [1073, 1102]],
    [[3, 1619], [236, 1624]],
    [[236, 1624], [410, 1628]],
    [[410, 1628], [600, 1633]],
    [[600, 1633], [875, 1640]],
    [[875, 1640], [1019, 1643]],
    [[1019, 1643], [1115, 1648]],
    [[1115, 1648], [1380, 1685]],
    [[1380, 1685], [1393, 1638]],
    [[1393, 1638], [1490, 1123]],
    [[1490, 1123], [1481, 930]],
    [[1481, 930], [1252, 770]],
    [[1252, 770], [1463, 674]],
    [[1463, 674], [1459, 629]],
    [[1459, 629], [1387, 10]],
    [[1387, 10], [1260, 12]],
    [[1260, 12], [1096, 1]],
    [[1096, 1], [880, 452]],
    [[880, 452], [455, 549]],
    [[455, 549], [347, 496]],
    [[347, 496], [325, 543]],
    [[325, 543], [289, 621]],
    [[289, 621], [221, 775]],
    [[221, 775], [110, 746]],
    [[110, 746], [3, 713]],
    [[3, 713], [3, 848]],
    [[3, 848], [3, 1161]],
    [[3, 1161], [3, 1403]],
    [[3, 1403], [3, 1619]],
    [[70, 854], [3, 848]],
    [[261, 800], [427, 905]],
    [[462, 927], [651, 1048]],
    [[909, 1255], [875, 1640]]
]


def get_broken_lines(lines):
    mls = MultiLineString([ (tuple(p1), tuple(p2)) for p1, p2 in lines ])
    unioned = unary_union(mls)
    polys = list(polygonize(unioned))
    overall = unary_union(polys)

    outer_lines = []
    ext_coords = list(overall.exterior.coords)
    for i in range(len(ext_coords) - 1):
        outer_lines.append([list(ext_coords[i]), list(ext_coords[i + 1])])

    inner_lines = []
    for poly in polys:
        coords = list(poly.exterior.coords)
        for i in range(len(coords) - 1):
            line = [list(coords[i]), list(coords[i + 1])]
            line_ = [list(coords[i]), list(coords[i + 1])]
            if line not in outer_lines:
                if line_ not in outer_lines:
                    inner_lines.append(line)

    # Remove duplicated lines (regardless of order) in both outer_lines and inner_lines

    def dedup_lines(lines):
        seen = set()
        deduped = []
        for line in lines:
            # Make a tuple key for the line, sorted so [a,b] == [b,a]
            key = tuple(sorted((tuple(line[0]), tuple(line[1]))))
            if key not in seen:
                deduped.append(line)
                seen.add(key)
        return deduped

    outer_lines = dedup_lines(outer_lines)
    inner_lines = dedup_lines(inner_lines)
    return outer_lines, inner_lines


if __name__ == "__main__":
    broken_lines = get_broken_lines(lines)
    print(broken_lines[0])
    print(broken_lines[1])