"use strict";
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.SshMessage = void 0;
const buffer_1 = require("buffer");
const sshData_1 = require("../io/sshData");
class SshMessage {
    get messageType() {
        return 0;
    }
    toBuffer() {
        const writer = new sshData_1.SshDataWriter(buffer_1.Buffer.alloc(16));
        this.write(writer);
        return writer.toBuffer();
    }
    read(reader) {
        this.rawBytes = reader.buffer;
        const number = reader.readByte();
        if (number !== this.messageType) {
            throw new Error(`Message type ${number} is not valid.`);
        }
        this.onRead(reader);
    }
    write(writer) {
        if (this.rawBytes) {
            // Piped messages are rewritten without re-serialization. This preserves any
            // unparsed extended message data. It assumes no properties of the message
            // have been modified without also updating the serialized bytes.
            writer.write(this.rawBytes);
        }
        else {
            writer.writeByte(this.messageType);
            this.onWrite(writer);
        }
    }
    /**
     * Rewrites the message to its buffer to ensure the buffer has the correct values.
     * This should be called after modifying properties of a message that was (potentially)
     * received, before re-sending it.
     */
    rewrite() {
        if (this.rawBytes) {
            const writer = new sshData_1.SshDataWriter(buffer_1.Buffer.alloc(16));
            writer.writeByte(this.messageType);
            this.onWrite(writer);
            this.rawBytes = writer.toBuffer();
        }
    }
    onRead(reader) {
        throw new Error('Not supported.');
    }
    onWrite(writer) {
        throw new Error('Not supported.');
    }
    validateField(value, name) {
        if (typeof value === 'undefined') {
            throw new Error(`${this.constructor.name} ${name} is required.`);
        }
        return value;
    }
    toString() {
        return this.constructor.name;
    }
    static create(config, messageType, messageContext, data) {
        let messageClass = config.messages.get(messageType);
        if (!messageClass && messageContext) {
            const contextMessageTypes = config.contextualMessages.get(messageContext);
            if (contextMessageTypes) {
                messageClass = contextMessageTypes.get(messageType);
            }
        }
        if (messageClass) {
            const message = new messageClass();
            message.read(new sshData_1.SshDataReader(data));
            return message;
        }
        else {
            return null;
        }
    }
    convertTo(otherMessage, copy = false) {
        const reader = new sshData_1.SshDataReader(copy ? buffer_1.Buffer.from(this.rawBytes) : this.rawBytes);
        otherMessage.read(reader);
        return otherMessage;
    }
}
exports.SshMessage = SshMessage;
/* @internal */
SshMessage.index = new Map();
//# sourceMappingURL=https://main.vscode-cdn.net/sourcemaps/f6cfa2ea2403534de03f069bdf160d06451ed282/node_modules/@microsoft/dev-tunnels-ssh/messages/sshMessage.js.map