import deviceClient from '@iotize/device-client.js';
import impl from '@iotize/device-client.js/client/impl';
import rxjs from 'rxjs';
import testingGrammar from '@iotize/testing-grammar.js';
import operators from 'rxjs/operators';

var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};

function commonjsRequire () {
	throw new Error('Dynamic requires are not currently supported by rollup-plugin-commonjs');
}

function unwrapExports (x) {
	return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}

function createCommonjsModule(fn, module) {
	return module = { exports: {} }, fn(module, module.exports), module.exports;
}

function getCjsExportFromNamespace (n) {
	return n && n['default'] || n;
}

var definitions = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ByteOrder32Bits;
(function (ByteOrder32Bits) {
    ByteOrder32Bits[ByteOrder32Bits["B3_B2_B1_B0"] = 0] = "B3_B2_B1_B0";
    ByteOrder32Bits[ByteOrder32Bits["B1_B0_B3_B2"] = 1] = "B1_B0_B3_B2";
    ByteOrder32Bits[ByteOrder32Bits["B2_B3_B0_B1"] = 2] = "B2_B3_B0_B1";
    ByteOrder32Bits[ByteOrder32Bits["B0_B1_B2_B3"] = 3] = "B0_B1_B2_B3";
})(ByteOrder32Bits = exports.ByteOrder32Bits || (exports.ByteOrder32Bits = {}));
var ByteOrder16Bits;
(function (ByteOrder16Bits) {
    ByteOrder16Bits[ByteOrder16Bits["B1_B0"] = 0] = "B1_B0";
    ByteOrder16Bits[ByteOrder16Bits["B0_B1"] = 1] = "B0_B1";
})(ByteOrder16Bits = exports.ByteOrder16Bits || (exports.ByteOrder16Bits = {}));

});

var definitions$1 = unwrapExports(definitions);
var definitions_1 = definitions.ByteOrder32Bits;
var definitions_2 = definitions.ByteOrder16Bits;

var util = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function toEnumFromValue(mapping, value) {
    let values = Object.keys(mapping).map(key => mapping[key]);
    if (values.find((v) => v.toString() === value) ===
        undefined) {
        throw new Error(`Missing value "${value}" in enum. Available keys are ${values.join(', ')}`);
    }
    if (value in mapping) {
        // For string to number convertion
        return mapping[mapping[value]];
    }
    return value;
}
exports.toEnumFromValue = toEnumFromValue;
// export function toEnumFromKey<T>(mapping: any, key: string): T {
//     if (!(key in mapping)) {
//         throw new Error(`Missing key "${key}" in enum. Available keys are ${Object.keys(mapping).join(', ')}`);
//     }
//     return mapping[mapping[key]];
// }

});

var util$1 = unwrapExports(util);
var util_1 = util.toEnumFromValue;

var variableDataTypes = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @module model
 */
var VariableDataTypes;
(function (VariableDataTypes) {
    VariableDataTypes[VariableDataTypes["int"] = 0] = "int";
    VariableDataTypes[VariableDataTypes["unsigned_int"] = 1] = "unsigned_int";
    VariableDataTypes[VariableDataTypes["float"] = 2] = "float";
})(VariableDataTypes = exports.VariableDataTypes || (exports.VariableDataTypes = {}));

});

var variableDataTypes$1 = unwrapExports(variableDataTypes);
var variableDataTypes_1 = variableDataTypes.VariableDataTypes;

var iotizeVariableModel = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @module model
 */




