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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFyZ2V0LW1lbW9yeS12YXJpYWJsZS5qcyIsInNvdXJjZVJvb3QiOiJuZzovL0Bpb3RpemUvdGFwL2RhdGEvIiwic291cmNlcyI6WyJsaWIvdGFyZ2V0LXZhcmlhYmxlL3RhcmdldC1tZW1vcnkvdGFyZ2V0LW1lbW9yeS12YXJpYWJsZS50cyJdLCJuYW1lcyI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