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