var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { BaseError } from '@iotize/common/error';
import { InvalidConfigurationError } from './invalid-configuration-error';
import { IotizeBundleModel } from './iotize-bundle-model';
import { IotizeProfileModel } from './iotize-profile-model';
/**
 *
 */
var BundleDoesNotExistError = /** @class */ (function (_super) {
    __extends(BundleDoesNotExistError, _super);
    // static CODE = 'BundleDoesNotExistError';
    function BundleDoesNotExistError(bundleId, config) {
        var _this = _super.call(this, 'Bundle "' + bundleId + '" does not exist.') || this;
        _this.bundleId = bundleId;
        _this.config = config;
        return _this;
    }
    return BundleDoesNotExistError;
}(BaseError));
export { BundleDoesNotExistError };
/**
 *
 */
var VariableDoesNotExistError = /** @class */ (function (_super) {
    __extends(VariableDoesNotExistError, _super);
    // static CODE = 'VariableDoesNotExistError';
    function VariableDoesNotExistError(variableId, config) {
        var _this = _super.call(this, "Variable \"" + variableId + "\" does not exist.") || this;
        _this.variableId = variableId;
        _this.config = config;
        return _this;
    }
    return VariableDoesNotExistError;
}(BaseError));
export { VariableDoesNotExistError };
/**
 * Class representing an iotize studio configuration for a module
 */
