import json
from pathlib import Path


def sort_dict(obj):
    if isinstance(obj, dict):
        return {k: sort_dict(obj[k]) for k in sorted(obj)}
    if isinstance(obj, list):
        return [sort_dict(i) for i in obj]
    return obj


def normalize_file(in_path: Path, out_path: Path):
    data = json.loads(in_path.read_text(encoding='utf-8'))
    normalized = []
    for item in data:
        title = item.get('title')
        selectors_raw = item.get('selectors')
        # selectors may already be a dict or a JSON-encoded string
        if isinstance(selectors_raw, str):
            try:
                selectors_obj = json.loads(selectors_raw)
            except Exception:
                # fallback: try to evaluate with single quotes replaced
                try:
                    selectors_obj = json.loads(selectors_raw.replace("'", '"'))
                except Exception:
                    selectors_obj = selectors_raw
        else:
            selectors_obj = selectors_raw

        # canonicalize
        canon = sort_dict(selectors_obj) if isinstance(selectors_obj, (dict, list)) else selectors_obj

        normalized.append({'title': title, 'selectors': canon})

    out_path.write_text(json.dumps(normalized, ensure_ascii=False, indent=2), encoding='utf-8')


def main():
    base = Path(__file__).parent
    files = [
        base / 'extractly_sourcemanual.json',
        base / 'extractly_sourcemanuLocal.json',
    ]
    for f in files:
        out = f.with_name(f.stem + '_normalized.json')
        print(f'Normalizing {f.name} -> {out.name}')
        normalize_file(f, out)


if __name__ == '__main__':
    main()
