// @requirements REQ-PRODUCT-CONTRACT-ACTIONS REQ-PRODUCT-DEMO-WORKSPACE import {createContractAction,mountContractAction,ContractActionError} from '/assets/runtime/contract-action.js'; const container=document.getElementById('contract-action'),route=document.querySelector('[data-contract-route]')?.dataset.contractRoute; try{ const generation=document.querySelector('meta[name="notabenda-demo-generation"]')?.content,operationRef=document.querySelector('meta[name="notabenda-contract-operation"]')?.content; if(!/^[a-f0-9]{64}$/u.test(generation??'')||!/^[a-f0-9]{64}$/u.test(operationRef??'')||!['withdrawal','cancellation'].includes(route))throw Error('Demo context unavailable.'); const response=await fetch('/contract-actions/'+route+'/definition.json');if(!response.ok)throw Error('Definition unavailable.');let definition=await response.json(); const recoveryKey='notabenda-demo-contract:'+generation+':'+route;let recovery,storageWarning;const saved=sessionStorage.getItem(recoveryKey);if(saved){const retained=JSON.parse(saved);definition=retained.definition;recovery=retained.request;if(!definition||!recovery)throw Error('Retained request is unavailable.');} const initial={name:'Avery Linden',contract:'Juniper Notes · annual plan · sample 24017',email:'avery@example.test',...(route==='cancellation'?{cancellation:{kind:'ordinary',reason:'',ending:''}}:{})}; const action=createContractAction(definition,{operationRef,allowPreview:true,...(recovery?{recovery}:{initial}),async submit(body,{signal}){try{sessionStorage.setItem(recoveryKey,JSON.stringify({definition,request:body}));storageWarning?.remove();storageWarning=undefined;}catch{storageWarning??=document.createElement('p');storageWarning.className='contract-example-note';storageWarning.setAttribute('role','status');storageWarning.textContent='This tab could not keep a recovery copy. No recording request was sent. Free browser storage and retry, or use another contact route.';container.before(storageWarning);throw Error('Recovery copy unavailable.');}const response=await fetch('/demo/contracts/'+route,{method:'POST',headers:{'Content-Type':'application/json','X-Notabenda-Demo':'1','X-Notabenda-Demo-Generation':generation},body:JSON.stringify(body),signal});if(response.status===410)throw new ContractActionError('context-retired');if(!response.ok)throw Error('Recording could not be confirmed.');return response.json();}}); container.replaceChildren();const mounted=mountContractAction(container,{action}),email=container.querySelector('input[name=email]');if(recovery){const note=document.createElement('p');note.className='contract-example-note';note.textContent='This tab retained a previous submission. Retry checks the same request; it does not create a second declaration.';container.before(note);}email.addEventListener('input',()=>email.setCustomValidity(/@[^@\s]+\.test$/u.test(email.value)?'':'Use an example.test address in this demonstration.')); action.subscribe(state=>{const link=document.getElementById('contract-record');link.hidden=!state.receipt&&!recovery;if(state.receipt)link.href='/demo/admin/contracts/record?'+new URLSearchParams({id:state.receipt.id});else if(recovery){link.textContent='Check this request in your demo workspace ↗';link.href='/demo/admin/contracts?'+new URLSearchParams({q:recovery.requestId});}}); addEventListener('pagehide',event=>{if(!event.persisted){mounted.destroy();action.destroy();}}); }catch{container.replaceChildren();const error=document.createElement('p');error.setAttribute('role','status');error.textContent='The form could not start. Reload or return to the examples. No declaration has been sent from this page.';container.append(error);}