"""Literal, speculative 'shed circumferences' extraction; no fitted key.

Scope fixed before inspection: strip one rune at each end of every word or
every transcribed line, then read retained runes in original order using GP.
This is not claimed to be an instruction specified by the puzzle.
"""
import json
from lp_core import ROOT, R, LET, lp, vals
from word_core import tokenize


def interior(parts):
    return [p[1:-1] for p in parts if len(p) > 2]


def main():
    out=[]
    for section, source in enumerate('%'.join(lp[:56]).split('$')):
        if len(vals(source)) < 60:
            continue
        wordparts=[w['runes'] for w in tokenize(source)]
        lineparts=[''.join(c for c in line if c in R) for line in source.replace('%','/').split('/')]
        for unit,parts in [('word',wordparts),('line',lineparts)]:
            kept=interior(parts)
            text=' '.join(''.join(LET[R.index(c)] for c in part) for part in kept)
            out.append({'section':section,'unit':unit,'original_runes':len(vals(source)),
                        'retained_runes':sum(map(len,kept)),
                        'empty_or_short_units_discarded':sum(len(x)<=2 for x in parts),
                        'transliteration':text})
    result={'rule':'Remove first and last rune from each word or line; standard direct GP transliteration',
            'candidate_count':len(out),'candidates':out,
            'limits':['Only literal interior extraction, not another encryption layer.',
                      'No shifts, reversals, source-text keys or fitted parameters.',
                      'Word boundaries follow master transcription; line boundaries use slash markers.',
                      'Short units leave nothing; chapter/section boundaries are not merged.',
                      'Metaphorical source language does not establish this algorithm.']}
    (ROOT/'interior_results.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n')
    for x in out:print(x['section'],x['unit'],x['retained_runes'],x['transliteration'][:220])


if __name__=='__main__':main()
