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 { TlvBundleConverter } from '@iotize/tap/client/impl';
import { TapDataError } from '../../tap-data-error';
import { AbstractBundleDataStream, } from '../../utility/editable-data-stream';
import { createTapVariableFromConfig, TapVariable, } from '../tap-variable/tap-variable';
export const TLV_BUNDLE_CONVERTER = new TlvBundleConverter();
export function createTapBundleFromConfig(tap, name, config) {
    const variables = Object.keys(config.variables).reduce((acc, key) => {
        const value = config.variables[key];
        acc[key] = createTapVariableFromConfig(tap.service.variable, key, value);
        return acc;
    }, {});
    return new TapBundle(name, {
        tap,
        config: {
            id: config.id,
        },
    }, variables);
}
export class TapBundle extends AbstractBundleDataStream {
    constructor(_id, context, _variables
    // converter: EncoderDecoder<DataType, Uint8Array>
    ) {
        super(_variables);
        this._id = _id;
        this.context = context;
        // this.converter = TLV_BUNDLE_CONVERTER;
        // super(
        //     context,
        //     undefined,
        // );
    }
    get id() {
        return this._id;
    }
    get bundleId() {
        return this.context.config.id;
    }
    // addConfiguredVariable<T>(name: string, options: {
    //     id: number;
    //     dataType: VariableType.Data,
    //     length?: number
    // }) {
    //     if (this.hasVariable(options.id)) {
    //         throw new Error(`Alredy has variable ${name}`);
    //     }
    //     // this.context.config.variables.push({
    //     //     name,
    //     //     id: options.id
    //     // });
    //     const variable = new ConfiguredVariableDataStream<DataType>(
    //         // config as VariableConfigWithName,
    //         {
    //             tap: this.context.tap,
    //             config: {
    //                 id: options.id,
    //                 name
    //             }
    //         },
    //         getConverterFromVariableType(
    //             options.dataType,
    //             options.length || 1
    //         ),
    //         // dataStream,
    //     );
    //     this._variables.push(variable);
    // }
    readRawInner() {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const variableIdMapping = (yield this.context.tap.service.bundle.getValues(this.bundleId)).body(TLV_BUNDLE_CONVERTER);
                const streamIdMapping = {}; // Partial<RawBundleType<DataType>>
                // for (const tapVariableId of vara)
                for (const variable of this.variables) {
                    if (variable instanceof TapVariable) {
                        if (variable.variableId in variableIdMapping) {
                            streamIdMapping[variable.id] =
                                variableIdMapping[variable.variableId];
                        }
                        else {
                            streamIdMapping[variable.id] = undefined;
                            // if (!(variable.id in streamIdMapping)) {
                            //     streamIdMapping[variable.id] = variableIdMapping[]
                            // }
                            // (streamIdMapping as any)[variable.id]
                            // TODO un mapped ?
                            // throw new Error(`${variable.id} is not mapped`)
                        }
                    }
                }
                return streamIdMapping;
            }
            catch (err) {
                if (isCodeError(TapError.Code.ResponseStatusError, err)) {
                    switch (err.response.status) {
                        case ResultCode.NOT_FOUND:
                            throw TapDataError.bundleNotConfigured(this.context.config, err);
                    }
                }
                throw err;
            }
        });
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLWJ1bmRsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uLy4uL2RhdGEvc3JjL2xpYi90YXJnZXQtdmFyaWFibGUvdGFwLWJ1bmRsZS90YXAtYnVuZGxlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUNBLE9BQU8sRUFBRSxXQUFXLEVBQUUsTUFBTSxzQkFBc0IsQ0FBQztBQUNuRCxPQUFPLEVBQU8sUUFBUSxFQUEwQixNQUFNLGFBQWEsQ0FBQztBQUNwRSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sd0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