"use strict";
/**
 * Auto-Yes Configuration Constants
 *
 * Shared config for Auto-Yes duration settings and stop pattern validation.
 * Used by both server (auto-yes-manager.ts, route.ts) and client
 * (AutoYesConfirmDialog.tsx, AutoYesToggle.tsx) components.
 *
 * Issue #225: Duration selection feature
 * Issue #314: Stop condition (regex) validation
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MAX_STOP_PATTERN_LENGTH = exports.AUTO_STOP_ERROR_THRESHOLD = exports.FULL_CAPTURE_LINES = exports.REDUCED_CAPTURE_LINES = exports.THINKING_POLLING_INTERVAL_MS = exports.DURATION_LABELS = exports.DEFAULT_AUTO_YES_DURATION = exports.ALLOWED_DURATIONS = void 0;
exports.isAllowedDuration = isAllowedDuration;
exports.validateStopPattern = validateStopPattern;
exports.formatTimeRemaining = formatTimeRemaining;
const safe_regex2_1 = __importDefault(require("safe-regex2"));
/** Allowed Auto-Yes durations in milliseconds */
exports.ALLOWED_DURATIONS = [3600000, 10800000, 28800000];
/** Default Auto-Yes duration (1 hour = 3600000ms) */
exports.DEFAULT_AUTO_YES_DURATION = 3600000;
/** i18n translation keys for each duration value */
exports.DURATION_LABELS = {
    3600000: 'autoYes.durations.1h',
    10800000: 'autoYes.durations.3h',
    28800000: 'autoYes.durations.8h',
};
/**
 * Type guard: check whether a value is a valid AutoYesDuration.
 * Replaces `as AutoYesDuration` casts with a runtime-safe check.
 */
function isAllowedDuration(value) {
    return typeof value === 'number' && exports.ALLOWED_DURATIONS.includes(value);
}
// =============================================================================
// Polling Interval Constants (Issue #499)
// =============================================================================
/** Polling interval when Thinking state is detected (Issue #499 Item 2) */
exports.THINKING_POLLING_INTERVAL_MS = 5000;
/** Reduced capture lines when stopPattern is not set (Issue #499 Item 3) */
exports.REDUCED_CAPTURE_LINES = 300;
/** Full capture lines when stopPattern is set (Issue #499 Item 3) */
exports.FULL_CAPTURE_LINES = 5000;
// =============================================================================
// Error Threshold Constants (Issue #499)
// =============================================================================
/** Number of consecutive errors before auto-stopping auto-yes (Issue #499 Item 5) */
exports.AUTO_STOP_ERROR_THRESHOLD = 20;
// =============================================================================
// Stop Pattern Validation (Issue #314)
// =============================================================================
/** Maximum length for stop pattern (security: prevent excessive regex complexity) */
exports.MAX_STOP_PATTERN_LENGTH = 500;
/**
 * Validate a stop pattern (regular expression string).
 *
 * Security measures:
 * - Length limit (MAX_STOP_PATTERN_LENGTH)
 * - safe-regex2 for catastrophic backtracking detection (ReDoS prevention)
 * - RegExp constructor for syntax validation
 * - Error messages are fixed strings only (no error.message passthrough for XSS prevention)
 *
 * @param pattern - Regular expression pattern string to validate
 * @returns Validation result with fixed-string error messages
 */
function validateStopPattern(pattern) {
    if (pattern.length > exports.MAX_STOP_PATTERN_LENGTH) {
        return { valid: false, error: `Pattern must be ${exports.MAX_STOP_PATTERN_LENGTH} characters or less` };
    }
    // safe-regex2 detects catastrophic backtracking patterns (ReDoS prevention)
    if (!(0, safe_regex2_1.default)(pattern)) {
        return { valid: false, error: 'Pattern may cause performance issues (catastrophic backtracking detected)' };
    }
    try {
        new RegExp(pattern);
        return { valid: true };
    }
    catch {
        return { valid: false, error: 'Invalid regular expression syntax' };
    }
}
// =============================================================================
// Time Formatting
// =============================================================================
/** Milliseconds per second */
const MS_PER_SECOND = 1000;
/** Milliseconds per minute */
const MS_PER_MINUTE = 60000;
/** Milliseconds per hour */
const MS_PER_HOUR = 3600000;
/**
 * Format remaining time as MM:SS (under 1 hour) or H:MM:SS (1 hour or more).
 *
 * Extracted from AutoYesToggle.tsx for direct testability and reuse.
 * Negative remaining time is clamped to 0.
 */
function formatTimeRemaining(expiresAt) {
    const remaining = Math.max(0, expiresAt - Date.now());
    const hours = Math.floor(remaining / MS_PER_HOUR);
    const minutes = Math.floor((remaining % MS_PER_HOUR) / MS_PER_MINUTE);
    const seconds = Math.floor((remaining % MS_PER_MINUTE) / MS_PER_SECOND);
    if (hours > 0) {
        return `${hours}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
    }
    return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}
