import { generateCredentialId } from '../utils/id.js';
import { getRootLogger } from '../utils/logger.js';
export class InMemoryCredentialVault {
    refs = new Map();
    secrets = new Map();
    log;
    constructor() {
        this.log = getRootLogger().child({ component: 'InMemoryCredentialVault' });
    }
    async store(tenantId, connectorId, label, auth) {
        const id = generateCredentialId();
        const ref = {
            id,
            connectorId,
            tenantId,
            label,
            authType: auth.type,
            createdAt: Date.now(),
        };
        this.refs.set(id, ref);
        this.secrets.set(id, auth);
        this.log.info(`Credential stored: ${id} (${label}) for tenant ${tenantId}`);
        return ref;
    }
    async retrieve(credentialId) {
        return this.secrets.get(credentialId);
    }
    async revoke(credentialId) {
        const existed = this.refs.has(credentialId);
        this.refs.delete(credentialId);
        this.secrets.delete(credentialId);
        if (existed) {
            this.log.info(`Credential revoked: ${credentialId}`);
        }
        return existed;
    }
    async list(tenantId, connectorId) {
        const results = [];
        for (const ref of this.refs.values()) {
            if (ref.tenantId !== tenantId)
                continue;
            if (connectorId && ref.connectorId !== connectorId)
                continue;
            results.push(ref);
        }
        return results;
    }
}
//# sourceMappingURL=InMemoryCredentialVault.js.map