import {
  forbidden
} from "./chunk-EZYKRG4W.js";

// src/license/index.ts
var COMMUNITY_LIMITS = {
  /** Max virtual API keys (excluding the seed key). */
  maxKeys: 20,
  /** Audit log read retention in milliseconds (7 days). */
  auditRetentionMs: 7 * 24 * 60 * 60 * 1e3
};
var g = globalThis;
function getState() {
  if (g.__caLicense) return g.__caLicense;
  validateLicense();
  return g.__caLicense;
}
function validateLicense() {
  const raw = process.env.AGENTSTEP_LICENSE_KEY;
  if (!raw || !raw.trim()) {
    g.__caLicense = {
      plan: "community",
      features: [],
      validatedAt: Date.now(),
      expiresAt: null
    };
    return;
  }
  const allFeatures = [
    // "tenancy" — hidden for launch; ship when ready
    "budgets",
    "upstream_pool",
    "redis_rate_limit",
    "per_key_analytics",
    "unlimited_keys",
    "unlimited_audit"
  ];
  g.__caLicense = {
    plan: "enterprise",
    features: allFeatures,
    validatedAt: Date.now(),
    expiresAt: null
  };
  console.log(`[license] enterprise license activated (${allFeatures.length} features)`);
}
function isEnterprise() {
  return getState().plan === "enterprise";
}
function hasFeature(feature) {
  const state = getState();
  if (state.plan === "enterprise") return true;
  return state.features.includes(feature);
}
function requireFeature(feature, friendlyName) {
  if (process.env.DISABLE_EXPERIMENTAL_FEATURES === "1") {
    const name2 = friendlyName ?? feature.replace(/_/g, " ");
    throw forbidden(`${name2} is temporarily disabled (DISABLE_EXPERIMENTAL_FEATURES=1)`);
  }
  if (hasFeature(feature)) return;
  const name = friendlyName ?? feature.replace(/_/g, " ");
  throw forbidden(
    `${name} requires an AgentStep Enterprise license. Set AGENTSTEP_LICENSE_KEY in your environment, or see https://agentstep.com/pricing`
  );
}
function getLicenseInfo() {
  const state = getState();
  return {
    plan: state.plan,
    features: state.plan === "enterprise" ? ["tenancy", "budgets", "upstream_pool", "redis_rate_limit", "per_key_analytics", "unlimited_keys", "unlimited_audit"] : [],
    limits: state.plan === "community" ? COMMUNITY_LIMITS : null
  };
}
function _setLicenseForTesting(plan) {
  if (plan === "enterprise") {
    g.__caLicense = {
      plan: "enterprise",
      features: ["tenancy", "budgets", "upstream_pool", "redis_rate_limit", "per_key_analytics", "unlimited_keys", "unlimited_audit"],
      validatedAt: Date.now(),
      expiresAt: null
    };
  } else {
    g.__caLicense = {
      plan: "community",
      features: [],
      validatedAt: Date.now(),
      expiresAt: null
    };
  }
}

export {
  COMMUNITY_LIMITS,
  validateLicense,
  isEnterprise,
  hasFeature,
  requireFeature,
  getLicenseInfo,
  _setLicenseForTesting
};
