'use strict';

/* ────────────────────────────────────────────────────────────────
   EPIC E10 — Enterprise Features
   
   White-label editor, self-hosted, LDAP/SSO, audit logs,
   role-based access, custom branding
──────────────────────────────────────────────────────────────── */
const DEFAULT_BRANDING = {
    appName: 'Nice2Dev Game Engine',
    appLogo: '/assets/logo.svg',
    appIcon: '/assets/favicon.ico',
    companyName: 'Nice2Dev',
    primaryColor: '#2563eb',
    secondaryColor: '#1e40af',
    accentColor: '#f59e0b',
    backgroundColor: '#0f172a',
    textColor: '#e2e8f0',
    fontFamily: '"Inter", system-ui, sans-serif',
    borderRadius: 8,
    footerText: '© Nice2Dev',
};
function generateCSSVariables(branding) {
    return `:root {
  --brand-primary: ${branding.primaryColor};
  --brand-secondary: ${branding.secondaryColor};
  --brand-accent: ${branding.accentColor};
  --brand-bg: ${branding.backgroundColor};
  --brand-text: ${branding.textColor};
  --brand-font: ${branding.fontFamily};
  --brand-radius: ${branding.borderRadius}px;
}`;
}
const DEFAULT_PASSWORD_POLICY = {
    minLength: 8,
    maxLength: 128,
    requireUppercase: true,
    requireLowercase: true,
    requireDigit: true,
    requireSpecial: false,
    preventReuse: 5,
    expiryDays: 0,
};
function validatePassword(password, policy) {
    const errors = [];
    if (password.length < policy.minLength)
        errors.push(`Minimum length: ${policy.minLength}`);
    if (password.length > policy.maxLength)
        errors.push(`Maximum length: ${policy.maxLength}`);
    if (policy.requireUppercase && !/[A-Z]/.test(password))
        errors.push('Requires uppercase letter');
    if (policy.requireLowercase && !/[a-z]/.test(password))
        errors.push('Requires lowercase letter');
    if (policy.requireDigit && !/\d/.test(password))
        errors.push('Requires digit');
    if (policy.requireSpecial && !/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(password))
        errors.push('Requires special character');
    return errors;
}
const SYSTEM_ROLES = [
    {
        id: 'admin',
        name: 'Administrator',
        description: 'Full access to all features',
        permissions: ['admin.all'],
        isSystem: true,
    },
    {
        id: 'editor',
        name: 'Editor',
        description: 'Can create and edit projects',
        permissions: [
            'project.create', 'project.read', 'project.edit',
            'asset.upload', 'asset.edit',
            'settings.view', 'api.access',
        ],
        isSystem: true,
    },
    {
        id: 'viewer',
        name: 'Viewer',
        description: 'Read-only access',
        permissions: ['project.read', 'settings.view'],
        isSystem: true,
    },
    {
        id: 'moderator',
        name: 'Moderator',
        description: 'Can moderate content',
        permissions: [
            'project.read', 'asset.edit', 'asset.delete',
            'marketplace.moderate', 'audit.view',
        ],
        isSystem: true,
    },
];
function hasPermission(userPermissions, required) {
    if (userPermissions.indexOf('admin.all') !== -1)
        return true;
    return userPermissions.indexOf(required) !== -1;
}
function resolveRolePermissions(roleId, allRoles) {
    const role = allRoles.find(r => r.id === roleId);
    if (!role)
        return [];
    const perms = new Set(role.permissions);
    if (role.inherits) {
        for (const inheritId of role.inherits) {
            const inherited = resolveRolePermissions(inheritId, allRoles);
            for (const p of inherited)
                perms.add(p);
        }
    }
    const result = [];
    perms.forEach(p => result.push(p));
    return result;
}
class AuditLog {
    constructor(maxEntries = 100000) {
        this.entries = [];
        this.maxEntries = maxEntries;
    }
    log(entry) {
        const full = {
            ...entry,
            id: `audit_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
            timestamp: Date.now(),
        };
        this.entries.push(full);
        if (this.entries.length > this.maxEntries) {
            this.entries.shift();
        }
        return full;
    }
    query(q) {
        let filtered = this.entries;
        if (q.userId)
            filtered = filtered.filter(e => e.userId === q.userId);
        if (q.action)
            filtered = filtered.filter(e => e.action === q.action);
        if (q.resourceType)
            filtered = filtered.filter(e => e.resourceType === q.resourceType);
        if (q.resourceId)
            filtered = filtered.filter(e => e.resourceId === q.resourceId);
        if (q.severity)
            filtered = filtered.filter(e => e.severity === q.severity);
        if (q.startDate)
            filtered = filtered.filter(e => e.timestamp >= q.startDate);
        if (q.endDate)
            filtered = filtered.filter(e => e.timestamp <= q.endDate);
        const total = filtered.length;
        const start = (q.page - 1) * q.pageSize;
        const paged = filtered.slice(start, start + q.pageSize);
        return { entries: paged, total };
    }
    getRecent(count) {
        return this.entries.slice(-count);
    }
    getFailedLogins(since) {
        return this.entries.filter(e => e.action === 'login_failed' && e.timestamp >= since);
    }
    clear() {
        this.entries.length = 0;
    }
}
const PLAN_LIMITS = {
    free: {
        maxProjects: 3, maxMembers: 3, maxStorageGB: 1,
        customDomain: false, ssoEnabled: false, apiAccess: false,
        prioritySupport: false, sla: 'none', dataRetentionDays: 30,
    },
    starter: {
        maxProjects: 10, maxMembers: 10, maxStorageGB: 10,
        customDomain: false, ssoEnabled: false, apiAccess: true,
        prioritySupport: false, sla: 'none', dataRetentionDays: 90,
    },
    professional: {
        maxProjects: 50, maxMembers: 50, maxStorageGB: 100,
        customDomain: true, ssoEnabled: true, apiAccess: true,
        prioritySupport: true, sla: '99.9%', dataRetentionDays: 365,
    },
    enterprise: {
        maxProjects: -1, maxMembers: -1, maxStorageGB: -1,
        customDomain: true, ssoEnabled: true, apiAccess: true,
        prioritySupport: true, sla: '99.99%', dataRetentionDays: -1,
    },
};
const DEFAULT_RATE_LIMITS = {
    standard: {
        windowMs: 60000,
        maxRequests: 100,
        burstLimit: 20,
        slowDownAfter: 80,
        slowDownDelay: 500,
    },
    premium: {
        windowMs: 60000,
        maxRequests: 1000,
        burstLimit: 100,
        slowDownAfter: 800,
        slowDownDelay: 100,
    },
    enterprise: {
        windowMs: 60000,
        maxRequests: 10000,
        burstLimit: 500,
        slowDownAfter: 8000,
        slowDownDelay: 50,
    },
};
class RateLimiter {
    constructor(config) {
        this.windows = new Map();
        this.config = config;
    }
    tryConsume(key) {
        const now = Date.now();
        let window = this.windows.get(key);
        if (!window || now > window.resetAt) {
            window = { count: 0, resetAt: now + this.config.windowMs };
            this.windows.set(key, window);
        }
        if (window.count >= this.config.maxRequests) {
            return {
                allowed: false,
                remaining: 0,
                retryAfter: window.resetAt - now,
            };
        }
        window.count++;
        return {
            allowed: true,
            remaining: this.config.maxRequests - window.count,
        };
    }
    getRemainingRequests(key) {
        const window = this.windows.get(key);
        if (!window || Date.now() > window.resetAt)
            return this.config.maxRequests;
        return Math.max(0, this.config.maxRequests - window.count);
    }
    reset(key) {
        this.windows.delete(key);
    }
}

exports.AuditLog = AuditLog;
exports.DEFAULT_BRANDING = DEFAULT_BRANDING;
exports.DEFAULT_PASSWORD_POLICY = DEFAULT_PASSWORD_POLICY;
exports.DEFAULT_RATE_LIMITS = DEFAULT_RATE_LIMITS;
exports.PLAN_LIMITS = PLAN_LIMITS;
exports.RateLimiter = RateLimiter;
exports.SYSTEM_ROLES = SYSTEM_ROLES;
exports.generateCSSVariables = generateCSSVariables;
exports.hasPermission = hasPermission;
exports.resolveRolePermissions = resolveRolePermissions;
exports.validatePassword = validatePassword;
//# sourceMappingURL=Enterprise.js.map
