#!/usr/bin/env python3
# Kolaboro worker module map (n31t, 18 Nov 2026). Run from the repository root: python3 _s/kolaboro.com/open/worker-modules.py
# Reads _worker.js/*.js, finds every static and dynamic import, checks that each module is reachable from index.js, lists exported
# names that no other file uses, and writes _s/kolaboro.com/data/worker-modules.json (shown on https://kolaboro.com/engineering/modules/).
import re, glob, json, os, hashlib, datetime, subprocess
W = '_worker.js/'
fs = sorted(os.path.basename(f) for f in glob.glob(W + '*.js'))
src = {f: open(W + f, encoding='utf-8').read() for f in fs}
IMP = re.compile(r"""(import\s*\(\s*|from\s*)['"]\./([\w.-]+\.js)['"]""")
dep = {f: {} for f in fs}
for f in fs:
    for m in IMP.finditer(src[f]):
        dep[f][m.group(2)] = 'dynamic' if m.group(1).startswith('import') else 'static'
by = {f: [] for f in fs}
for f in fs:
    for g, k in dep[f].items():
        if g in by: by[g].append([f, k])
seen, st = set(), ['index.js']
while st:
    f = st.pop()
    if f in seen or f not in dep: continue
    seen.add(f); st += list(dep[f])
def about(s):
    out = []
    for l in s.split('\n')[:6]:
        l = l.strip()
        if not l.startswith('//'): break
        out.append(l[2:].strip())
    return ' '.join(out)[:300]
def family(f):
    if f in ('index.js', 'kolaboro.js'): return 'core'
    if f.endswith('_i18n.js') or f.endswith('_lines.js'): return 'translations'
    if not f.startswith('kb'): return 'other sites'
    return 'feature'
unused = []
for f, s in src.items():
    other = '\n'.join(v for k, v in src.items() if k != f)
    for m in re.finditer(r'export\s+(?:async\s+)?(?:function|const|let)\s+(\w+)', s):
        n = m.group(1)
        if not re.search(r'\b' + n + r'\b', other) and len(re.findall(r'\b' + n + r'\b', s)) <= 1: unused.append([f, n])
try: commit = subprocess.check_output(['git', 'rev-parse', '--short=10', 'HEAD'], text=True).strip()
except Exception: commit = None
mods = [{'file': f, 'bytes': len(src[f].encode('utf-8')), 'lines': src[f].count('\n') + 1, 'family': family(f), 'reachable': f in seen,
         'imports': sorted(dep[f]), 'imported_by': sorted(by[f]), 'loaded': 'entry' if f == 'index.js' else ('static' if any(k == 'static' for _, k in by[f]) else 'on demand'),
         'about': about(src[f])} for f in fs]
out = {'about': 'Modules of the kolaboro.com edge worker (Cloudflare Pages Functions, _worker.js/). "on demand" modules are loaded with import() only by the routes that need them, so a page pays only for its own code. Generated by /open/worker-modules.py from the repository.',
       'generated': datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%MZ'), 'commit_before': commit,
       'totals': {'modules': len(fs), 'bytes': sum(m['bytes'] for m in mods), 'unreachable': [m['file'] for m in mods if not m['reachable']],
                  'on_demand': sum(1 for m in mods if m['loaded'] == 'on demand'), 'static': sum(1 for m in mods if m['loaded'] == 'static'),
                  'families': {k: sum(1 for m in mods if m['family'] == k) for k in ('core', 'feature', 'translations', 'other sites')}},
       'unused_exports': unused, 'modules': mods}
open('_s/kolaboro.com/data/worker-modules.json', 'w', encoding='utf-8').write(json.dumps(out, ensure_ascii=False, indent=1))
print(json.dumps(out['totals']), 'unused exports:', len(unused))
