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 { isCodeError } from '@iotize/common/error';
import { TapError } from '@iotize/tap';
import { ResultCode } from '@iotize/tap/client/api';
import { TapDataError } from '../../tap-data-error';
import { BLANK_CONVERTER } from '../../utility/blank-converter';
import { getConverterFromVariableType } from '../../utility/type-converter-factory';
import { AbstractVariable } from '../abstract-variable-stream';
export function createTapVariableFromConfig(variableService, key, value) {
    return new TapVariable(key, {
        variableService: variableService,
        config: {
            id: value.id,
            dataType: value.dataType,
            length: value.length
        },
        converter: value.converter || (value.dataType !== undefined
            ? getConverterFromVariableType(value.dataType, value.length || 1)
            : BLANK_CONVERTER)
    });
}
export class TapVariable extends AbstractVariable {
    constructor(
    /**
     * Variable name identifier.
     */
    id, context) {
        super();
        this.id = id;
        this.context = context;
    }
    get variableId() {
        return this.context.config.id;
    }
    get converter() {
        return this.context.converter || BLANK_CONVERTER;
    }
    get config() {
        return this.context.config;
    }
    set converter(converter) {
        this.context.converter = converter;
    }
    readRawInner() {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                let rawData = (yield this.context.variableService.getValue(this.context.config.id)).body();
                if (this.context.subIndex) {
                    rawData = this.context.subIndex.reduce((acc, subIndex, index) => {
                        acc[index] = rawData[subIndex];
                        return acc;
                    }, new Uint8Array(this.context.subIndex.length));
                }
                return rawData;
            }
            catch (err) {
                if (isCodeError(TapError.Code.ResponseStatusError, err)) {
                    switch (err.response.status) {
                        case ResultCode.NOT_FOUND:
                            throw TapDataError.variableNotConfigured(this.context.config, err);
                    }
                }
                throw err;
            }
        });
    }
    writeRaw(value) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                if (this.context.subIndex) {
                    const existingData = (yield this.context.variableService.getValue(this.context.config.id)).body();
                    this.context.subIndex.forEach((subIndex, index) => {
                        existingData[subIndex] = value[index];
                    });
                    value = existingData;
                }
                (yield this.context.variableService.setValue(this.context.config.id, value)).body();
            }
            catch (err) {
                if (isCodeError(TapError.Code.ResponseStatusError, err)) {
                    switch (err.response.status) {
                        case ResultCode.NOT_FOUND:
                            throw TapDataError.variableNotConfigured(this.context.config, err);
                    }
                }
                throw err;
            }
        });
    }
}
// TODO add Sub variable data stream (IE read/write index )
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLXZhcmlhYmxlLmpzIiwic291cmNlUm9vdCI6Im5nOi8vQGlvdGl6ZS90YXAvZGF0YS8iLCJzb3VyY2VzIjpbImxpYi90YXJnZXQtdmFyaWFibGUvdGFwLXZhcmlhYmxlL3RhcC12YXJpYWJsZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7QUFDQSxPQUFPLEVBQUUsV0FBVyxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFDbkQsT0FBTyxFQUFFLFFBQVEsRUFBMEIsTUFBTSxhQUFhLENBQUM7QUFDL0QsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLHdCQUF3QixDQUFDO0FBR3BELE9BQU8sRUFBRSxZQUFZLEVBQUUsTUFBTSxzQkFBc0IsQ0FBQztBQUNwRCxPQUFPLEVBQUUsZUFBZSxFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFFaEUsT0FBTyxFQUFFLDRCQUE0QixFQUFFLE1BQU0sc0NBQXNDLENBQUM7QUFDcEYsT0FBTyxFQUFFLGdCQUFnQixFQUFFLE1BQU0sNkJBQTZCLENBQUM7QUFHL0QsTUFBTSxVQUFVLDJCQUEyQixDQUF3QyxlQUFnQyxFQUFFLEdBQVksR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