"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.integroVariantOptions = exports.parseIntegroVariantBuffer = exports.unpackIntegroVariantBuffer = exports.integroVariantPackFormat = void 0;
const python_struct_1 = require("python-struct");
/**
 * Header information (128 bytes):
 * - 4B: File Info(Version)
 * - 64B: Signature
 * - 12B: NetX Chip - ID
 * - 24B: Integro device serial number
 * - 8B: MAC(reserved)
 * - 4B: Customer ID
 * - 2B: Operation Mode (Passive=0x51, Trial=0x53, Prod=0x55, Live=0x57)
 * - 10B: Reserved
 *
 * Variant data section:
 * - 2B: (Reserved for sw use)
 * - 2B: Number of options
 * - 2B: option code 1
 * - 2B: option code 2
 * - 2B: for each following option code...
 *
 * All options are specified as 'H' (unsigned short, 2 bytes).
 * Number 'H' added to the format depends on the size of Integro Variant buffer.
 */
exports.integroVariantPackFormat = '@L64B12B24s8cLH10B';
function unpackIntegroVariantBuffer(buffer) {
    const optionCodePackFormatLength = (buffer.length - 128) / 2;
    const format = exports.integroVariantPackFormat.padEnd(exports.integroVariantPackFormat.length + optionCodePackFormatLength, 'H');
    return (0, python_struct_1.unpack)(format, buffer);
}
exports.unpackIntegroVariantBuffer = unpackIntegroVariantBuffer;
function parseIntegroVariantBuffer(buffer) {
    const data = unpackIntegroVariantBuffer(buffer);
    const version = data[0];
    const signature = data.slice(1, 65);
    const chipId = data.slice(65, 77);
    const serialNumber = data[77];
    // Extract 6-byte MAC address starting at offset 80, skipping bytes 78 and 79 (both 0x00 padding)
    const macAddress = data.slice(80, 86)
        .map((c) => c.charCodeAt(0).toString(16).padStart(2, '0').toUpperCase())
        .join(':');
    const customerId = data[86];
    const operationMode = data[87];
    const reserved = data.slice(88, 98);
    // data[98] is reserved for SW use
    const numberOfOptionIds = data[99];
    const optionIds = data.slice(100);
    if (numberOfOptionIds !== optionIds.length) {
        throw new Error(`The specified number of options (${numberOfOptionIds}) doesn't match the actual number of options (${optionIds.length}).`);
    }
    const options = optionIds.map((value, index) => {
        const optionId = value;
        const maxOptionId = exports.integroVariantOptions.reduce((max, option) => Math.max(max, option.id), -1);
        if (optionId < 0 || optionId > maxOptionId) {
            throw new Error(`Invalid option ID (${optionId}) at position ${index}. Max allowed option ID is ${maxOptionId}.`);
        }
        return exports.integroVariantOptions.find((option) => option.id === optionId);
    });
    options.sort((a, b) => a.id - b.id);
    const selectedOptionId = {
        fieldbusProtocol: [],
        phaseCurrentSensorGain: [],
        currentLimits: [],
        multiturnAbility: [],
        encoderResolution: [],
        cyclicMode: [],
        performanceLimitation: [],
        digitalIOsAllowed: [],
        encoderConnectorsAllowed: [],
        analogInputsAllowed: [],
        dcClock: [],
    };
    options.forEach((option) => selectedOptionId[option.key].push(option.id));
    return {
        header: {
            version,
            signature,
            chipId,
            serialNumber,
            macAddress,
            customerId,
            operationMode,
            reserved,
        },
        options,
        selectedOptionId,
    };
}
exports.parseIntegroVariantBuffer = parseIntegroVariantBuffer;
exports.integroVariantOptions = [
    {
        category: 'Fieldbus Protocol',
        id: 0,
        meaning: 'EtherNet/IP - CiA 402',
        socVariables: [],
        incompatibleOptionIds: [1, 2, 3, 39],
        mpnSegmentCodes: ['EI'],
        key: 'fieldbusProtocol',
    },
    {
        category: 'Fieldbus Protocol',
        id: 1,
        meaning: 'EtherCAT',
        socVariables: [],
        incompatibleOptionIds: [0, 2, 3, 39],
        mpnSegmentCodes: ['EC'],
        key: 'fieldbusProtocol',
    },
    {
        category: 'Fieldbus Protocol',
        id: 2,
        meaning: 'PROFINET - CiA 402',
        socVariables: [],
        incompatibleOptionIds: [0, 1, 3, 39],
        mpnSegmentCodes: ['EP'],
        key: 'fieldbusProtocol',
    },
    {
        category: 'Fieldbus Protocol',
        id: 3,
        meaning: 'CAN',
        socVariables: [],
        incompatibleOptionIds: [0, 1, 2, 39],
        mpnSegmentCodes: ['CB'],
        key: 'fieldbusProtocol',
    },
    {
        category: 'Phase current sensor gain',
        id: 4,
        meaning: 'gain for size 60',
        socVariables: ['adc_phase_current_sensor_gain'],
        incompatibleOptionIds: [5],
        mpnSegmentCodes: ['025', '060'],
        key: 'phaseCurrentSensorGain',
    },
    {
        category: 'Phase current sensor gain',
        id: 5,
        meaning: 'gain for size 80',
        socVariables: ['adc_phase_current_sensor_gain'],
        incompatibleOptionIds: [4],
        mpnSegmentCodes: ['050', '120'],
        key: 'phaseCurrentSensorGain',
    },
    {
        category: 'Current Limits',
        id: 6,
        meaning: '10/25',
        socVariables: [
            'drive_max_continuous_current_mA',
            'drive_max_current_mA',
            'drive_max_mechanical_power_watts',
            'protection_hardware_overcurrent_mA',
            'fet_max_accumulated_heat_cold_amp_squared_second',
            'fet_max_accumulated_heat_hot_amp_squared_second',
            'fet_cold_temperature_degree_celsius',
            'fet_hot_temperature_degree_celsius',
            'fet_rated_current_cold_i2t_integration_deci_amp',
            'cool_down_period_fet_overheat_s',
            'cool_down_period_fet_startup_s',
            'nominal_drive_temperature_degree_celsius',
            'DC_current_peak_ripple_32_kHz_threshold_mA',
            'DC_current_peak_ripple_64_kHz_threshold_mA',
            'AC_current_threshold_mA',
        ],
        incompatibleOptionIds: [5, 7, 8, 9],
        mpnSegmentCodes: ['025'],
        key: 'currentLimits',
    },
    {
        category: 'Current Limits',
        id: 7,
        meaning: '20/60',
        socVariables: [
            'drive_max_continuous_current_mA',
            'drive_max_current_mA',
            'drive_max_mechanical_power_watts',
            'protection_hardware_overcurrent_mA',
            'fet_max_accumulated_heat_cold_amp_squared_second',
            'fet_max_accumulated_heat_hot_amp_squared_second',
            'fet_cold_temperature_degree_celsius',
            'fet_hot_temperature_degree_celsius',
            'fet_rated_current_cold_i2t_integration_deci_amp',
            'cool_down_period_fet_overheat_s',
            'cool_down_period_fet_startup_s',
            'nominal_drive_temperature_degree_celsius',
            'DC_current_peak_ripple_32_kHz_threshold_mA',
            'DC_current_peak_ripple_64_kHz_threshold_mA',
            'AC_current_threshold_mA',
        ],
        incompatibleOptionIds: [5, 6, 8, 9],
        mpnSegmentCodes: ['060'],
        key: 'currentLimits',
    },
    {
        category: 'Current Limits',
        id: 8,
        meaning: '25/50',
        socVariables: [
            'drive_max_continuous_current_mA',
            'drive_max_current_mA',
            'drive_max_mechanical_power_watts',
            'protection_hardware_overcurrent_mA',
            'fet_max_accumulated_heat_cold_amp_squared_second',
            'fet_max_accumulated_heat_hot_amp_squared_second',
            'fet_cold_temperature_degree_celsius',
            'fet_hot_temperature_degree_celsius',
            'fet_rated_current_cold_i2t_integration_deci_amp',
            'cool_down_period_fet_overheat_s',
            'cool_down_period_fet_startup_s',
            'nominal_drive_temperature_degree_celsius',
            'DC_current_peak_ripple_32_kHz_threshold_mA',
            'DC_current_peak_ripple_64_kHz_threshold_mA',
            'AC_current_threshold_mA',
        ],
        incompatibleOptionIds: [4, 6, 7, 9],
        mpnSegmentCodes: ['050'],
        key: 'currentLimits',
    },
    {
        category: 'Current Limits',
        id: 9,
        meaning: '40/120',
        socVariables: [
            'drive_max_continuous_current_mA',
            'drive_max_current_mA',
            'drive_max_mechanical_power_watts',
            'protection_hardware_overcurrent_mA',
            'fet_max_accumulated_heat_cold_amp_squared_second',
            'fet_max_accumulated_heat_hot_amp_squared_second',
            'fet_cold_temperature_degree_celsius',
            'fet_hot_temperature_degree_celsius',
            'fet_rated_current_cold_i2t_integration_deci_amp',
            'cool_down_period_fet_overheat_s',
            'cool_down_period_fet_startup_s',
            'nominal_drive_temperature_degree_celsius',
            'DC_current_peak_ripple_32_kHz_threshold_mA',
            'DC_current_peak_ripple_64_kHz_threshold_mA',
            'AC_current_threshold_mA',
        ],
        incompatibleOptionIds: [4, 6, 7, 8],
        mpnSegmentCodes: ['120'],
        key: 'currentLimits',
    },
    {
        category: 'Multiturn ability',
        id: 10,
        meaning: 'MT',
        socVariables: ['is_integrated_encoder_mt_used'],
        incompatibleOptionIds: [11],
        mpnSegmentCodes: ['ML', 'MM', 'MH', 'MX'],
        key: 'multiturnAbility',
    },
    {
        category: 'Multiturn ability',
        id: 11,
        meaning: 'ST',
        socVariables: ['is_integrated_encoder_mt_used'],
        incompatibleOptionIds: [10],
        mpnSegmentCodes: ['SL', 'SM', 'SH', 'SX'],
        key: 'multiturnAbility',
    },
    {
        category: 'Encoder Resolution',
        id: 12,
        meaning: '12 bits',
        socVariables: ['integrated_encoder_st_bits_used', 'should_integrated_encoder_noise_be_added'],
        incompatibleOptionIds: [13, 14, 15, 20],
        mpnSegmentCodes: ['ML', 'SL'],
        key: 'encoderResolution',
    },
    {
        category: 'Encoder Resolution',
        id: 13,
        meaning: '14 bits',
        socVariables: ['integrated_encoder_st_bits_used', 'should_integrated_encoder_noise_be_added'],
        incompatibleOptionIds: [12, 14, 15, 20],
        mpnSegmentCodes: ['MM', 'SM'],
        key: 'encoderResolution',
    },
    {
        category: 'Encoder Resolution',
        id: 14,
        meaning: '18 bits',
        socVariables: ['integrated_encoder_st_bits_used', 'should_integrated_encoder_noise_be_added'],
        incompatibleOptionIds: [12, 13, 15, 20],
        mpnSegmentCodes: ['MH', 'SH'],
        key: 'encoderResolution',
    },
    {
        category: 'Cyclic mode',
        id: 16,
        meaning: 'inactive',
        socVariables: ['are_cyclic_opmodes_allowed'],
        incompatibleOptionIds: [17],
        mpnSegmentCodes: ['00'],
        key: 'cyclicMode',
    },
    {
        category: 'Cyclic mode',
        id: 17,
        meaning: 'active',
        socVariables: ['are_cyclic_opmodes_allowed'],
        incompatibleOptionIds: [16],
        mpnSegmentCodes: ['01'],
        key: 'cyclicMode',
    },
    {
        category: 'Performance Limitation',
        id: 18,
        meaning: 'Input frequency is limited to 4Hz to make the drive unable to follow highly dynamic trajectories',
        socVariables: ['is_input_frequency_limited'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'performanceLimitation',
    },
    {
        category: 'Performance Limitation',
        id: 19,
        meaning: 'Cyclic accelerations are limited to a low value to make the drive unable to react to large travel demands quickly',
        socVariables: ['are_accelerations_limited'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'performanceLimitation',
    },
    {
        category: 'Encoder Resolution',
        id: 20,
        meaning: '11 bits',
        socVariables: ['integrated_encoder_st_bits_used', 'should_integrated_encoder_noise_be_added'],
        incompatibleOptionIds: [12, 13, 14, 15],
        mpnSegmentCodes: [],
        key: 'encoderResolution',
    },
    {
        category: 'Digital IOs allowed',
        id: 21,
        meaning: 'None (0b0000)',
        socVariables: ['dio_allowed_bitmap'],
        incompatibleOptionIds: [22, 23, 24],
        mpnSegmentCodes: [],
        key: 'digitalIOsAllowed',
    },
    {
        category: 'Digital IOs allowed',
        id: 22,
        meaning: 'DIO3, DIO4 (0b1100)',
        socVariables: ['dio_allowed_bitmap'],
        incompatibleOptionIds: [21, 23, 24],
        mpnSegmentCodes: [],
        key: 'digitalIOsAllowed',
    },
    {
        category: 'Digital IOs allowed',
        id: 23,
        meaning: 'DIO1, DIO2, DIO4 (0b1011)',
        socVariables: ['dio_allowed_bitmap'],
        incompatibleOptionIds: [21, 22, 24],
        mpnSegmentCodes: [],
        key: 'digitalIOsAllowed',
    },
    {
        category: 'Digital IOs allowed',
        id: 24,
        meaning: 'DIO1, DIO2, DIO3 (0b0111)',
        socVariables: ['dio_allowed_bitmap'],
        incompatibleOptionIds: [21, 22, 23],
        mpnSegmentCodes: [],
        key: 'digitalIOsAllowed',
    },
    {
        category: 'Encoder connectors allowed',
        id: 25,
        meaning: 'Only integrated (0b01)',
        socVariables: ['encoder_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'encoderConnectorsAllowed',
    },
    {
        category: 'Analog inputs allowed',
        id: 26,
        meaning: 'Only motor temperature / internal (0b001)',
        socVariables: ['analog_inputs_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'analogInputsAllowed',
    },
    {
        category: 'DC clock',
        id: 27,
        meaning: 'Disable the DC clock synchronization',
        socVariables: ['is_dc_clock_disabled'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'dcClock',
    },
    {
        category: 'Digital IOs allowed',
        id: 28,
        meaning: 'Disable DI1',
        socVariables: ['di_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'digitalIOsAllowed',
    },
    {
        category: 'Digital IOs allowed',
        id: 29,
        meaning: 'Disable DO1',
        socVariables: ['do_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'digitalIOsAllowed',
    },
    {
        category: 'Digital IOs allowed',
        id: 30,
        meaning: 'Disable DI2',
        socVariables: ['di_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'digitalIOsAllowed',
    },
    {
        category: 'Digital IOs allowed',
        id: 31,
        meaning: 'Disable DO2',
        socVariables: ['do_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'digitalIOsAllowed',
    },
    {
        category: 'Digital IOs allowed',
        id: 32,
        meaning: 'Disable DI3',
        socVariables: ['di_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'digitalIOsAllowed',
    },
    {
        category: 'Digital IOs allowed',
        id: 33,
        meaning: 'Disable DI4',
        socVariables: ['di_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'digitalIOsAllowed',
    },
    {
        category: 'Encoder connectors allowed',
        id: 34,
        meaning: 'Disable encoder connector 1 (Integrated encoder)',
        socVariables: ['encoder_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'encoderConnectorsAllowed',
    },
    {
        category: 'Encoder connectors allowed',
        id: 35,
        meaning: 'Disable encoder connector 2 (External encoder)',
        socVariables: ['encoder_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'encoderConnectorsAllowed',
    },
    {
        category: 'Analog inputs allowed',
        id: 36,
        meaning: 'Disable AI Internal (Motor temperature sensor)',
        socVariables: ['analog_inputs_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'analogInputsAllowed',
    },
    {
        category: 'Analog inputs allowed',
        id: 37,
        meaning: 'Disable AI 1',
        socVariables: ['analog_inputs_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'analogInputsAllowed',
    },
    {
        category: 'Analog inputs allowed',
        id: 38,
        meaning: 'Disable AI 2',
        socVariables: ['analog_inputs_allowed_bitmap'],
        incompatibleOptionIds: [],
        mpnSegmentCodes: [],
        key: 'analogInputsAllowed',
    },
    {
        category: 'Fieldbus Protocol',
        id: 39,
        meaning: 'EtherNet/IP - LXM',
        socVariables: [],
        incompatibleOptionIds: [0, 1, 2, 3],
        mpnSegmentCodes: ['EI+CSE'],
        key: 'fieldbusProtocol',
    },
];
//# sourceMappingURL=integro-variant.js.map