"use strict";
/**
 * Platform Detector for CLIProxyAPI Binary Downloads
 *
 * Detects OS and architecture to determine correct binary asset.
 * Supports 6 platforms: darwin/linux/windows x amd64/arm64
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPlatformDescription = exports.isPlatformSupported = exports.getFallbackVersion = exports.getChecksumsUrl = exports.getDownloadUrl = exports.getArchiveBinaryName = exports.getExecutableName = exports.detectPlatform = exports.mapNodeArchToReleaseArch = exports.CLIPROXY_VERSION = exports.CLIPROXY_FAULTY_RANGE = exports.CLIPROXY_MAX_STABLE_VERSION = exports.CLIPROXY_FALLBACK_VERSION = exports.DEFAULT_BACKEND = exports.BACKEND_CONFIG = void 0;
/** Backend configuration */
exports.BACKEND_CONFIG = {
    original: {
        repo: 'router-for-me/CLIProxyAPI',
        binaryPrefix: 'CLIProxyAPI',
        executable: 'cli-proxy-api',
        fallbackVersion: '6.9.45',
    },
    plus: {
        repo: 'kaitranntt/CLIProxyAPIPlus',
        binaryPrefix: 'CLIProxyAPIPlus',
        executable: 'cli-proxy-api-plus',
        fallbackVersion: '6.9.45-0',
    },
};
/**
 * Default backend
 *
 * Keep 'original' as the stable default. The Plus backend is available as an
 * opt-in community-maintained fork for providers that are not available in the
 * original upstream binary.
 */
exports.DEFAULT_BACKEND = 'original';
/**
 * CLIProxyAPIPlus fallback version (used when GitHub API unavailable)
 * Auto-update fetches latest from GitHub; this is only a safety net
 * Note: CLIProxyAPIPlus uses v6.6.X-0 suffix pattern
 * @deprecated Use getFallbackVersion() or BACKEND_CONFIG instead
 */
exports.CLIPROXY_FALLBACK_VERSION = exports.BACKEND_CONFIG[exports.DEFAULT_BACKEND].fallbackVersion;
/**
 * Maximum stable version cap - prevents auto-update to known unstable releases
 * Currently set high since v89+ are all stable.
 * Only v81-88 have known bugs (see CLIPROXY_FAULTY_RANGE).
 * See: https://github.com/kaitranntt/ccs/issues/269
 */
exports.CLIPROXY_MAX_STABLE_VERSION = '9.9.999-0';
/**
 * Faulty version range - versions with known critical bugs
 * v81-88 have context cancellation bugs causing intermittent 500 errors
 * v89 confirmed stable
 */
exports.CLIPROXY_FAULTY_RANGE = { min: '6.6.81-0', max: '6.6.88-0' };
/** @deprecated Use CLIPROXY_FALLBACK_VERSION instead */
exports.CLIPROXY_VERSION = exports.CLIPROXY_FALLBACK_VERSION;
/** Platform mapping from Node.js values to CCS public architecture labels. */
const OS_MAP = {
    darwin: 'darwin',
    linux: 'linux',
    win32: 'windows',
};
const ARCH_MAP = {
    x64: 'amd64',
    arm64: 'arm64',
};
/** CLIProxy release assets use aarch64 while Node.js reports arm64. */
const RELEASE_ARCH_MAP = {
    amd64: 'amd64',
    arm64: 'aarch64',
    aarch64: 'aarch64',
};
function mapNodeArchToReleaseArch(nodeArch) {
    const arch = ARCH_MAP[nodeArch];
    return arch ? RELEASE_ARCH_MAP[arch] : undefined;
}
exports.mapNodeArchToReleaseArch = mapNodeArchToReleaseArch;
/**
 * Detect current platform and return binary info
 * @param version Optional version for binaryName (defaults to backend fallback)
 * @param backend Backend variant to use (defaults to DEFAULT_BACKEND)
 * @throws Error if platform is unsupported
 */
