"use strict";
/**
 * API Validation Service
 *
 * Provides validation functions for API profile names and URLs.
 * Extracted from api-command.ts for reuse and testability.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.sanitizeBaseUrl = exports.getUrlWarning = exports.validateUrl = exports.validateApiName = void 0;
const reserved_names_1 = require("../../config/reserved-names");
/**
 * Validate API profile name
 * @returns Error message if invalid, null if valid
 */
function validateApiName(name) {
    if (!name) {
        return 'API name is required';
    }
    if (!/^[a-zA-Z][a-zA-Z0-9._-]*$/.test(name)) {
        return 'API name must start with letter, contain only letters, numbers, dot, dash, underscore';
    }
    if (name.length > 32) {
        return 'API name must be 32 characters or less';
    }
    if ((0, reserved_names_1.isReservedName)(name)) {
        return `'${name}' is a reserved name`;
    }
    if ((0, reserved_names_1.isWindowsReservedName)(name)) {
        return `'${name}' is a Windows reserved device name and cannot be used`;
    }
    return null;
}
exports.validateApiName = validateApiName;
/**
 * Validate URL format
 * @returns Error message if invalid, null if valid
 */
function validateUrl(url) {
    if (!url) {
        return 'Base URL is required';
    }
    try {
        new URL(url);
        return null;
    }
    catch {
        return 'Invalid URL format (must include protocol, e.g., https://)';
    }
}
exports.validateUrl = validateUrl;
/**
 * Check if URL looks like it includes endpoint path (common mistake)
 * @returns Warning message if problematic, null if OK
 */
function getUrlWarning(url) {
    const problematicPaths = ['/chat/completions', '/v1/messages', '/messages', '/completions'];
    const lowerUrl = url.toLowerCase();
    for (const pathSuffix of problematicPaths) {
        if (lowerUrl.endsWith(pathSuffix)) {
            return (`URL ends with "${pathSuffix}" - Claude appends this automatically.\n` +
                `    You likely want: ${url.replace(new RegExp(pathSuffix + '$', 'i'), '')}`);
        }
    }
    return null;
}
exports.getUrlWarning = getUrlWarning;
/**
 * Sanitize URL by removing common endpoint suffixes
 */
function sanitizeBaseUrl(url) {
    const suffixes = ['/chat/completions', '/v1/messages', '/messages', '/completions'];
    let sanitized = url;
    for (const suffix of suffixes) {
        const regex = new RegExp(suffix + '$', 'i');
        sanitized = sanitized.replace(regex, '');
    }
    return sanitized;
}
exports.sanitizeBaseUrl = sanitizeBaseUrl;
//# sourceMappingURL=validation-service.js.map