import{ok,err}from"./_deps/shared/index.js";export var ApprovalErrorCode;!function(e){e.USER_DENIED="APPROVAL_USER_DENIED",e.TIMEOUT="APPROVAL_TIMEOUT",e.INVALID_DURATION="APPROVAL_INVALID_DURATION",e.SIGNATURE_FAILED="APPROVAL_SIGNATURE_FAILED",e.TOKEN_EXPIRED="APPROVAL_TOKEN_EXPIRED",e.TOKEN_REVOKED="APPROVAL_TOKEN_REVOKED",e.INVALID_TOKEN="APPROVAL_INVALID_TOKEN"}(ApprovalErrorCode||(ApprovalErrorCode={}));export class ApprovalError extends Error{code;details;constructor(e,r,o){super(r),this.code=e,this.details=o,this.name="ApprovalError"}}export class CLIApprovalPresenter{async present(e){console.log("\n=== AGENT APPROVAL REQUEST ==="),console.log(`Agent: ${e.agentDid}`),console.log(`Duration: ${e.duration}`),console.log("\nRequested Scopes:");for(const r of e.scopes)console.log(`  - ${r}`);return e.limits&&(console.log("\nPolicy Limits:"),e.limits.amountPerTxn&&console.log(`  - Max per transaction: $${e.limits.amountPerTxn.toLocaleString()}`),e.limits.dailyAmount&&console.log(`  - Daily limit: $${e.limits.dailyAmount.toLocaleString()}`),e.limits.callsPerMinute&&console.log(`  - Rate limit: ${e.limits.callsPerMinute} calls/minute`)),e.description&&console.log(`\nDescription: ${e.description}`),console.log("\n[This is a mock presenter - auto-approving for development]"),console.log("In production, this would prompt for user input.\n"),ok({approved:!0})}}export class ApprovalFlow{presenter;tokens=new Map;constructor(e={}){this.presenter=e.presenter??new CLIApprovalPresenter}async requestApproval(e){const r=this.parseDuration(e.duration);if(r<=0)return err(new ApprovalError(ApprovalErrorCode.INVALID_DURATION,`Invalid duration: ${e.duration}`));const o=await this.presenter.present(e);if(!o.ok)return o;if(!o.value.approved)return ok({approved:!1,reason:o.value.reason??"User denied approval"});const t=Date.now(),n={id:this.generateTokenId(),agentDid:e.agentDid,scopes:e.scopes,limits:e.limits,expiresAt:t+r,createdAt:t,valid:!0,signature:await this.signToken(e.agentDid,e.scopes,t+r)};return this.tokens.set(n.id,n),ok({approved:!0,token:n})}verifyToken(e){const r=this.tokens.get(e);return r?r.valid?Date.now()>r.expiresAt?err(new ApprovalError(ApprovalErrorCode.TOKEN_EXPIRED,`Token ${e} expired at ${new Date(r.expiresAt).toISOString()}`)):ok(r):err(new ApprovalError(ApprovalErrorCode.TOKEN_REVOKED,`Token ${e} has been revoked`)):err(new ApprovalError(ApprovalErrorCode.INVALID_TOKEN,`Token ${e} not found`))}revokeToken(e){const r=this.tokens.get(e);r&&this.tokens.set(e,{...r,valid:!1})}parseDuration(e){if("number"==typeof e)return e;const r=e.match(/^(\d+)([smhd])$/);if(!r)return-1;const o=parseInt(r[1]??"0",10);switch(r[2]){case"s":return 1e3*o;case"m":return 60*o*1e3;case"h":return 60*o*60*1e3;case"d":return 24*o*60*60*1e3;default:return-1}}generateTokenId(){const e=new Uint8Array(12);crypto.getRandomValues(e);const r=Array.from(e,e=>e.toString(16).padStart(2,"0")).join("");return`appr_${Date.now()}_${r}`}async signToken(e,r,o){const t=JSON.stringify({agentDid:e,scopes:r,expiresAt:o});return btoa(t).substring(0,32)}}