"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CLIUnsupportedClientError = void 0;
exports.buildRequestHeaders = buildRequestHeaders;
exports.handleUnsupportedError = handleUnsupportedError;
const clientInfo_1 = require("./clientInfo");
class CLIUnsupportedClientError extends Error {
    constructor(message) {
        super(message ?? 'Client version unsupported');
        this.name = 'CLIUnsupportedClientError';
    }
}
exports.CLIUnsupportedClientError = CLIUnsupportedClientError;
function buildRequestHeaders(additional = {}) {
    return {
        ...(0, clientInfo_1.createClientHeaders)(),
        ...additional,
    };
}
function handleUnsupportedError(error, context) {
    const details = (error.details && typeof error.details === 'object') ? error.details : null;
    if (typeof details?.message === 'string') {
        console.error(`${context} failed: ${details.message}`);
    }
    else {
        console.error(`${context} failed: ${error.message}`);
    }
    const currentVersion = details?.currentVersion;
    if (typeof currentVersion === 'string' && currentVersion.trim()) {
        const build = details?.currentBuild;
        const buildLabel = build !== undefined && build !== null ? ` (build ${build})` : '';
        console.error(`Current CLI version: ${currentVersion}${buildLabel}`);
    }
    const requiredVersion = details?.requiredVersion;
    const requiredBuild = details?.requiredBuild;
    const requiredParts = [];
    if (typeof requiredVersion === 'string' && requiredVersion.trim()) {
        requiredParts.push(`version ${requiredVersion}`);
    }
    if (requiredBuild !== undefined && requiredBuild !== null) {
        requiredParts.push(`build ${requiredBuild} or later`);
    }
    if (requiredParts.length > 0) {
        console.error(`Required: ${requiredParts.join(', ')}`);
    }
    console.error('Run `playdrop update` to update Playdrop.');
    process.exitCode = 1;
    throw new CLIUnsupportedClientError(error.message);
}
