"use strict";
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.SshVersionInfo = void 0;
const packageJson = require("./package.json");
const packageName = packageJson.name.replace(/^@\w+\//, ''); // Strip scope from name.
const packageVersion = packageJson.version;
/**
 * Parses the SSH software name and version from the version string exchanged via the
 * initial packets sent between client and server.
 */
class SshVersionInfo {
    /**
     * Attempts to parse an SSH version string into a version info object.
     */
    static tryParse(versionString) {
        if (!versionString) {
            throw new TypeError('Version string expected.');
        }
        if (!versionString.startsWith('SSH-')) {
            return null;
        }
        const firstDashIndex = 3;
        const secondDashIndex = versionString.indexOf('-', firstDashIndex + 1);
        if (secondDashIndex <= 0) {
            return null;
        }
        const protocolVersion = versionString.substring(firstDashIndex + 1, secondDashIndex);
        if (!/^\d+\.\d+$/.test(protocolVersion)) {
            return null;
        }
        let name;
        let version;
        const nameAndVersion = versionString.substring(secondDashIndex + 1);
        const spaceIndex = nameAndVersion.indexOf(' ');
        const lastUnderscoreBeforeSpace = nameAndVersion.lastIndexOf('_', spaceIndex >= 0 ? spaceIndex : nameAndVersion.length - 1);
        if (lastUnderscoreBeforeSpace >= 0) {
            name = nameAndVersion.substring(0, lastUnderscoreBeforeSpace).replace(/_/g, ' ');
            // Ignore any non-digit characters after the version.
            version = nameAndVersion.substring(lastUnderscoreBeforeSpace + 1);
            for (let i = 0; i < version.length; i++) {
                const c = version[i];
                if (!(c >= '0' && c <= '9') && c !== '.') {
                    version = version.substring(0, i);
                    break;
                }
            }
            if (!/^\d+(\.\d+)*$/.test(version)) {
                version = null;
            }
        }
        else {
            name = nameAndVersion;
            version = null;
        }
        return new SshVersionInfo(versionString, protocolVersion, name, version);
    }
    /**
     * Gets the version info for the current SSH library.
     */
    static getLocalVersion() {
        const protocolVersion = '2.0';
        const versionString = `SSH-${protocolVersion}-${packageName}_${packageVersion}`;
        return new SshVersionInfo(versionString, protocolVersion, packageName, packageVersion);
    }
    constructor(versionString, protocolVersion, name, version) {
        this.versionString = versionString;
        this.protocolVersion = protocolVersion;
        this.name = name;
        this.version = version;
    }
    /** Returns the original SSH version string that was parsed. */
    toString() {
        return this.versionString;
    }
    /**
     * Gets a value indicating whether this version info represents some version of
     * this library.
     */
    get isVsSsh() {
        return this.isVsSshCS || this.isVsSshTS;
    }
    get isVsSshCS() {
        return this.name === 'Microsoft.VisualStudio.Ssh' || this.name === 'Microsoft.DevTunnels.Ssh';
    }
    get isVsSshTS() {
        return this.name === 'vs-ssh' || this.name === 'dev-tunnels-ssh';
    }
}
exports.SshVersionInfo = SshVersionInfo;
//# sourceMappingURL=https://github.com/VSCodium/sourcemaps/releases/download/stable-221e0a382c0be3a673a4e4cab0601344a0b3de3a/node_modules-@microsoft-dev-tunnels-ssh-sshVersionInfo.js.map