import json
from pathlib import Path

from . import parsers

SOURCE_DATA_PATH = Path(__file__).parent / "sources"


SEARCH_PATTERNS = [
    "spells/spells-*.json",
    "items*.json",
]


def get_sources(pattern: str):
    yield from SOURCE_DATA_PATH.rglob(pattern)


def walk_sources():
    for search_path in SEARCH_PATTERNS:
        for source in get_sources(search_path):
            data = json.loads(source.read_text())
            yield from [i for i in data.items() if not i[0].startswith("_")]


def get_all():
    for source_type, entries in walk_sources():
        parser = parsers.get_parser(source_type)
        if not parser:
            continue
        for entry in entries:
            yield parser(entry).parse()
