import json,re
K='/workspace/kit-wt/auth-kit/tokens'
launch=json.load(open('/workspace/auth-kit-20nov/tok-v12.json'))
sel={'pearl':'html[data-theme=day][data-pal=white]','champagne':'html[data-theme=day][data-pal=beige]','midnight':'html[data-theme=night][data-pal=black]','amethyst':'html[data-theme=night][data-pal=purple]'}
hdr='''/* Go Around · auth kit · tokens, LAUNCH family (v11 and v12 sign-in / sign-up on go-around-launch-11nov).
   Every value below is the computed value read from the live v12 Version 2 sign-in page (a851b7c8) in each of the four themes,
   so nothing is retyped. Selector style is the launch site's: html[data-theme=day|night][data-pal=white|beige|black|purple].
   Pearl = day/white, Champagne = day/beige, Midnight = night/black, Amethyst = night/purple.
   Components in components/launch-* load this file. Components in the other folders load tokens-design-repo.css instead.
   Load only ONE of the two token files on a page: they share variable names but not every value (see README). */
'''
out=hdr
for t in ['pearl','champagne','midnight','amethyst']:
    out+=f"\n/* {t.capitalize()} */\n{sel[t]}{{\n"+''.join(f"  {k}:{v};\n" for k,v in sorted(launch[t].items()))+"}\n"
open(K+'/tokens-launch.css','w').write(out)
# design repo (ga.css) verbatim blocks
ga=open('/workspace/v10-wt/components/pilot-ato-wired-19nov/shared/ga.css').read()
v10=open('/workspace/v10-wt/components/pilot-ato-wired-19nov/auth-v10/v10.css').read()
themes=re.findall(r'^html\[data-t=\w+\]\{.*?\}\n',ga,flags=re.S|re.M)
root=re.search(r'^:root\{--gold1.*?\}\n',ga,flags=re.S|re.M).group(0)
flare=re.findall(r'^html\[data-t=\w+\]\{--flare.*?\}\n',v10,flags=re.S|re.M)
mot=re.search(r':root\{\n /\* spring.*?\}\n@supports.*?\}\}\n',v10,flags=re.S).group(0)
red=re.search(r'@media \(prefers-reduced-motion:reduce\)\{:root\{--d-split.*?\}\}\n',v10).group(0)
d='''/* Go Around · auth kit · tokens, DESIGN-REPO family (go-around-design: auth-touch, v10, combined A and B, top bar unify).
   Copied verbatim from components/pilot-ato-wired-19nov/shared/ga.css (theme blocks + :root) and auth-v10/v10.css (spring, durations,
   each theme's own light: runway lights and progress flare). Selector style: html[data-t=pearl|champagne|midnight|amethyst]. */
'''
d+='\n/* Themes (ga.css) */\n'+''.join(themes)+'\n/* Shared (ga.css :root) */\n'+root+'\n/* Motion (v10.css) */\n'+mot+'\n/* Each theme\'s own light: runway lights + progress flare (v10.css) */\n'+''.join(flare)+'\n/* Reduced motion (v10.css) */\n'+red
open(K+'/tokens-design-repo.css','w').write(d)
print(len(themes),len(flare))
# json
def parse(block):
    return dict(re.findall(r'(--[\w-]+):([^;}]+)',block))
dr={re.search(r'data-t=(\w+)',b).group(1):parse(b) for b in themes}
for b in flare:
    n=re.search(r'data-t=(\w+)',b).group(1); dr[n].update(parse(b))
json.dump({'launch_v12_computed':launch,'design_repo_ga_css':dr,'design_repo_shared':parse(root)},open(K+'/tokens.json','w'),indent=1)
