// @requirements REQ-PRODUCT-COLLECTION-WORKBENCH REQ-PRODUCT-COLLECTION-FLOW import {scopeGuardContext} from './scope-guard-policy.js'; import {selectFormPermissionLocalization} from './form-permission-localization-policy.js'; import {readFile} from 'node:fs/promises'; import {createHash} from 'node:crypto'; import {normalizePublicLegalArtifact,createCollectionNotice,createCollectionFlow,compileLegalDocuments} from './content.js'; import {normalizeCollectionWorkbenchContext} from './collection-workbench.js'; import type {CollectionWorkbenchContext} from './collection-workbench.js'; import type {CollectionFlowSettings} from './collection-flow.js'; import type {ValidationOptions} from './types.js'; /** Prepare public facts from an intact host-selected final release. No private source/config notes are copied. */ export function prepareCollectionWorkbenchContext(input:unknown,selection:{purposeIds:readonly string[];settings?:CollectionFlowSettings},options:ValidationOptions={}):CollectionWorkbenchContext { const artifact=normalizePublicLegalArtifact(input,options); const notice=createCollectionNotice(artifact,{scopeId:'form-notice',purposeIds:selection.purposeIds,reviewStatus:'draft',wording:{title:'Unconfigured notice',intro:'Write the form-specific introduction in the workbench.'}},{mode:'preview'}); const {noticeId,scopeId,reviewStatus,wording,...facts}=notice; const initial=selection.settings===undefined?undefined:{settings:selection.settings,flow:createCollectionFlow(artifact,selection.settings,{mode:'preview'})}; let buildMethod:CollectionWorkbenchContext['buildMethod']='final-artifact'; try{if(compileLegalDocuments(artifact.publicConfig,{mode:artifact.manifest.mode}).manifest.artifactId===artifact.manifest.artifactId)buildMethod='configuration';}catch{} return normalizeCollectionWorkbenchContext({schemaVersion:1,kind:'notabenda-collection-workbench',buildMethod,facts,permissionContext:{scopeGuards:selection.purposeIds.map(purposeId=>({purposeId,assessment:scopeGuardContext(artifact.publicConfig.scopeGuards,purposeId)??null})),...(artifact.publicConfig.localization?.formPermission?{localization:selectFormPermissionLocalization(artifact.publicConfig.localization)}:{})},...(initial?{initial}:{})}); } /** A self-contained local HTML tool. It exports settings, never authoritative fragments or descriptors. */ export async function createCollectionWorkbenchFiles(input:unknown,selection:{purposeIds:readonly string[];settings?:CollectionFlowSettings},options:ValidationOptions&{delivery?:'standalone'|'directory'}={}):Promise> { const context=prepareCollectionWorkbenchContext(input,selection,options),script=await readFile(new URL('./collection-workbench-app.js',import.meta.url),'utf8'); if(/<\/script/iu.test(script))throw Error('Workbench script cannot be embedded safely.'); const css=(await readFile(new URL('./styles.css',import.meta.url),'utf8'))+'\n'+await readFile(new URL('./collection-workbench.css',import.meta.url),'utf8'); if(/<\/style/iu.test(css))throw Error('Workbench styles cannot be embedded safely.'); const digest=(v:string)=>createHash('sha256').update(v).digest('base64'),data=JSON.stringify(context).replace(/Prepare a form notice ยท Notabenda${directory?'':``}

Notabenda / form notices

Explain what this form collects.

Choose its purposes, write the notice, then decide which uses need a separate permission.

${directory?'':``}`}; }