class IotizeVariableModel {
    constructor(data, iotizeModule, bundle) {
        this.data = data;
        this.iotizeModule = iotizeModule;
        if (bundle) {
            this._bundle = bundle;
        }
    }
    toJson() {
        return this.data;
    }
    module() {
        return this.iotizeModule;
    }
    /**
     * Size (int bits) of one element
     */
    valueSize() {
        return IotizeVariableModel.unitSizeToBitSize(parseInt(this.data.$.Unit));
    }
    valueQuantity() {
        return Number(this.data.$.Quantity);
    }
    name() {
        return this.data.$.GUI_Name;
    }
    id() {
        return this.data.$.Id;
    }
    alias() {
        return this.data.$.GUI_Alias;
    }
    description() {
        return this.data.$.GUI_Description;
    }
    unit() {
        return this.data.$.GUI_DispUnit;
    }
    bundle() {
        return this._bundle;
    }
    type() {
        if (this.data.$.GUI_float && this.data.$.GUI_float == 1) {
            return variableDataTypes.VariableDataTypes.float;
        }
        else if (this.data.$.GUI_unsigned && this.data.$.GUI_unsigned == 1) {
            return variableDataTypes.VariableDataTypes.unsigned_int;
        }
        else {
            return variableDataTypes.VariableDataTypes.int;
        }
    }
    dataType() {
        if (this.data.$.GUI_float && this.data.$.GUI_float == 1) {
            return deviceClient.VariableType.FLOAT32;
        }
        else if (this.data.$.GUI_unsigned && this.data.$.GUI_unsigned == 1) {
            switch (this.data.$.Unit) {
                case 0:
                    return deviceClient.VariableType.BOOLEAN;
                case 1:
                    return deviceClient.VariableType.UINT8;
                case 2:
                    return deviceClient.VariableType.UINT16;
                case 3:
                    return deviceClient.VariableType.UINT32;
                default:
                    throw new Error(`Invalid data size for variable ${this.name()}`);
            }
        }
        else {
            switch (this.data.$.Unit) {
                case 0:
                    return deviceClient.VariableType.BOOLEAN;
                case 1:
                    return deviceClient.VariableType.INT8;
                case 2:
                    return deviceClient.VariableType.INT16;
                case 3:
                    return deviceClient.VariableType.INT32;
                default:
                    throw new Error(`Invalid data size for variable ${this.name()}`);
            }
        }
    }
    /**
     * Return attribute by name
     */
    attribute(name) {
        return this.data.$[name];
    }
    byteOrder() {
        let guiCoding = this.attribute('GUI_coding');
        if (guiCoding) {
            if (this.valueSize() === 16) {
                return util.toEnumFromValue(definitions.ByteOrder16Bits, guiCoding);
            }
            else if (this.valueSize() === 32) {
                return util.toEnumFromValue(definitions.ByteOrder32Bits, guiCoding);
            }
        }
        return undefined;
    }
    /**
     * Convert unit size found in configuration file into a number of bits
     */
    static unitSizeToBitSize(unitSize) {
        switch (unitSize) {
            case 0:
                return 1;
            case 1:
                return 8;
            case 2:
                return 16;
            case 3:
                return 32;
            default:
                throw new Error(`Invalid unit size: ${unitSize}`);
        }
    }
}
exports.IotizeVariableModel = IotizeVariableModel;

});

var iotizeVariableModel$1 = unwrapExports(iotizeVariableModel);
var iotizeVariableModel_1 = iotizeVariableModel.IotizeVariableModel;

var iotizeProfileModel = createCommonjsModule(function (module, exports) {
"use strict";
/**
 * @module model
 */
Object.defineProperty(exports, "__esModule", { value: true });
class IotizeProfileModel {
    constructor(data) {
        this.data = data;
    }
    toJson() {
        return this.data;
    }
    description() {
        return this.data.$.GUI_Description;
    }
    name() {
        return this.data.$.Name;
    }
    id() {
        return this.data.$.Id;
    }
}
exports.IotizeProfileModel = IotizeProfileModel;

});

var iotizeProfileModel$1 = unwrapExports(iotizeProfileModel);
var iotizeProfileModel_1 = iotizeProfileModel.IotizeProfileModel;

