"use strict";
/**
 * Entitlements — the single gate between a cached license and paid features.
 *
 * Design intent (open-core): the open-source CORE never calls `requireFeature`
 * to lock its own behavior — Community must stay 100% functional. This helper
 * exists so the PRIVATE `openlife-enterprise` code (which depends on
 * @openlife/cli) has one trustworthy place to ask "is this feature paid-for?".
 *
 * A feature is granted only when ALL hold:
 *   1. a license file exists,
 *   2. its Ed25519 signature verifies against the embedded public key,
 *   3. it has not expired ("em dia"),
 *   4. the resolved entitlements include the feature.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Entitlements = void 0;
const LicenseManager_1 = require("./LicenseManager");
const LicenseStore_1 = require("./LicenseStore");
class Entitlements {
    manager;
    store;
    constructor(root = process.cwd(), manager) {
        this.store = new LicenseStore_1.LicenseStore(root);
        this.manager = manager || new LicenseManager_1.LicenseManager();
    }
    /** Resolve the current, fully-validated entitlement state. */
    state(now = new Date()) {
        const token = this.store.current();
        if (!token)
            return { tier: 'community', features: [], expiresAt: null, status: 'none' };
        const verdict = this.manager.verifyToken(token);
        if (!verdict.valid) {
            return { tier: 'community', features: [], expiresAt: token.payload.expiresAt ?? null, status: 'invalid' };
        }
        if (!this.manager.isCurrent(token, now)) {
            return { tier: token.payload.tier, features: [], expiresAt: token.payload.expiresAt, status: 'expired' };
        }
        return {
            tier: token.payload.tier,
            features: this.manager.entitlements(token),
            expiresAt: token.payload.expiresAt,
            status: 'active',
        };
    }
    /** Non-throwing check used by paid code paths. */
    hasFeature(feature, now = new Date()) {
        return this.state(now).features.includes(feature);
    }
    /**
     * Throwing guard for paid code paths. The message points the user at the CLI
     * so the upsell is self-service.
     */
    requireFeature(feature, now = new Date()) {
        if (this.hasFeature(feature, now))
            return;
        throw new Error(`Feature "${feature}" requires OpenLife Business. ` +
            `Activate a license with: openlife license activate --key <YOUR-KEY> ` +
            `(check status with: openlife license status).`);
    }
}
exports.Entitlements = Entitlements;
