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