"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createClientHeaders = createClientHeaders;
exports.getCliVersionLabel = getCliVersionLabel;
exports.getCliVersion = getCliVersion;
exports.getCliBuild = getCliBuild;
exports.getRuntimeInfo = getRuntimeInfo;
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const fs_1 = require("fs");
const config_1 = require("@playdrop/config");
let cachedCliPackageVersion = null;
function assertValidCliVersion(version) {
    if (!/^\d+\.\d+\.\d+$/.test(version)) {
        throw new Error(`invalid_cli_package_version:${version}`);
    }
    return version;
}
function readVersionFromJson(pathname, field) {
    try {
        const parsed = JSON.parse((0, fs_1.readFileSync)(pathname, 'utf8'));
        const value = typeof parsed[field] === 'string' ? parsed[field].trim() : '';
        if (!value) {
            return null;
        }
        return assertValidCliVersion(value);
    }
    catch {
        return null;
    }
}
function readCliPackageVersion() {
    if (cachedCliPackageVersion) {
        return cachedCliPackageVersion;
    }
    const candidateMetaPaths = [
        path_1.default.resolve(__dirname, '..', 'config', 'client-meta.json'),
        path_1.default.resolve(__dirname, '..', '..', '..', 'config', 'client-meta.json'),
    ];
    for (const candidatePath of candidateMetaPaths) {
        const clientMetaVersion = readVersionFromJson(candidatePath, 'version');
        if (clientMetaVersion) {
            cachedCliPackageVersion = clientMetaVersion;
            return clientMetaVersion;
        }
    }
    const packageJsonPath = path_1.default.resolve(__dirname, '..', 'package.json');
    const version = readVersionFromJson(packageJsonPath, 'version');
    if (!version) {
        throw new Error(`invalid_cli_package_version:${packageJsonPath}`);
    }
    cachedCliPackageVersion = version;
    return version;
}
function mapPlatform(platform) {
    switch (platform) {
        case 'aix':
            return 'aix';
        case 'android':
            return 'android';
        case 'cygwin':
            return 'cygwin';
        case 'darwin':
            return 'macos';
        case 'freebsd':
            return 'freebsd';
        case 'haiku':
            return 'haiku';
        case 'win32':
            return 'windows';
        case 'linux':
            return 'linux';
        case 'netbsd':
            return 'netbsd';
        case 'openbsd':
            return 'openbsd';
        case 'sunos':
            return 'sunos';
    }
}
function detectPlatformVersion() {
    return os_1.default.release();
}
function buildRuntimeInfo() {
    return {
        client: 'cli',
        clientVersion: readCliPackageVersion(),
        clientBuild: (0, config_1.getClientBuild)(),
        platform: mapPlatform(os_1.default.platform()),
        platformVersion: detectPlatformVersion()
    };
}
function createClientHeaders() {
    return (0, config_1.createClientHeaderRecord)(buildRuntimeInfo());
}
function getCliVersionLabel() {
    return `${readCliPackageVersion()} (build ${(0, config_1.getClientBuild)()})`;
}
function getCliVersion() {
    return readCliPackageVersion();
}
function getCliBuild() {
    return (0, config_1.getClientBuild)();
}
function getRuntimeInfo() {
    return buildRuntimeInfo();
}
