"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const device_client_js_1 = require("@iotize/device-client.js");
const ByteOrderMap32Bits = {
    0: "B3_B2_B1_B0",
    1: "B1_B0_B3_B2",
    2: "B2_B3_B0_B1",
    3: "B0_B1_B2_B3"
};
const ByteOrderMap16Bits = {
    0: "B1_B0",
    1: "B0_B1"
};
const ByteOrderMapping = {
    2: ByteOrderMap16Bits,
    3: ByteOrderMap32Bits
};
class TypeConverterHelper {
    // public static intToColor(num: number) {
    //     num >>>= 0;
    //     var b = num & 0xFF,
    //         g = (num & 0xFF00) >>> 8,
    //         r = (num & 0xFF0000) >>> 16,
    //         // a = ( (num & 0xFF000000) >>> 24 ) / 255 ;
    //         a = 1;
    //     return "rgba(" + [r, g, b, a].join(",") + ")";
    // }
    static deduceConverter(variable) {
        let baseConverter = TypeConverterHelper.deduceBaseTypeConverter(variable);
        if (variable.length > 1) {
            return `new ArrayConverter(${baseConverter}, {
                sizeOfItem: ${TypeConverterHelper.byteSize(variable)}
            })`;
        }
        else {
            return baseConverter;
        }
    }
    static byteSize(variable) {
        switch (variable.type) {
            case "FLOAT32":
            case "UINT32":
            case "INT32":
                return 32;
            case "INT16":
            case "UINT16":
                return 16;
            case "UINT8":
            case "INT8":
                return 8;
            default:
                throw new Error(`Unknown byte size for type "${variable.type}"`);
        }
    }
    static deduceBaseTypeConverter(variable) {
        let converter = "";
        switch (variable.type) {
            case device_client_js_1.VariableType[device_client_js_1.VariableType.BOOLEAN]:
                converter = "BooleanConverter.instance()";
                break;
            case device_client_js_1.VariableType[device_client_js_1.VariableType.INT8]:
                converter = "NumberConverter.int8Instance()";
                break;
            case device_client_js_1.VariableType[device_client_js_1.VariableType.INT16]:
                converter = "NumberConverter.int16Instance()";
                break;
            case device_client_js_1.VariableType[device_client_js_1.VariableType.INT32]:
                converter = "NumberConverter.int32Instance()";
                break;
            case device_client_js_1.VariableType[device_client_js_1.VariableType.UINT8]:
                converter = "NumberConverter.uint8Instance()";
                break;
            case device_client_js_1.VariableType[device_client_js_1.VariableType.UINT16]:
                converter = "NumberConverter.uint16Instance()";
                break;
            case device_client_js_1.VariableType[device_client_js_1.VariableType.UINT32]:
                converter = "NumberConverter.uint32Instance()";
                break;
            case device_client_js_1.VariableType[device_client_js_1.VariableType.FLOAT32]:
                converter = "FloatConverter.instance32()";
                break;
            case device_client_js_1.VariableType[device_client_js_1.VariableType.STRING]:
                converter = "StringConverter.instance(\"ascii\")";
                break;
            default:
                converter = "undefined";
        }
        if (variable._options && variable._options.byteOrder != null) {
            converter = `PipeConverter
                .pipe(new ByteSwapConverter("${variable._options.byteOrder}"))
                .pipe(${converter})`;
        }
        return converter;
    }
}
exports.TypeConverterHelper = TypeConverterHelper;
TypeConverterHelper.UnitSizeMap = {
    0: 1,
    1: 8,
    2: 16,
    3: 32
};
//# sourceMappingURL=type-converter.js.map