"""One-time import of the requested scope; subsequent changes belong in manifest.json."""
import json, re, sys
from pathlib import Path

text = Path(sys.argv[1]).read_text(encoding='utf-8')
units = []
for number, name, body in re.findall(r'### Unit (\d+): ([^\n]+)\n(.*?)(?=### Unit|This list is a starting framework)', text, re.S):
    slug = re.sub(r'[^a-z0-9]+', '-', name.lower()).strip('-')
    topics = []
    for title in re.findall(r'^- (.+)$', body, re.M):
        title = title.strip().replace(' (where applicable)', '')
        topics.append({'title':title, 'slug':re.sub(r'[^a-z0-9]+','-',title.lower()).strip('-'), 'teks':[], 'status':'planned'})
    units.append({'id':int(number),'title':name.strip(),'slug':slug,'topics':topics})
extra = {
  4: ['Continuous and Discrete Linear Domains', 'Horizontal and Vertical Lines', 'Transformations of Linear Functions'],
  5: ['Correlation Coefficient with Technology', 'Association and Causation'],
  7: ['Dividing Polynomials', 'Rational Exponents', 'Perfect Square Trinomials', 'Difference of Two Squares'],
  8: ['Exponential Domain and Range', 'Exponential Asymptotes', 'Exponential Regression'],
  9: ['Quadratic Domain and Range', 'Writing Quadratics from a Vertex and a Point', 'Writing Quadratics from Roots', 'Linear Factors and Zeros', 'Quadratic Regression'],
}
for unit in units:
    for title in extra.get(unit['id'],[]):
        unit['topics'].append({'title':title,'slug':re.sub(r'[^a-z0-9]+','-',title.lower()).strip('-'),'teks':[],'status':'planned'})
Path('data/algebra/manifest.json').write_text(json.dumps(units,indent=2)+'\n',encoding='utf-8')
print(len(units),'units;',sum(len(u['topics']) for u in units),'topics')