var baseError = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class BaseError extends Error {
    constructor(message, errCode, original) {
        super(message);
        this.message = message;
        this.name = this.constructor.name;
        this.code = errCode || BaseError.UNKNOWN_ERROR;
        this.original = original;
        // this.stack = (new Error()).stack;
    }
}
exports.BaseError = BaseError;
BaseError.UNKNOWN_ERROR = 'UnknownError';

});

var baseError$1 = unwrapExports(baseError);
var baseError_1 = baseError.BaseError;

var invalidConfigurationError = createCommonjsModule(function (module, exports) {
"use strict";
/**
 * @module model
 */
Object.defineProperty(exports, "__esModule", { value: true });

class InvalidConfigurationError extends baseError.BaseError {
    constructor(msg) {
        super(msg, 'InvalidConfigurationError');
    }
}
exports.InvalidConfigurationError = InvalidConfigurationError;

});

var invalidConfigurationError$1 = unwrapExports(invalidConfigurationError);
var invalidConfigurationError_1 = invalidConfigurationError.InvalidConfigurationError;

var iotizeAcl = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @module model
 */

class IotizeACL {
    constructor(data, aclGroup) {
        this.aclGroup = aclGroup;
        this.data = data;
    }
    static create(profileName, aclGroup) {
        return new IotizeACL({
            $: {
                ProfileName: profileName,
                Rights: ''
            }
        }, aclGroup);
    }
    toJson() {
        return this.data;
    }
    listRights() {
        let rights = this._getProperty('Rights', '');
        if (rights.length === 0) {
            return [];
        }
        return rights.split(',').map((entry) => {
            return entry.trim();
        });
    }
    username() {
        return this._getProperty('ProfileName');
    }
    hasRight(name) {
        if (this.username() === 'admin') {
            return true;
        }
        let rights = this.listRights();
        return rights.indexOf(name) !== -1;
    }
    _getProperty(name, defaultValue) {
        if (!(name in this.data.$)) {
            if (defaultValue === undefined) {
                throw new invalidConfigurationError.InvalidConfigurationError('Missing key ' + name + ' in IoTize ACL');
            }
            return defaultValue;
        }
        return this.data.$[name];
    }
}
exports.IotizeACL = IotizeACL;

});

var iotizeAcl$1 = unwrapExports(iotizeAcl);
var iotizeAcl_1 = iotizeAcl.IotizeACL;

var iotizeAclGroup = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @module model
 */

const ANONYMOUS_PROFILE = 'anonymous';
/**
 *
 */
class IotizeACLGroup {
    constructor(data, bundle) {
        this.bundle = bundle;
        this.data = data;
    }
    toJson() {
        return this.data;
    }
    /**
     * Get acl for a profile
     * @param profileName
     */
    getByProfileName(profileName) {
        let profiles = this.listProfiles();
        for (let profileAcl of profiles) {
            if (profileAcl.username() === profileName) {
                return profileAcl;
            }
        }
        return iotizeAcl.IotizeACL.create(profileName, this);
    }
    hasRight(profileName, right) {
        let anonymousProfile = this.getByProfileName(ANONYMOUS_PROFILE);
        if (anonymousProfile && anonymousProfile.hasRight(right)) {
            return true;
        }
        if (profileName !== ANONYMOUS_PROFILE) {
            let profile = this.getByProfileName(profileName);
            if (profile) {
                return profile.hasRight(right);
            }
        }
        return false;
    }
    /**
     * List acl entries for each profile
     */
    listProfiles() {
        if (!this.data.IOTizeACL) {
            return [];
        }
        let entries = [];
        for (let data of this.data.IOTizeACL) {
            entries.push(new iotizeAcl.IotizeACL(data, this));
        }
        return entries;
    }
}
exports.IotizeACLGroup = IotizeACLGroup;

});

var iotizeAclGroup$1 = unwrapExports(iotizeAclGroup);
var iotizeAclGroup_1 = iotizeAclGroup.IotizeACLGroup;

