// @requirements REQ-PRODUCT-TERMS-HISTORY REQ-PRODUCT-CF-ADMIN REQ-PRODUCT-A11Y REQ-PRODUCT-MOBILE import {normalizeTermsSettingsRelease} from './terms-history.js'; import type {TermsSettingsRelease} from './terms-history.js'; import type {TermsAgreementSetting} from './terms-settings.js'; import {canonicalJson} from './history.js'; import {escapeHtml as e,renderWorkspaceShell} from './render.js'; import type {RenderOptions} from './render.js'; import type {EvidenceRecord,JsonValue} from './types.js'; const same=(a:unknown,b:unknown)=>canonicalJson((a??null) as JsonValue)===canonicalJson((b??null) as JsonValue); const messageLabels:Record={regionLabel:'Region name',heading:'Heading',metadata:'Document metadata',draft:'Preview note',recorded:'Saved status',submitting:'Saving status',stale:'Changed terms',retired:'Closed workflow',unavailable:'Browser unavailable',error:'Recording failure'}; const fields=[['statement','Agreement statement'],['actionLabel','Button label'],['revision','Wording revision'],['reviewStatus','Declared review']] as const; const css=`.terms-history{max-width:1080px}.terms-history .intro{max-width:70ch}.terms-history-lead{margin-bottom:30px}.terms-history-panel{border-top:1px solid var(--border);padding:26px 0}.terms-history-panel>header{display:flex;gap:16px;align-items:baseline;justify-content:space-between;margin-bottom:20px}.terms-history-panel>header h2{margin:0;font-size:20px;letter-spacing:-.025em;overflow-wrap:anywhere}.terms-history-state{font-size:12px;color:var(--muted);white-space:nowrap}.terms-history-fields{display:grid;gap:20px}.terms-history-field{border-top:1px solid var(--border);padding-top:16px}.terms-history-field h3{font-size:12px;color:var(--muted);margin:0 0 10px}.terms-history-pair{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:20px}.terms-history-pair>div{min-width:0}.terms-history-pair dt{font-size:11px;color:var(--muted);margin-bottom:7px}.terms-history-pair dd{margin:0;white-space:pre-wrap;overflow-wrap:anywhere;line-height:1.65}.terms-history-pair dl{margin:0}.terms-history-statement{font-size:17px;letter-spacing:-.015em}.terms-history-reference{font-family:ui-monospace,monospace;font-size:12px;overflow-wrap:anywhere}.terms-history details{margin:18px 0}.terms-history summary{min-height:44px;padding:12px 0;cursor:pointer;overflow-wrap:anywhere}.terms-history>details.terms-history-meta{padding:0;border:0;background:none}.terms-history-meta{font-size:13px;color:var(--muted);line-height:1.7}.terms-history-compare{display:flex;gap:12px;flex-wrap:wrap;align-items:end;margin-top:24px}.terms-history-compare>div{flex:1;min-width:min(100%,260px)}.terms-history-compare label{display:block;font-size:12px;margin-bottom:8px}.terms-history-compare input{box-sizing:border-box;width:100%;min-height:44px}.terms-history-compare button{min-height:44px}.terms-history pre{overflow:auto;white-space:pre-wrap;overflow-wrap:anywhere}.terms-history a{overflow-wrap:anywhere}.terms-history :focus-visible{outline:3px solid var(--accent,#675394);outline-offset:3px}@media(max-width:600px){.terms-history-pair{grid-template-columns:minmax(0,1fr);gap:16px}.terms-history-panel>header{align-items:start;flex-direction:column;gap:7px}.terms-history-panel{padding:22px 0}.terms-history-statement{font-size:16px}}@media(forced-colors:active){.terms-history-panel,.terms-history-field{border-color:CanvasText}}`; function inspect(record:EvidenceRecord):TermsSettingsRelease { const value=normalizeTermsSettingsRelease(record.data);if(value.artifactId!==record.id)throw Error('Invalid settings reference.');return value; } const language=(v:TermsSettingsRelease)=>` lang="${e(v.locale)}" dir="${v.localization?.direction??'auto'}"`; function row(label:string,a:string|undefined,b:string|undefined,before:TermsSettingsRelease,after:TermsSettingsRelease|undefined,copy=false){ const cell=(value:string|undefined,release:TermsSettingsRelease,heading:string)=>`
${heading}
${e(value??'Not declared')}
`; return `

${e(label)}

${cell(a,before,after?'Before':'Release')}${after?cell(b,after,'After'):''}
`; } function declaration(a:TermsAgreementSetting|undefined,b:TermsAgreementSetting|undefined,before:TermsSettingsRelease,after:TermsSettingsRelease|undefined,onlyChanges=false){ return fields.filter(([key])=>!onlyChanges||!same(a?.[key],b?.[key])).map(([key,label])=>row(label,a?.[key],b?.[key],before,after,key==='statement'||key==='actionLabel')).join(''); } function reference(release:TermsSettingsRelease,label:string,basePath:string){ const d=release.document;return `

