// ══════════════════════════════════════════════════════════════
// The Agent Social Contract — High-Level API
// ══════════════════════════════════════════════════════════════
//
// Everything below is one function call.
//
// To JOIN the social contract:
//   const agent = joinSocialContract({ name, mission, ... })
//
// To VERIFY another agent:
//   const trust = verifySocialContract(agent.passport)
//
// To RECORD work:
//   const receipt = recordWork(agent, { action, result })
//
// To PROVE your contributions:
//   const proof = proveContribution(agent, receipts)
//
// That's it. Four functions. The rest is implementation detail.
import { createPassport } from './core/passport.js';
import { verifyPassport } from './verification/verify.js';
import { attestFloor, verifyAttestation, loadFloor, evaluateCompliance } from './core/values.js';
import { createDelegation, createReceipt } from './core/delegation.js';
/**
 * Join the Agent Social Contract.
 *
 * One call. Creates identity, attests to values, registers beneficiary.
 * Returns everything an agent needs to participate.
 */
export function joinSocialContract(opts) {
    const agentId = `agent-${opts.name.toLowerCase().replace(/[^a-z0-9]/g, '-')}-${Date.now().toString(36)}`;
    const { signedPassport, keyPair } = createPassport({
        agentId,
        agentName: opts.name,
        ownerAlias: opts.owner,
        mission: opts.mission,
        capabilities: opts.capabilities,
        runtime: {
            platform: opts.platform,
            models: opts.models,
            toolsCount: opts.capabilities.length,
            memoryType: 'persistent'
        },
        beneficiary: opts.beneficiary ? {
            principalId: opts.beneficiary.id,
            relationship: opts.beneficiary.relationship,
            registeredAt: new Date().toISOString()
        } : undefined,
        validityWindow: opts.validityWindow,
    });
    // Attest to floor if provided
    let attestation = null;
    if (opts.floor) {
        const floor = typeof opts.floor === 'string' ? loadFloor(opts.floor) : opts.floor;
        attestation = attestFloor(agentId, signedPassport.passport.publicKey, floor.version, opts.floorExtensions || [], keyPair.privateKey);
    }
    return {
        passport: signedPassport,
        keyPair,
        attestation,
        agentId,
        publicKey: signedPassport.passport.publicKey
    };
}
/**
 * Verify another agent's standing in the social contract.
 *
 * One call. Checks identity, attestation, gives you a trust decision.
 */
export function verifySocialContract(passport, attestation) {
    const identity = verifyPassport(passport);
    let values = null;
    if (attestation) {
        const attResult = verifyAttestation(attestation);
        values = {
            attested: true,
            valid: attResult.valid,
            errors: attResult.errors
        };
    }
    return {
        identity: { valid: identity.valid, errors: identity.errors },
        values,
        overall: identity.valid && (!values || values.valid)
    };
}
/**
 * Delegate authority from one agent (or human) to another.
 */
export function delegate(opts) {
    return createDelegation({
        delegatedTo: opts.toPublicKey,
        delegatedBy: opts.from.publicKey,
        scope: opts.scope,
        spendLimit: opts.spendLimit,
        maxDepth: opts.maxDepth ?? 1,
        expiresInHours: opts.expiresInHours ?? 24,
        privateKey: opts.from.keyPair.privateKey
    });
}
/**
 * Record a unit of work under a delegation.
 *
 * Returns a signed, verifiable receipt.
 */
export function recordWork(agent, delegation, delegationChain, work) {
    return createReceipt({
        agentId: agent.agentId,
        delegationId: delegation.delegationId,
        delegation,
        action: {
            type: work.type,
            target: work.target,
            scopeUsed: work.scope,
            spend: work.spend ? { amount: work.spend, currency: work.currency || 'USD' } : undefined
        },
        result: { status: work.result, summary: work.summary },
        delegationChain,
        privateKey: agent.keyPair.privateKey
    });
}
/**
 * Generate cryptographic proof of an agent's contributions.
 *
 * Moved to @aeoess/gateway (attribution-reports) because proof generation
 * depends on scope-weight product policy. See MIGRATION.md#attribution-reports.
 */
export function proveContributions(_agent, _receipts, _delegations, _beneficiary, _beneficiaryMap) {
    throw new Error('Moved to @aeoess/gateway. See MIGRATION.md#attribution-reports');
}
// ══════════════════════════════════════
// AUDIT — Check compliance against the Floor
// ══════════════════════════════════════
/**
 * Audit an agent's compliance. Requires a verifier keypair
 * (the auditor signs the report).
 */
export function auditCompliance(agentId, receipts, floor, delegations, verifierKeyPair) {
    return evaluateCompliance(agentId, receipts, floor, delegations, verifierKeyPair.privateKey);
}
//# sourceMappingURL=contract.js.map