var iotizeBundleModel = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @module model
 */





class IotizeBundleModel {
    constructor(data, iotizeModule) {
        this.data = data;
        this.iotizeModule = iotizeModule;
    }
    toJson() {
        return this.data;
    }
    module() {
        return this.iotizeModule;
    }
    id() {
        if (!this.data.$.Id) {
            throw new invalidConfigurationError.InvalidConfigurationError('This bundle does not have an id');
        }
        return this.data.$.Id;
    }
    name() {
        return this.data.$.GUI_Name || '#' + this.id();
    }
    dataLogPeriod() {
        if (!this.data.$.DataLogPeriod) {
            return 0;
        }
        return Number(this.data.$.DataLogPeriod);
    }
    hasDataLog() {
        return this.dataLogPeriod() > 0;
    }
    description() {
        return this.data.$.GUI_Description;
    }
    variable(variableId) {
        for (let variable of this.variables()) {
            if (variable.id() === variableId) {
                return variable;
            }
        }
        throw new iotizeConfigModel.VariableDoesNotExistError(variableId, this.iotizeModule);
    }
    hasVariable(variableId) {
        for (let variable of this.variables()) {
            if (variable.id() === variableId) {
                return variable;
            }
        }
        return false;
    }
    profiles() {
        if (!this.data.IOTizeAccessControl) {
            return [];
        }
        let profiles = [];
        for (let profile of this.data.IOTizeAccessControl) {
            profiles.push(new iotizeProfileModel.IotizeProfileModel(profile));
        }
        return profiles;
    }
    variables() {
        if (!this.data.IOTizeVariable) {
            return [];
        }
        let variables = [];
        for (let variable of this.data.IOTizeVariable) {
            variables.push(new iotizeVariableModel.IotizeVariableModel(variable, this.iotizeModule, this));
        }
        return variables;
    }
    getACLGroup() {
        let data = {};
        if (!this.data.IOTizeAccessControl) {
            this.data.IOTizeAccessControl = [];
        }
        if (this.data.IOTizeAccessControl.length > 0) {
            data = this.data.IOTizeAccessControl[0];
        }
        return new iotizeAclGroup.IotizeACLGroup(data, this);
    }
}
exports.IotizeBundleModel = IotizeBundleModel;

});

var iotizeBundleModel$1 = unwrapExports(iotizeBundleModel);
var iotizeBundleModel_1 = iotizeBundleModel.IotizeBundleModel;

var iotizeConfigModel = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @module model
 */




/**
 *
 */
class BundleDoesNotExistError extends baseError.BaseError {
    constructor(bundleId, config) {
        super('Bundle "' + bundleId + '" does not exist.', BundleDoesNotExistError.CODE);
        this.bundleId = bundleId;
        this.config = config;
    }
}
exports.BundleDoesNotExistError = BundleDoesNotExistError;
BundleDoesNotExistError.CODE = 'BundleDoesNotExistError';
/**
 *
 */
class VariableDoesNotExistError extends baseError.BaseError {
    constructor(variableId, config) {
        super(`Variable "${variableId}" does not exist.`, VariableDoesNotExistError.CODE);
        this.variableId = variableId;
        this.config = config;
    }
}
exports.VariableDoesNotExistError = VariableDoesNotExistError;
VariableDoesNotExistError.CODE = 'VariableDoesNotExistError';
/**
 * Class representing an iotize studio configuration for a module
 */
