import { converters } from '@iotize/tap/service/core';
import { SerialSettings, TargetCanProtocolConfiguration, } from '@iotize/tap/service/impl/target';
import { ConfigKeyNotSupportedByFirmwareVersion, TapConfiguratorError, } from './errors';
import { WRITE_CONFIGURATOR_CONFIG as WRITE_CONFIGURATOR_DEFAULT_CONFIG, } from './generated/write-configurator-config';
import { configValueToBytes, isTapFirmwareVersionGreaterThanOrEqual, toEnum, } from './util';
const ɵ0 = (context) => {
    var _a, _b, _c, _d, _e;
    const protocolType = ((_b = (_a = context.config) === null || _a === void 0 ? void 0 : _a.target) === null || _b === void 0 ? void 0 : _b.type) === 'MCU'
        ? (_e = (_d = (_c = context.config) === null || _c === void 0 ? void 0 : _c.target) === null || _d === void 0 ? void 0 : _d.protocol) === null || _e === void 0 ? void 0 : _e.type
        : 'MODBUS';
    switch (protocolType) {
        case 'NONE':
        case 'SWD':
        // case 'JTAG':
        case 'GPIO':
        case 'CUSTOM':
            return TapConfiguratorError.invalidConfiguration(`Target protocol "${protocolType}" does not accept any additional configuration`);
        case 'S3P':
            return context.service.target.putS3PConfigurationCall(context.value);
        case 'MODBUS_TCP':
            if (context.firmwareVersion) {
                if (!isTapFirmwareVersionGreaterThanOrEqual(context.firmwareVersion, '2.6')) {
                    return new ConfigKeyNotSupportedByFirmwareVersion('target.protocol.config', context.firmwareVersion, {
                        minApiVersion: '2.6',
                        maxApiVersion: undefined,
                    });
                }
            }
            return context.service.target.putModbusTcpConfigurationCall(context.value);
        case 'MODBUS':
        case 'SERIAL':
            const tapnpassConfigJson = context.value;
            const tapnpassConfigApi = tapnpassConfigJsonToApi(tapnpassConfigJson);
            return context.service.target.putSerialModbusConfigurationCall(tapnpassConfigApi);
        case 'CAN':
            if (context.firmwareVersion) {
                if (!isTapFirmwareVersionGreaterThanOrEqual(context.firmwareVersion, '2.6')) {
                    return new ConfigKeyNotSupportedByFirmwareVersion('target.protocol.config', context.firmwareVersion, {
                        minApiVersion: '2.6',
                        maxApiVersion: undefined,
                    });
                }
            }
            const canConfigJson = context.value;
            const canConfigApi = canConfigJsonToApi(canConfigJson);
            return context.service.target.putCanConfigurationCall(canConfigApi);
        default:
            return TapConfiguratorError.invalidConfiguration(`Target protocol "${protocolType}" is not supported by this version of the configurator`);
    }
}, ɵ1 = (context) => {
    var _a;
    if (context.firmwareVersion) {
        if (!isTapFirmwareVersionGreaterThanOrEqual(context.firmwareVersion, '2.6' // TODO check version
        )) {
            return new ConfigKeyNotSupportedByFirmwareVersion('slaveServer', context.firmwareVersion, {
                minApiVersion: '2.6',
                maxApiVersion: undefined,
            });
        }
    }
    const protocolType = (_a = context.value) === null || _a === void 0 ? void 0 : _a.type;
    switch (protocolType) {
        case undefined:
            throw new Error(`Undefined property`);
        case 'SPI':
            return context.service.slaveServer.putSpiProtocolConfigurationCall();
        case 'MODBUS_TCP':
            const modbusTcpConfig = context.value
                .config;
            return context.service.slaveServer.putModbusTcpProtocolConfigurationCall(modbusTcpConfig);
        case 'MODBUS_RTU':
            const tapnpassConfigJson = context.value
                .config;
            const tapnpassConfigApi = tapnpassConfigJsonToApi(tapnpassConfigJson);
            return context.service.slaveServer.putModbusRtuProtocolConfigurationCall(tapnpassConfigApi);
        case 'CAN':
            const canConfigJson = context.value.config;
            const canConfigApi = canConfigJsonToApi(canConfigJson);
            return context.service.slaveServer.putCanProtocolConfigurationCall(canConfigApi);
        default:
            return TapConfiguratorError.invalidConfiguration(`Slave server protocol "${protocolType}" is not supported by this version of the configurator`);
    }
};
const WRITE_CONFIGURATOR_CUSTOM_CONFIG = {
    'target.protocol.config': ɵ0,
    slaveServer: ɵ1,
};
export const WRITE_CONFIGURATOR_CONFIG = Object.assign(Object.assign({}, WRITE_CONFIGURATOR_DEFAULT_CONFIG), WRITE_CONFIGURATOR_CUSTOM_CONFIG);
const DEFAULT_MODBUS_CONFIG = {
    offsetFirstRegister: false,
    slave: 0,
};
const DEFAULT_USB_CONFIG = {
    aliases: [],
};
function tapnpassConfigJsonToApi(jsonConfig) {
    return {
        modbus: jsonConfig.modbus || DEFAULT_MODBUS_CONFIG,
        serial: {
            baudRate: jsonConfig.serial.baudRate,
            bitParity: toEnum(jsonConfig.serial.bitParity, SerialSettings.BitParity),
            dataBits: toEnum(jsonConfig.serial.dataBits, SerialSettings.DataBits),
            handshakeDelimiter: toEnum(jsonConfig.serial.handshakeDelimiter, SerialSettings.HandshakeDelimiter),
            hardwareFlowControl: toEnum(jsonConfig.serial.hardwareFlowControl, SerialSettings.HardwareFlowControl),
            identification: jsonConfig.serial.identification,
            ledStatus: jsonConfig.serial.ledStatus,
            physicalPort: toEnum(jsonConfig.serial.physicalPort, SerialSettings.PhysicalPort),
            stopBits: toEnum(jsonConfig.serial.stopBits, SerialSettings.StopBits),
            timeout: jsonConfig.serial.timeout,
        },
        usb: jsonConfig.usb || DEFAULT_USB_CONFIG,
    };
}
function canConfigJsonToApi(canConfigJson) {
    const canConfigApi = {
        code: converters.uint32.decode(configValueToBytes(canConfigJson.code)),
        mask: converters.uint32.decode(configValueToBytes(canConfigJson.mask)),
        filter: toEnum(canConfigJson.filter, TargetCanProtocolConfiguration.Filter),
        frequency: canConfigJson.frequency,
        mode: toEnum(canConfigJson.mode, TargetCanProtocolConfiguration.Mode),
    };
    return canConfigApi;
}
export { ɵ0, ɵ1 };
//# sourceMappingURL=data:application/json;base64, 