import { VariableType } from '@iotize/tap/service/impl/variable';
import { ByteOrder16Bits, ByteOrder32Bits } from '../definitions';
import { toEnumFromValue } from '../util';
var IotizeVariableModel = /** @class */ (function () {
    function IotizeVariableModel(data, iotizeModule, bundle) {
        this.data = data;
        this.iotizeModule = iotizeModule;
        if (bundle) {
            this._bundle = bundle;
        }
    }
    IotizeVariableModel.prototype.toJson = function () {
        return this.data;
    };
    IotizeVariableModel.prototype.module = function () {
        return this.iotizeModule;
    };
    /**
     * Size (int bits) of one element
     */
    IotizeVariableModel.prototype.valueSize = function () {
        return IotizeVariableModel.unitSizeToBitSize(parseInt(this.data.$.Unit));
    };
    IotizeVariableModel.prototype.valueQuantity = function () {
        return Number(this.data.$.Quantity);
    };
    IotizeVariableModel.prototype.name = function () {
        return this.data.$.GUI_Name;
    };
    IotizeVariableModel.prototype.id = function () {
        return this.data.$.Id;
    };
    IotizeVariableModel.prototype.alias = function () {
        return this.data.$.GUI_Alias;
    };
    IotizeVariableModel.prototype.description = function () {
        return this.data.$.GUI_Description;
    };
    IotizeVariableModel.prototype.unit = function () {
        return this.data.$.GUI_DispUnit;
    };
    IotizeVariableModel.prototype.bundle = function () {
        return this._bundle;
    };
    // public type(): VariableType.Data {
    //     if (this.data.$.GUI_float && this.data.$.GUI_float == 1) {
    //         return VariableType.Data.FLOAT32;
    //     } else if (this.data.$.GUI_unsigned && this.data.$.GUI_unsigned == 1) {
    //         return VariableType.Data.UINT32;
    //     } else {
    //         return VariableType.Data.INT32;
    //     }
    // }
    IotizeVariableModel.prototype.dataType = function () {
        var unit = parseInt(this.data.$.Unit, 10);
        if (this.data.$.GUI_float && this.data.$.GUI_float == 1) {
            return VariableType.Data.FLOAT32;
        }
        else if (this.data.$.GUI_unsigned && this.data.$.GUI_unsigned == 1) {
            switch (unit) {
                case 0:
                    return VariableType.Data.BOOLEAN;
                case 1:
                    return VariableType.Data.UINT8;
                case 2:
                    return VariableType.Data.UINT16;
                case 3:
                    return VariableType.Data.UINT32;
                default:
                    throw new Error("Invalid data size \"" + unit + "\" for variable " + this.name());
            }
        }
        else {
            switch (unit) {
                case 0:
                    return VariableType.Data.BOOLEAN;
                case 1:
                    return VariableType.Data.INT8;
                case 2:
                    return VariableType.Data.INT16;
                case 3:
                    return VariableType.Data.INT32;
                default:
                    throw new Error("Invalid data size \"" + unit + "\" for variable " + this.name());
            }
        }
    };
    /**
     * Return attribute by name
     */
    IotizeVariableModel.prototype.attribute = function (name) {
        return this.data.$[name];
    };
    IotizeVariableModel.prototype.byteOrder = function () {
        var guiCoding = this.attribute('GUI_coding');
        if (guiCoding) {
            if (this.valueSize() === 16) {
                return toEnumFromValue(ByteOrder16Bits, guiCoding);
            }
            else if (this.valueSize() === 32) {
                return toEnumFromValue(ByteOrder32Bits, guiCoding);
            }
        }
        return undefined;
    };
    /**
     * Convert unit size found in configuration file into a number of bits
     */
    IotizeVariableModel.unitSizeToBitSize = function (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);
        }
    };
    return IotizeVariableModel;
}());
export { IotizeVariableModel };
//# sourceMappingURL=data:application/json;base64, 