class IotizeConfigModel {
    constructor(data) {
        if (!data.IOTizeConfiguration) {
            //console.error('Trying to create an IotizeConfigModel with invalid data: ', data);
            throw new invalidConfigurationError.InvalidConfigurationError('Key IOTizeConfiguration must exists');
        }
        if (!data.IOTizeConfiguration.IOTizeApp) {
            throw new invalidConfigurationError.InvalidConfigurationError('Key IOTizeConfiguration.IOTizeApp must exists');
        }
        if (data.IOTizeConfiguration.IOTizeApp.length === 0 ||
            !data.IOTizeConfiguration.IOTizeApp[0]['$']) {
            throw new invalidConfigurationError.InvalidConfigurationError('No IOTizeConfiguration.IOTizeApp defined in this configuration file');
        }
        this.data = data;
    }
    toJson() {
        return this.data;
    }
    getAppConfig() {
        return this.data.IOTizeConfiguration.IOTizeApp[0];
    }
    /**
     * Get application name
     */
    appName() {
        if (!this.getAppConfig().$.Name) {
            throw new invalidConfigurationError.InvalidConfigurationError('Application Name is not set');
        }
        return this.getAppConfig().$.Name;
    }
    /**
     * Get config version
     */
    version() {
        if (!this.getAppConfig().$.Config) {
            throw new invalidConfigurationError.InvalidConfigurationError('App config version is not set');
        }
        return this.getAppConfig().$.Config;
    }
    /**
     * Get module serial number
     */
    id() {
        if (!this.getAppConfig().$.DevIoTizeSN) {
            throw new invalidConfigurationError.InvalidConfigurationError('Serial number is not set');
        }
        return this.getAppConfig().$.DevIoTizeSN;
    }
    /**
     * Set module serial number
     * @param id
     */
    setId(id) {
        this.getAppConfig().$.DevIoTizeSN = id;
        return this;
    }
    /**
     * Alias for this.id();
     */
    serialNumber() {
        return this.id();
    }
    /**
     * Get an array of all bundles
     */
    bundles() {
        if (!this.data.IOTizeConfiguration.IOTizeBundle) {
            return [];
        }
        let bundles = [];
        for (let bundle of this.data.IOTizeConfiguration.IOTizeBundle) { // TODO bug if only one bundle
            bundles.push(new iotizeBundleModel.IotizeBundleModel(bundle, this));
        }
        return bundles;
    }
    /**
     * Array of bundles available for a specific profile
     */
    bundleAvailableForProfileName(profileName) {
        return this.bundles().reduce((list, currentValue) => {
            if (currentValue.getACLGroup().hasRight(profileName, 'read')) {
                list.push(currentValue);
            }
            return list;
        }, []);
    }
    /**
     * Get an array of all variables
     */
    variables() {
        if (!this.data.IOTizeConfiguration.IOTizeBundle) {
            return [];
        }
        let variables = {};
        for (let bundle of this.data.IOTizeConfiguration.IOTizeBundle) {
            bundle = new iotizeBundleModel.IotizeBundleModel(bundle, this);
            //            console.log('VAR: ', bundle.variables());
            for (let variable of bundle.variables()) {
                variables[variable.name()] = variable;
            }
        }
        let res = [];
        for (let key in variables) {
            res.push(variables[key]);
        }
        return res;
    }
    /**
     * Get bundle by id, if it does not exist, throw BundleDoesNotExistError
     * @throws BundleDoesNotExistError
     * @param id
     */
    bundle(id) {
        for (let bundle of this.bundles()) {
            if (bundle.id() === id) {
                return bundle;
            }
        }
        throw new BundleDoesNotExistError(id, this);
    }
    hasBundle(id) {
        for (let bundle of this.bundles()) {
            if (bundle.id() === id) {
                return bundle;
            }
        }
        return false;
    }
    /**
     * Get variable by id, if it does not exist, throw VariableDoesNotExistError
     * @throws VariableDoesNotExistError
     * @param id
     */
    variable(variableId) {
        for (let variable of this.variables()) {
            if (variable.id() == variableId) {
                return variable;
            }
        }
        throw new VariableDoesNotExistError(variableId, this);
    }
    /**
     * Get an array of all profiles
     */
    profiles() {
        if (!this.data.IOTizeConfiguration.IOTizeProfile) {
            return [];
        }
        let profiles = [];
        for (let profile of this.data.IOTizeConfiguration.IOTizeProfile) { // TODO bug if there is only one profile...
            profiles.push(new iotizeProfileModel.IotizeProfileModel(profile));
        }
        return profiles;
    }
    description() {
        return this.getAppConfig().$.GUI_Description;
    }
    name() {
        return this.getAppConfig().$.GUI_Name;
    }
}
exports.IotizeConfigModel = IotizeConfigModel;

});

