(function(r){
  function transaction({controls=[],status}) {
    let busy=false, disabled=[];
    async function run(message, action, {origin,failure}={}) {
      if (busy) return false;
      busy=true;
      disabled=controls.map(control=>control.disabled);
      controls.forEach(control=>{ control.disabled=true; });
      status.textContent=message;
      let completed=false;
      try { completed=Boolean(await action()); }
      catch (error) { failure=failure || error.message || 'Could not complete decision. Retry.'; }
      if (!completed && failure) status.textContent=failure;
      busy=false;
      controls.forEach((control,index)=>{ control.disabled=disabled[index]; });
      if (!completed) origin?.focus?.();
      return completed;
    }
    return {run,busy:()=>busy};
  }
  function actions(o) {
    return {
      keepUnread:()=>o.transaction.run('Keeping unread…',()=>{
        if(o.triage.active()) o.triage.keepUnreadAndNext(); else o.close();
        return true;
      },{origin:o.keepControl,failure:'Could not keep this update unread. Retry.'}),
      markRead:()=>o.transaction.run('Marking read…',async()=>{
        const result=await o.reader.markReadAndNext(o.items());
        if(!result) return false;
        o.undo.offer(result.item,result.items);
        if(o.triage.active()) o.triage.acceptCompleted();
        return true;
      },{origin:o.readControl,failure:'Could not mark update read. Retry.'}),
    };
  }
  if(typeof module==='object'&&module.exports){module.exports=transaction;module.exports.createActions=actions;}
  else {r.createUpdateDecisionTransaction=transaction;r.createUpdateDecisionActions=actions;}
})(typeof self!=='undefined'?self:this);
