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 { map } from 'rxjs/operators';
import { TapDataError } from '../tap-data-error';
import { BLANK_CONVERTER } from '../utility/blank-converter';
import { AbstractEditableDataStream } from '../utility/editable-data-stream';
export class AbstractVariable extends AbstractEditableDataStream {
    /**
     * Create a subvariable instance from the given variable index
     * @param key the uniq variable identifier to use for data manager
     * @param rawIndexes list of array index (starting from 0) to extract. Each index represents 1 byte of data
     */
    variableView(key, rawIndexes, options) {
        return new VariableView(key, {
            converter: (options === null || options === void 0 ? void 0 : options.converter) || BLANK_CONVERTER,
            indexes: rawIndexes,
            variable: this,
            useLastKnownValueBeforeWrite: options === null || options === void 0 ? void 0 : options.useLastKnownValueBeforeWrite
        });
    }
}
export class VariableView {
    constructor(id, context) {
        this.id = id;
        this.context = context;
    }
    get converter() {
        return this.context.converter;
    }
    get indexes() {
        return this.context.indexes;
    }
    get rawValues() {
        return this.context.variable
            .rawValues
            .pipe(map((rawData) => {
            return this.subDataView(rawData);
        }));
    }
    get valueSnapshot() {
        const lastRawValue = this.rawValueSnaphot;
        return lastRawValue !== undefined ? this.decodeValue(this.subDataView(lastRawValue)) : undefined;
    }
    get values() {
        return this.rawValues.pipe(map(v => this.decodeValue(v)));
    }
    get rawValueSnaphot() {
        const parentValueSnapshot = this.context.variable.rawValueSnaphot;
        return parentValueSnapshot !== undefined ? this.subDataView(parentValueSnapshot) : parentValueSnapshot;
    }
    notifyRawValue(newValue) {
    }
    read() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.decodeValue(yield this.readRaw());
        });
    }
    write(value) {
        return this.writeRaw(this.encodeValue(value));
    }
    readRaw() {
        return __awaiter(this, void 0, void 0, function* () {
            const rawData = yield this.context.variable.readRaw();
            return this.subDataView(rawData);
        });
    }
    writeRaw(value) {
        return __awaiter(this, void 0, void 0, function* () {
            let existingData;
            if (this.context.useLastKnownValueBeforeWrite) {
                existingData = this.context.variable.rawValueSnaphot || (yield this.context.variable.readRaw());
            }
            else {
                existingData = yield this.context.variable.readRaw();
            }
            this.indexes.forEach((subIndex, index) => {
                existingData[subIndex] = value[index];
            });
            yield this.context.variable.writeRaw(existingData);
        });
    }
    subDataView(rawData) {
        return this.indexes.reduce((acc, subIndex, index) => {
            acc[index] = rawData[subIndex];
            return acc;
        }, new Uint8Array(this.indexes.length));
    }
    encodeValue(value) {
        try {
            return this.converter.encode(value);
        }
        catch (err) {
            throw TapDataError.unexpectedEncodedValueError(this, value, err);
        }
    }
    decodeValue(data) {
        try {
            return this.converter.decode(data);
        }
        catch (err) {
            throw TapDataError.unexpectedVariableDataRead(this, data, err);
        }
    }
}
//# sourceMappingURL=data:application/json;base64, 