var IotizeConfigModel = /** @class */ (function () {
    function IotizeConfigModel(data) {
        if (!data.IOTizeConfiguration) {
            //console.error('Trying to create an IotizeConfigModel with invalid data: ', data);
            throw new InvalidConfigurationError('Key IOTizeConfiguration must exists');
        }
        if (!data.IOTizeConfiguration.IOTizeApp) {
            throw new InvalidConfigurationError('Key IOTizeConfiguration.IOTizeApp must exists');
        }
        if (data.IOTizeConfiguration.IOTizeApp.length === 0 ||
            !data.IOTizeConfiguration.IOTizeApp[0]['$']) {
            throw new InvalidConfigurationError('No IOTizeConfiguration.IOTizeApp defined in this configuration file');
        }
        this.data = data;
    }
    IotizeConfigModel.prototype.toJson = function () {
        return this.data;
    };
    IotizeConfigModel.prototype.getAppConfig = function () {
        return this.data.IOTizeConfiguration.IOTizeApp[0];
    };
    /**
     * Get application name
     */
    IotizeConfigModel.prototype.appName = function () {
        if (!this.getAppConfig().$.Name) {
            throw new InvalidConfigurationError('Application Name is not set');
        }
        return this.getAppConfig().$.Name;
    };
    /**
     * Get config version
     */
    IotizeConfigModel.prototype.version = function () {
        if (!this.getAppConfig().$.Config) {
            throw new InvalidConfigurationError('App config version is not set');
        }
        return this.getAppConfig().$.Config;
    };
    /**
     * Get module serial number
     */
    IotizeConfigModel.prototype.id = function () {
        if (!this.getAppConfig().$.DevIoTizeSN) {
            throw new InvalidConfigurationError('Serial number is not set');
        }
        return this.getAppConfig().$.DevIoTizeSN;
    };
    /**
     * Set module serial number
     * @param id
     */
    IotizeConfigModel.prototype.setId = function (id) {
        this.getAppConfig().$.DevIoTizeSN = id;
        return this;
    };
    /**
     * Alias for this.id();
     */
    IotizeConfigModel.prototype.serialNumber = function () {
        return this.id();
    };
    /**
     * Get an array of all bundles
     */
    IotizeConfigModel.prototype.bundles = function () {
        var e_1, _a;
        if (!this.data.IOTizeConfiguration.IOTizeBundle) {
            return [];
        }
        var bundles = [];
        try {
            for (var _b = __values(this.data.IOTizeConfiguration.IOTizeBundle), _c = _b.next(); !_c.done; _c = _b.next()) { // TODO bug if only one bundle
                var bundle = _c.value;
                bundles.push(new IotizeBundleModel(bundle, this));
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return bundles;
    };
    /**
     * Array of bundles available for a specific profile
     */
    IotizeConfigModel.prototype.bundleAvailableForProfileName = function (profileName) {
        return this.bundles().reduce(function (list, currentValue) {
            if (currentValue.getACLGroup().hasRight(profileName, 'read')) {
                list.push(currentValue);
            }
            return list;
        }, []);
    };
    /**
     * Get an array of all variables
     */
    IotizeConfigModel.prototype.variables = function () {
        var e_2, _a, e_3, _b;
        if (!this.data.IOTizeConfiguration.IOTizeBundle) {
            return [];
        }
        var variables = {};
        try {
            for (var _c = __values(this.data.IOTizeConfiguration.IOTizeBundle), _d = _c.next(); !_d.done; _d = _c.next()) {
                var bundle = _d.value;
                bundle = new IotizeBundleModel(bundle, this);
                try {
                    //            console.log('VAR: ', bundle.variables());
                    for (var _e = (e_3 = void 0, __values(bundle.variables())), _f = _e.next(); !_f.done; _f = _e.next()) {
                        var variable = _f.value;
                        variables[variable.name()] = variable;
                    }
                }
                catch (e_3_1) { e_3 = { error: e_3_1 }; }
                finally {
                    try {
                        if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
                    }
                    finally { if (e_3) throw e_3.error; }
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
            }
            finally { if (e_2) throw e_2.error; }
        }
        var res = [];
        for (var key in variables) {
            res.push(variables[key]);
        }
        return res;
    };
    /**
     * Get bundle by id, if it does not exist, throw BundleDoesNotExistError
     * @throws BundleDoesNotExistError
     * @param id
     */
    IotizeConfigModel.prototype.bundle = function (id) {
        var e_4, _a;
        try {
            for (var _b = __values(this.bundles()), _c = _b.next(); !_c.done; _c = _b.next()) {
                var bundle = _c.value;
                if (bundle.id() === id) {
                    return bundle;
                }
            }
        }
        catch (e_4_1) { e_4 = { error: e_4_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_4) throw e_4.error; }
        }
        throw new BundleDoesNotExistError(id, this);
    };
    IotizeConfigModel.prototype.hasBundle = function (id) {
        var e_5, _a;
        try {
            for (var _b = __values(this.bundles()), _c = _b.next(); !_c.done; _c = _b.next()) {
                var bundle = _c.value;
                if (bundle.id() === id) {
                    return bundle;
                }
            }
        }
        catch (e_5_1) { e_5 = { error: e_5_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_5) throw e_5.error; }
        }
        return false;
    };
    /**
     * Get variable by id, if it does not exist, throw VariableDoesNotExistError
     * @throws VariableDoesNotExistError
     * @param id
     */
    IotizeConfigModel.prototype.variable = function (variableId) {
        var e_6, _a;
        try {
            for (var _b = __values(this.variables()), _c = _b.next(); !_c.done; _c = _b.next()) {
                var variable = _c.value;
                if (variable.id() == variableId) {
                    return variable;
                }
            }
        }
        catch (e_6_1) { e_6 = { error: e_6_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_6) throw e_6.error; }
        }
        throw new VariableDoesNotExistError(variableId, this);
    };
    /**
     * Get an array of all profiles
     */
    IotizeConfigModel.prototype.profiles = function () {
        var e_7, _a;
        if (!this.data.IOTizeConfiguration.IOTizeProfile) {
            return [];
        }
        var profiles = [];
        try {
            for (var _b = __values(this.data.IOTizeConfiguration.IOTizeProfile), _c = _b.next(); !_c.done; _c = _b.next()) { // TODO bug if there is only one profile...
                var profile = _c.value;
                profiles.push(new IotizeProfileModel(profile));
            }
        }
        catch (e_7_1) { e_7 = { error: e_7_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_7) throw e_7.error; }
        }
        return profiles;
    };
    IotizeConfigModel.prototype.description = function () {
        return this.getAppConfig().$.GUI_Description;
    };
    IotizeConfigModel.prototype.name = function () {
        return this.getAppConfig().$.GUI_Name;
    };
    return IotizeConfigModel;
}());
export { IotizeConfigModel };
//# sourceMappingURL=data:application/json;base64, 