"""Versioned editor-only resources; never rewrites frozen337 or human events."""
from pathlib import Path
import sys

PROJECT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(PROJECT))
from pallet.mask_class_keypoint_review import EXTENSION,EDIT_NOTICE,editable_editor_js
from pallet.mask_model_review import HUMAN_KEYPOINT_EDIT_POLICY
from pallet.mask_gallery import ASSETS
from pallet.cluster_targets import digest
from pallet.pose_review_geometry import canonical
from scripts.build_mask_remaining_direct_requests import build_font


def main():
    if (EXTENSION/'editor_extension_manifest.json').exists():raise ValueError('Do not overwrite an existing editor-extension version')
    if not (EXTENSION/'private/before.json').is_file():raise ValueError('Capture genuine sources/history before any runtime update')
    root=PROJECT/'outputs/pallet_mask_model_requests_v3';js=editable_editor_js()
    if (EXTENSION/'editor.js').exists():raise ValueError('Preserve any partial resource generation')
    (EXTENSION/'editor.js').write_text(js)
    build_font(EXTENSION,(root/'report_zh.html').read_text()+(ASSETS/'editor.html').read_text()+js+EDIT_NOTICE)
    value=dict(schema='class_keypoint_editor_only_extension_v1',policy=HUMAN_KEYPOINT_EDIT_POLICY,
        authorization='User: 微调时保留原有的增加关键点功能',
        initial_worklist_manifest_sha256=digest(root/'manifest.json'),class_standard_sha256=digest(root/'class_keypoint_standard.json'),
        source_namespace='model-class-keypoints-v3',local_draft_namespace_unchanged=True,
        frozen_initial_keypoint_counts_unchanged=True,human_changes_do_not_change_class_standard=True,
        human_saved_polygon_valid_range=[3,128],artifacts_not_written_to_frozen_worklist=True,
        assets={name:digest(EXTENSION/name) for name in ('editor.js','report-font.otf','FONT_LICENSE.txt')},
        initial_history_baseline_sha256=digest(EXTENSION/'private/history_baseline.json'),code_sha256=digest(__file__))
    with (EXTENSION/'editor_extension_manifest.json').open('xb') as f:f.write(canonical(value)+b'\n')
    print('Prepared add-keypoint editor extension; frozen initial337 and genuine human history untouched')


if __name__=='__main__':main()