var iotizeConfigModel$1 = unwrapExports(iotizeConfigModel);
var iotizeConfigModel_1 = iotizeConfigModel.BundleDoesNotExistError;
var iotizeConfigModel_2 = iotizeConfigModel.VariableDoesNotExistError;
var iotizeConfigModel_3 = iotizeConfigModel.IotizeConfigModel;

var iotizeConfigFactory = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

/**
 * Utility class to create iotize config model from XML or JSON file
 */
class IotizeConfigFactory {
    /**
     * Create IotizeConfigModel from a XML string
     * @param xml
     */
    static fromXML(xml) {
        throw new Error(`Add import to @iotize/device-client.js/config/iotize-studio/parser/xml to create configuration model from XML file`);
    }
    /**
     * Create IotizeConfigModel from a JSON string
     * @param json
     */
    static fromJson(json) {
        return new iotizeConfigModel.IotizeConfigModel(JSON.parse(json));
    }
    /**
     * Build and IotizeConfigModel
     * Input can either be
     * - A json string
     * - A XML string
     * - A JSON Object
     *
     * TODO create test
     * @param data
     */
    static create(data) {
        if (typeof data === 'string') {
            data.trim();
            if (data.length === 0) {
                throw new Error('Cannot create configuration from empty data');
            }
            if (data[0] === '{') {
                return IotizeConfigFactory.fromJson(data);
            }
            else {
                return IotizeConfigFactory.fromXML(data);
            }
        }
        else {
            return new iotizeConfigModel.IotizeConfigModel(data);
        }
    }
}
exports.IotizeConfigFactory = IotizeConfigFactory;

});

var iotizeConfigFactory$1 = unwrapExports(iotizeConfigFactory);
var iotizeConfigFactory_1 = iotizeConfigFactory.IotizeConfigFactory;

var model = createCommonjsModule(function (module, exports) {
"use strict";
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @module model
 */
__export(iotizeBundleModel);
__export(iotizeConfigModel);
__export(iotizeProfileModel);
__export(iotizeVariableModel);
__export(variableDataTypes);

exports.InvalidConfigurationError = invalidConfigurationError.InvalidConfigurationError;

});

var index = unwrapExports(model);
var model_1 = model.InvalidConfigurationError;

var tapConfigConfigurator = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });



function toVariableConfig(variables) {
    return variables.map((value) => {
        return {
            converter: TapConfigConfigurator.getConverterFromVariableModel(value),
            id: parseInt(value.id()),
            length: value.valueQuantity(),
            name: value.name(),
            unit: value.unit()
        };
    });
}
exports.toVariableConfig = toVariableConfig;
function toBundleConfig(bundles) {
    return bundles.map((bundle) => {
        return {
            id: parseInt(bundle.id()),
            name: bundle.name(),
            variables: toVariableConfig(bundle.variables())
        };
    });
}
exports.toBundleConfig = toBundleConfig;
/**
 * Configure your tap device instance from a configuration model
 * This will
 *  - populate variable manager and bundle manager according to the configuration
 */