${label}

${e(d.title)} · ${e(d.version)}

Effective ${e(d.effectiveDate)} · ${d.reviewStatus==='reviewed'?'Host-declared reviewed':'Draft'} · ${e(release.locale)}

${e(release.artifactId.slice(0,16))}…
Document hashes and path
HTML SHA-256
${e(d.sha256)}
Content SHA-256
${e(d.contentSha256)}
Pinned path
${e(d.url)}
`; } function interfaceCopy(before:TermsSettingsRelease,after:TermsSettingsRelease|undefined){ const a=before.localization,b=after?.localization,changed=!!after&&(!same(a,b)||before.locale!==after.locale); const note=(v:TermsSettingsRelease,label:string)=>v.localization?'':`

${label}: no versioned interface dictionary retained.${/^en(?:-|$)/iu.test(v.locale)?' Historical built-in messages are not reconstructed.':' Configured native acceptance needs a complete matching terms dictionary.'}

`; const rows:[string,string|undefined,string|undefined][]=[['Site language',before.locale,after?.locale],...(['locale','direction','revision','reviewStatus'] as const).map(key=>[key==='locale'?'Dictionary language':key==='direction'?'Text direction':key==='revision'?'Interface revision':'Interface review',a?.[key],b?.[key]] as [string,string|undefined,string|undefined]),...Object.keys(a?.terms??b?.terms??{}).map(key=>['Message / '+(messageLabels[key]??key),a?.terms[key as keyof typeof a.terms],b?.terms[key as keyof typeof b.terms]] as [string,string|undefined,string|undefined])]; const body=note(before,after?'Before':'Release')+(after?note(after,'After'):'')+rows.filter(([,x,y])=>after?!same(x,y):x!==undefined).map(([label,x,y])=>row(label,x,y,before,after,label.startsWith('Message /'))).join(''); return `

Interface language and messages

${after?changed?'Changed':'Unchanged':'Retained copy'}
${body||'

No interface fields changed.

'}
`; } /** Readable archive comparison. It does not read active offers or infer presentation/acceptance. */ export function renderTermsSettings(beforeRecord:EvidenceRecord,afterRecord:EvidenceRecord|undefined,options:RenderOptions):string { const before=inspect(beforeRecord),after=afterRecord?inspect(afterRecord):undefined;if(after&&before.siteId!==after.siteId)throw Error('Compare releases for the same site.'); const previous=new Map(before.agreements.map(v=>[v.scopeId,v])),next=new Map(after?.agreements.map(v=>[v.scopeId,v])??[]),ids=[...new Set([...previous.keys(),...next.keys()])].sort();let changed=0; const workflows=ids.map(id=>{const a=previous.get(id),b=next.get(id),different=!!after&&!same(a,b);if(different)changed++;const status=!after?'Configured workflow':!a?'Added':!b?'Removed':different?'Changed':'Declaration unchanged'; const body=`

${e(id)}

${status}
${declaration(a,b,before,after,!!after)}
${after&&a&&b&&different?`
All declaration fields${declaration(a,b,before,after)}
`:''}
`; return after&&!different?`
${e(id)} · declaration unchanged${declaration(a,b,before,after)}
`:body; }).join(''); const declarationNote=(r:TermsSettingsRelease,label:string)=>r.declaration==='not-declared'?`

${label}: no configured agreement declarations retained. Application-authored offers may exist; this does not establish that the site has no agreement flow.

`:''; const current=after??before,form=after?'':`
`; const documentNote=after?`${before.document.sha256===after.document.sha256?'Pinned terms HTML unchanged.':'Pinned terms HTML changed.'} ${before.document.contentSha256===after.document.contentSha256?'Structured document content unchanged.':'Structured document content changed.'}`:'The document reference retained with these settings.'; return renderWorkspaceShell(`
← Back to release

Archived agreement wording

${after?'Agreement wording changes':'Terms agreement settings'}

Authorized scope: ${e(options.scopeLabel)}

${after?`${changed} of ${ids.length} workflows changed.`:`${ids.length} workflow${ids.length===1?'':'s'} retained.`} Compare the agreement statement, button label and wording revision.

What this history shows

These are archived settings, not active backend selections or evidence that anyone saw or accepted them. Unchanged declarations can still belong to different offers when the document, interface or release changes. Removing settings does not retire a backend offer.

${declarationNote(before,after?'Before':'Release')}${after?declarationNote(after,'After'):''}${workflows}

Terms document

${documentNote}

${reference(before,after?'Before':'Release',options.basePath)}${after?reference(after,'After',options.basePath):''}
${after?`Compare document sections →`:''}
${interfaceCopy(before,after)}${form}
`,options,``); }