function detectPlatform(version, backend = exports.DEFAULT_BACKEND) {
    const nodePlatform = process.platform;
    const nodeArch = process.arch;
    const os = OS_MAP[nodePlatform];
    const arch = ARCH_MAP[nodeArch];
    const releaseArch = mapNodeArchToReleaseArch(nodeArch);
    if (!os) {
        throw new Error(`Unsupported operating system: ${nodePlatform}\n` +
            `Supported: macOS (darwin), Linux, Windows`);
    }
    if (!arch || !releaseArch) {
        throw new Error(`Unsupported CPU architecture: ${nodeArch}\n` + `Supported: x64 (amd64), arm64 (aarch64)`);
    }
    const config = exports.BACKEND_CONFIG[backend];
    const ver = version || config.fallbackVersion;
    const extension = os === 'windows' ? 'zip' : 'tar.gz';
    const binaryName = `${config.binaryPrefix}_${ver}_${os}_${releaseArch}.${extension}`;
    return {
        os,
        arch,
        binaryName,
        extension,
    };
}
exports.detectPlatform = detectPlatform;
/**
 * Get executable name based on platform
 * @param backend Backend variant to use (defaults to DEFAULT_BACKEND)
 * @returns Binary executable name (with .exe on Windows)
 */
function getExecutableName(backend = exports.DEFAULT_BACKEND) {
    const config = exports.BACKEND_CONFIG[backend];
    const platform = detectPlatform(undefined, backend);
    return platform.os === 'windows' ? `${config.executable}.exe` : config.executable;
}
exports.getExecutableName = getExecutableName;
/**
 * Get the name of the binary inside the archive
 * @param backend Backend variant to use (defaults to DEFAULT_BACKEND)
 * @returns Binary name as it appears in the tar.gz/zip
 */
function getArchiveBinaryName(backend = exports.DEFAULT_BACKEND) {
    return getExecutableName(backend);
}
exports.getArchiveBinaryName = getArchiveBinaryName;
/**
 * Get download URL for current platform
 * @param version Version to download (defaults to backend fallback version)
 * @param backend Backend variant to use (defaults to DEFAULT_BACKEND)
 * @returns Full GitHub release download URL
 */
function getDownloadUrl(version, backend = exports.DEFAULT_BACKEND) {
    const config = exports.BACKEND_CONFIG[backend];
    const ver = version || config.fallbackVersion;
    const platform = detectPlatform(ver, backend);
    return `https://github.com/${config.repo}/releases/download/v${ver}/${platform.binaryName}`;
}
exports.getDownloadUrl = getDownloadUrl;
/**
 * Get checksums.txt URL for version
 * @param version Version to get checksums for (defaults to backend fallback version)
 * @param backend Backend variant to use (defaults to DEFAULT_BACKEND)
 * @returns Full URL to checksums.txt
 */
function getChecksumsUrl(version, backend = exports.DEFAULT_BACKEND) {
    const config = exports.BACKEND_CONFIG[backend];
    const ver = version || config.fallbackVersion;
    return `https://github.com/${config.repo}/releases/download/v${ver}/checksums.txt`;
}
exports.getChecksumsUrl = getChecksumsUrl;
/**
 * Get fallback version for backend
 * @param backend Backend variant to use (defaults to DEFAULT_BACKEND)
 * @returns Fallback version string
 */
function getFallbackVersion(backend = exports.DEFAULT_BACKEND) {
    return exports.BACKEND_CONFIG[backend].fallbackVersion;
}
exports.getFallbackVersion = getFallbackVersion;
/**
 * Check if platform is supported
 * @returns true if current platform is supported
 */
function isPlatformSupported() {
    try {
        detectPlatform();
        return true;
    }
    catch {
        return false;
    }
}
exports.isPlatformSupported = isPlatformSupported;
/**
 * Get human-readable platform description
 * @returns Description string (e.g., "macOS ARM64")
 */
function getPlatformDescription() {
    try {
        const platform = detectPlatform();
        const osName = platform.os === 'darwin' ? 'macOS' : platform.os === 'linux' ? 'Linux' : 'Windows';
        const archName = platform.arch === 'arm64' || platform.arch === 'aarch64' ? 'ARM64' : platform.arch;
        return `${osName} ${archName}`;
    }
    catch {
        return `${process.platform} ${process.arch} (unsupported)`;
    }
}
exports.getPlatformDescription = getPlatformDescription;
//# sourceMappingURL=platform-detector.js.map