import { TapStreamReader, TapStreamWriter } from '@iotize/tap/client/impl';
import { ASCIIControl } from './ascii-control';
import { DBIOT_BYTE_ORDER_CONVERTER } from './dbiot-compressor/dbiot-byte-order-converter';
import { DBIOT_BOOLEAN_CONVERTER, DBIOT_FLOAT32_CONVERTER, DBIOT_FLOAT64_CONVERTER, DBIOT_UINT_CONVERTER, DBIOT_UTF8_CONVERTER, } from './json-compressor/tlv-json-base-type-compressor';
import { TLVMappedJsonArrayOfObjectCompressor, TLVMappedJsonObjectCompressor, } from './json-compressor/tlv-mapped-json-compressor';
var ButtonSizeEnum;
(function (ButtonSizeEnum) {
    ButtonSizeEnum[ButtonSizeEnum["DEFAULT"] = 0] = "DEFAULT";
    ButtonSizeEnum[ButtonSizeEnum["SMALL"] = 1] = "SMALL";
    ButtonSizeEnum[ButtonSizeEnum["LARGE"] = 2] = "LARGE";
})(ButtonSizeEnum || (ButtonSizeEnum = {}));
const DBIOT_INNER_CONVERTER = new TLVMappedJsonObjectCompressor({
    encoding: {
        code: 'E',
        converter: new TLVMappedJsonObjectCompressor({
            scaling: {
                code: 'x',
                converter: DBIOT_FLOAT32_CONVERTER,
            },
            bitLength: {
                code: 'b',
                converter: DBIOT_UINT_CONVERTER,
            },
            offset: {
                code: 'o',
                converter: DBIOT_FLOAT32_CONVERTER,
            },
        }),
    },
    tap: {
        code: 'T',
        converter: new TLVMappedJsonObjectCompressor({
            address: {
                code: 'a',
                converter: DBIOT_UINT_CONVERTER,
            },
            valueAcquisitionPeriod: {
                code: 's',
                converter: DBIOT_UINT_CONVERTER,
            },
        }),
    },
    dashboard: {
        code: 'D',
        converter: new TLVMappedJsonObjectCompressor({
            offset: {
                code: 'O',
                converter: DBIOT_FLOAT64_CONVERTER,
            },
            scaling: {
                code: 'X',
                converter: DBIOT_FLOAT64_CONVERTER,
            },
            readable: {
                code: 'R',
                converter: DBIOT_BOOLEAN_CONVERTER,
            },
            writable: {
                code: 'W',
                converter: DBIOT_BOOLEAN_CONVERTER,
            },
            format: {
                code: 'F',
                converter: DBIOT_UTF8_CONVERTER,
            },
            name: {
                code: 'N',
                converter: DBIOT_UTF8_CONVERTER,
            },
            unit: {
                code: 'U',
                converter: DBIOT_UTF8_CONVERTER,
            },
            byteOrder: {
                code: 'B',
                converter: DBIOT_BYTE_ORDER_CONVERTER,
            },
            component: {
                code: 'C',
                converter: new TLVMappedJsonObjectCompressor({
                    'tap-variable-number': {
                        code: 't',
                        converter: new TLVMappedJsonObjectCompressor({}),
                    },
                    'tap-variable-range': {
                        code: 'r',
                        converter: new TLVMappedJsonObjectCompressor({
                            min: {
                                code: 'm',
                                converter: DBIOT_FLOAT64_CONVERTER,
                            },
                            max: {
                                code: 'M',
                                converter: DBIOT_FLOAT64_CONVERTER,
                            },
                            step: {
                                code: 's',
                                converter: DBIOT_FLOAT64_CONVERTER,
                            },
                            writeValueForEveryChange: {
                                code: 'w',
                                converter: DBIOT_BOOLEAN_CONVERTER,
                            },
                        }),
                    },
                    'tap-variable-linear-gauge': {
                        code: 'G',
                        converter: new TLVMappedJsonObjectCompressor({
                            min: {
                                code: 'm',
                                converter: DBIOT_FLOAT64_CONVERTER,
                            },
                            max: {
                                code: 'M',
                                converter: DBIOT_FLOAT64_CONVERTER,
                            },
                        }),
                    },
                    'tap-variable-gauge': {
                        code: 'g',
                        converter: new TLVMappedJsonObjectCompressor({
                            min: {
                                code: 'm',
                                converter: DBIOT_FLOAT64_CONVERTER,
                            },
                            max: {
                                code: 'M',
                                converter: DBIOT_FLOAT64_CONVERTER,
                            },
                        }),
                    },
                    'tap-variable-line-chart': {
                        code: 'l',
                        converter: new TLVMappedJsonObjectCompressor({}),
                    },
                    'tap-variable-bar-chart': {
                        code: 'B',
                        converter: new TLVMappedJsonObjectCompressor({}),
                    },
                    'tap-variable-table': {
                        code: 'T',
                        converter: new TLVMappedJsonObjectCompressor({}),
                    },
                    'tap-variable-bits-editor': {
                        code: 'b',
                        converter: new TLVMappedJsonObjectCompressor({
                            bitsTemplate: {
                                code: 'T',
                                converter: new TLVMappedJsonArrayOfObjectCompressor({
                                    fields: [
                                        {
                                            fieldName: 'index',
                                            converter: DBIOT_UINT_CONVERTER,
                                        },
                                        {
                                            fieldName: 'label',
                                            converter: DBIOT_UTF8_CONVERTER,
                                        },
                                    ],
                                }),
                            },
                        }),
                    },
                    'tap-variable-buttons': {
                        code: 'U',
                        converter: new TLVMappedJsonObjectCompressor({
                            buttons: {
                                code: 'b',
                                converter: new TLVMappedJsonArrayOfObjectCompressor({
                                    fields: [
                                        {
                                            fieldName: 'value',
                                            converter: DBIOT_FLOAT64_CONVERTER,
                                        },
                                        {
                                            fieldName: 'label',
                                            converter: DBIOT_UTF8_CONVERTER,
                                        },
                                    ],
                                }),
                            },
                        }),
                    },
                    'tap-variable-push-button': {
                        code: 'P',
                        converter: new TLVMappedJsonObjectCompressor({
                            mouseDownValue: {
                                code: 'd',
                                converter: DBIOT_FLOAT64_CONVERTER,
                            },
                            mouseUpValue: {
                                code: 'U',
                                converter: DBIOT_FLOAT64_CONVERTER,
                            },
                            value: {
                                code: 'u',
                                converter: DBIOT_FLOAT64_CONVERTER,
                            },
                            buttonText: {
                                code: 'l',
                                converter: DBIOT_UTF8_CONVERTER,
                            },
                            // size: {
                            //   code: 's',
                            //   converter: new TLVMappedEnumConverter(ButtonSizeEnum),
                            // },
                            icon: {
                                code: 'i',
                                converter: DBIOT_UTF8_CONVERTER,
                            },
                        }),
                    },
                    'tap-variable-select': {
                        code: 'S',
                        converter: new TLVMappedJsonObjectCompressor({
                            options: {
                                code: 'o',
                                converter: new TLVMappedJsonArrayOfObjectCompressor({
                                    fields: [
                                        {
                                            fieldName: 'value',
                                            converter: DBIOT_FLOAT64_CONVERTER,
                                        },
                                        {
                                            fieldName: 'text',
                                            converter: DBIOT_UTF8_CONVERTER,
                                        },
                                    ],
                                }),
                            },
                        }),
                    },
                }),
            },
        }),
    },
    cloud: {
        code: 'C',
        converter: new TLVMappedJsonObjectCompressor({
            name: {
                code: 'N',
                converter: DBIOT_UTF8_CONVERTER,
            },
            optional: {
                code: 'p',
                converter: DBIOT_UINT_CONVERTER,
            },
            uploadPeriod: {
                code: 't',
                converter: DBIOT_UINT_CONVERTER,
            },
            valueDelta: {
                code: 'd',
                converter: DBIOT_FLOAT32_CONVERTER,
            },
            alarmMinValue: {
                code: 'l',
                converter: DBIOT_FLOAT32_CONVERTER,
            },
            alarmMaxValue: {
                code: 'h',
                converter: DBIOT_FLOAT32_CONVERTER,
            },
        }),
    },
});
export class DBIOTVarConfigConverter {
    encode(mainConfig) {
        if (!mainConfig) {
            return new Uint8Array();
        }
        const writter = new TapStreamWriter();
        writter.writeU1(ASCIIControl.DEVICE_CONTROL_1);
        DBIOT_INNER_CONVERTER.encode(mainConfig, writter);
        return writter.toBytes;
    }
    decode(streamOrBuffer) {
        const stream = TapStreamReader.create(streamOrBuffer);
        if (stream.byteLeft === 0) {
            return undefined;
        }
        let char;
        if (!stream.isEof() && char !== ASCIIControl.DEVICE_CONTROL_1) {
            char = stream.readU1();
        }
        if (char === ASCIIControl.DEVICE_CONTROL_1) {
            try {
                return DBIOT_INNER_CONVERTER.decode(stream);
            }
            catch (err) {
                console.warn(`Failed to restore variable configuration`, err);
            }
        }
        return undefined;
    }
}
//# sourceMappingURL=data:application/json;base64, 