// engine/license-validator/core.ts — Offline-first license validation logic.
// Source of truth: local license state. Remote validation only on activation + every 30 days.
import { loadLicensePlans } from './config-loader.js';
import { verifyMachineFingerprint } from './fingerprint.js';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const VALIDATION_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
const GRACE_PERIOD_MS = 3 * 24 * 60 * 60 * 1000; // 3 days offline grace
// ---------------------------------------------------------------------------
// Tier resolution
// ---------------------------------------------------------------------------
export function resolveCurrentTier(state) {
    if (!state) {
        return 'free';
    }
    if (!verifyMachineFingerprint(state.machineFingerprint)) {
        return 'free';
    }
    return state.tier;
}
// ---------------------------------------------------------------------------
// Variant → tier mapping
// ---------------------------------------------------------------------------
const VARIANT_MAP = {
    pro: 'pro',
    'planu pro': 'pro',
    'planu pro monthly': 'pro',
    'planu pro yearly': 'pro',
    team: 'team',
    'planu team': 'team',
    'planu team monthly': 'team',
    'planu team yearly': 'team',
    enterprise: 'enterprise',
    'planu enterprise': 'enterprise',
    'planu enterprise monthly': 'enterprise',
    'planu enterprise yearly': 'enterprise',
};
export function variantToTier(variantName) {
    const normalized = variantName.toLowerCase().trim();
    return VARIANT_MAP[normalized] ?? 'pro';
}
// ---------------------------------------------------------------------------
// Tool access
// ---------------------------------------------------------------------------
export function isToolAllowed(toolName, tier) {
    const config = loadLicensePlans();
    if (config.alwaysAllowed.includes(toolName)) {
        return true;
    }
    if (config.freeTools.includes(toolName)) {
        return true;
    }
    if (tier === 'free') {
        return false;
    }
    // Pro, team, enterprise have access to all tools
    return true;
}
export function checkToolAccess(toolName, tier) {
    const allowed = isToolAllowed(toolName, tier);
    if (allowed) {
        return { allowed: true, currentTier: tier, requiredTier: tier, message: 'Access granted.' };
    }
    const config = loadLicensePlans();
    const freeCount = config.freeTools.length + config.alwaysAllowed.length;
    const totalCount = freeCount + config.proTools.length;
    return {
        allowed: false,
        currentTier: tier,
        requiredTier: 'pro',
        message: [
            `🔒 "${toolName}" requires a Pro or higher plan.`,
            `You are currently on the Free plan (${String(freeCount)} tools available).`,
            `Pro plan includes unlimited projects and all ${String(totalCount)}+ tools.`,
            `Upgrade to Pro → ${config.upgradeUrl}`,
            '',
            'To activate a license key:',
            '  → Use the activate_license tool with your license key.',
        ].join('\n'),
    };
}
// ---------------------------------------------------------------------------
// Limits
// ---------------------------------------------------------------------------
export function getTierLimits(tier) {
    const config = loadLicensePlans();
    return config.tiers[tier];
}
export function checkLimits(tier, currentCount, limitType) {
    const limits = getTierLimits(tier);
    const max = limits[limitType];
    if (currentCount < max) {
        return { allowed: true, currentTier: tier, requiredTier: tier, message: 'Within limits.' };
    }
    const config = loadLicensePlans();
    const label = limitType === 'maxProjects' ? 'projects' : 'active specs';
    const proLimits = config.tiers.pro;
    const proMax = proLimits[limitType];
    const proLabel = proMax >= 9999 ? 'unlimited' : String(proMax);
    return {
        allowed: false,
        currentTier: tier,
        requiredTier: 'pro',
        message: [
            `🔒 ${label.charAt(0).toUpperCase() + label.slice(1)} limit reached (${String(currentCount)}/${String(max)} on free plan).`,
            `Upgrade to Pro for ${proLabel} ${label} and 85+ tools → ${config.upgradeUrl}`,
        ].join('\n'),
    };
}
// ---------------------------------------------------------------------------
// Validation freshness
// ---------------------------------------------------------------------------
export function isValidationFresh(state) {
    const lastValidated = new Date(state.lastValidatedAt).getTime();
    return Date.now() - lastValidated < VALIDATION_TTL_MS;
}
export function isInGracePeriod(state) {
    const lastValidated = new Date(state.lastValidatedAt).getTime();
    return Date.now() - lastValidated < VALIDATION_TTL_MS + GRACE_PERIOD_MS;
}
//# sourceMappingURL=core.js.map