var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { BLANK_CONVERTER } from '../../utility/blank-converter';
import { variableFormatToByteSize } from '../../utility/type-converter-factory';
import { AbstractVariable } from '../abstract-variable-stream';
export class TargetMemoryVariable extends AbstractVariable {
    constructor(id, context) {
        super();
        this.id = id;
        this.context = context;
    }
    get converter() {
        return this.context.converter || BLANK_CONVERTER;
    }
    get config() {
        return this.context.config;
    }
    readRawInner() {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield this.context.targetService.readAddress(this.context.config)).body();
        });
    }
    writeRaw(value) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!value || !(value instanceof Uint8Array)) {
                throw new Error(`Bad parameter for TargetMemoryVariable.writeRaw(). Expecting an Uint8Array.`);
            }
            const expectedByteSize = this.config.length * variableFormatToByteSize(this.config.format);
            if (value.length !== expectedByteSize) {
                // TODO better error
                throw new Error(`Encoded value does not have the expected byte size. Expecting ${expectedByteSize} byte(s) but given ${value.length} byte(s)`);
            }
            return (yield this.context.targetService.writeAddress({
                data: value,
                info: this.context.config,
            })).body();
        });
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFyZ2V0LW1lbW9yeS12YXJpYWJsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uLy4uL2RhdGEvc3JjL2xpYi90YXJnZXQtdmFyaWFibGUvdGFyZ2V0LW1lbW9yeS90YXJnZXQtbWVtb3J5LXZhcmlhYmx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