class TapConfigConfigurator {
    constructor(configModel) {
        this.configModel = configModel;
    }
    configure(device) {
        return rxjs.Observable.create(observer => {
            try {
                observer.next(this.configureVariables(device));
                observer.complete();
            }
            catch (error) {
                observer.error(error);
            }
        });
    }
    configureVariables(tap) {
        let bundleConfig = toBundleConfig(this.configModel.bundles());
        tap.bundles.addFromConfig(bundleConfig);
        let variableConfigs = this.configModel
            .bundles()
            .map((bundle) => toVariableConfig(bundle.variables()))
            .reduce((prev, variableConfig) => {
            prev.push(...variableConfig);
            return prev;
        }, []);
        tap.variables.addFromConfig(variableConfigs);
    }
    static getConverterFromVariableModel(variable) {
        switch (variable.type()) {
            case model.VariableDataTypes.float:
                return impl.FloatConverter.instance32();
            case model.VariableDataTypes.int:
                return new impl.NumberConverter({
                    signed: true,
                    sizeOf: variable.valueSize(),
                    leastSignificantBitFirst: false
                });
            case model.VariableDataTypes.unsigned_int:
                return new impl.NumberConverter({
                    signed: false,
                    sizeOf: variable.valueSize(),
                    leastSignificantBitFirst: false
                });
            default:
                throw new Error(`Unknown converter for: ${variable}`);
        }
    }
}
exports.TapConfigConfigurator = TapConfigConfigurator;

});

var tapConfigConfigurator$1 = unwrapExports(tapConfigConfigurator);
var tapConfigConfigurator_1 = tapConfigConfigurator.toVariableConfig;
var tapConfigConfigurator_2 = tapConfigConfigurator.toBundleConfig;
var tapConfigConfigurator_3 = tapConfigConfigurator.TapConfigConfigurator;

var tapScriptConfiguratorLoader = createCommonjsModule(function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });



/**
 * Load configuration on your tap
 */
class TapScriptConfiguratorLoader {
    constructor(
    /**
     * The script content string
     */
    script, 
    /**
     * Script runner instance (optional)
     */
    scriptRunner, 
    /**
     * Extra options
     */
    options = {
        waitDurationMs: 400
    }) {
        this.script = script;
        this.options = options;
        if (!scriptRunner) {
            scriptRunner = new testingGrammar.ScriptRunner();
        }
        this.scriptRunner = scriptRunner;
    }
    configure(device) {
        var instructions = testingGrammar.GrammarHelper.toProgInstructions(this.script);
        let result = rxjs.from(device.service.device.factoryReset()).pipe(operators.concat(this.scriptRunner.run(instructions, device), TapScriptConfiguratorLoader.sleep(this.options.waitDurationMs), device.service.interface.reloadConfig(), TapScriptConfiguratorLoader.sleep(this.options.waitDurationMs)));
        return result;
    }
    static sleep(ms) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve();
            }, ms);
        });
    }
}
exports.TapScriptConfiguratorLoader = TapScriptConfiguratorLoader;

});

var tapScriptConfiguratorLoader$1 = unwrapExports(tapScriptConfiguratorLoader);
var tapScriptConfiguratorLoader_1 = tapScriptConfiguratorLoader.TapScriptConfiguratorLoader;

var lib = createCommonjsModule(function (module, exports) {
"use strict";
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(iotizeConfigFactory);
__export(definitions);
__export(tapConfigConfigurator);
__export(tapScriptConfiguratorLoader);
__export(model);

});

var index$1 = unwrapExports(lib);

var public_api = createCommonjsModule(function (module, exports) {
"use strict";
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(lib);

});

var public_api$1 = unwrapExports(public_api);

var iotizeDeviceClient_jsConfigIotizeStudio = createCommonjsModule(function (module, exports) {
"use strict";
/**
 * Generated bundle index. Do not edit.
 */
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(public_api);

exports.ɵb = model.IotizeConfigModel;

exports.ɵa = iotizeConfigModel.IotizeConfigModel;

});

var iotizeDeviceClient_jsConfigIotizeStudio$1 = unwrapExports(iotizeDeviceClient_jsConfigIotizeStudio);

export default iotizeDeviceClient_jsConfigIotizeStudio$1;
//# sourceMappingURL=iotize-device-client.js-config-iotize-studio.js.map
