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