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 { TapVariable, createTapVariableFromConfig, } 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) {
        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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLWJ1bmRsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uLy4uL2RhdGEvc3JjL2xpYi90YXJnZXQtdmFyaWFibGUvdGFwLWJ1bmRsZS90YXAtYnVuZGxlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUNBLE9BQU8sRUFBRSxXQUFXLEVBQUUsTUFBTSxzQkFBc0IsQ0FBQztBQUNuRCxPQUFPLEVBQU8sUUFBUSxFQUEwQixNQUFNLGFBQWEsQ0FBQztBQUNwRSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sd0JBQXdCLENBQUM7QUFDcEQsT0FBTyxF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