"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseIntegroIntegratedEncoderError = exports.getIntegroIntegratedEncoderErrorDescription = exports.splitIntegroIntegratedEncoderErrorValue = exports.integroIntegratedEncoderErrorCodeDescriptions = void 0;
/**
 * Predefined list of known encoder error codes and their detailed descriptions.
 *
 * Each entry provides structured information about a specific error code, including:
 * - `errorCode`: The numeric 16-bit error code.
 * - `name`: A short, human-readable name of the error.
 * - `description`: Detailed explanation of the error.
 * - `category`: Classification of the error type (e.g., initialization, signal, command).
 * - `behavior`: Expected behavior or consequence when this error occurs.
 *
 * This array is used by functions like `getIntegroIntegratedEncoderErrorDescription` to
 * resolve a numeric error code into a structured, human-readable format.
 */
exports.integroIntegratedEncoderErrorCodeDescriptions = [
    {
        errorCode: 0x0000,
        name: 'No Error',
        description: 'No error was detected.',
        category: 'None',
        behavior: 'None',
    },
    {
        errorCode: 0x0001,
        name: 'ADC1 Calibration not finished',
        description: 'ADC1 calibration on initialization was never finished and aborted. Signal processing did not get started.',
        category: 'Initialization error (µC Peripheral)',
        behavior: 'No signal processing is done. No valid position will be output. Error bit set. Error cannot be reset by command.',
    },
    {
        errorCode: 0x0002,
        name: 'ADC2 Calibration not finished',
        description: 'ADC2 calibration on initialization was never finished and aborted. Signal processing did not get started.',
        category: 'Initialization error (µC Peripheral)',
        behavior: 'No signal processing is done. No valid position will be output. Error bit set. Error cannot be reset by command.',
    },
    {
        errorCode: 0x0006,
        name: 'Flash unlock fail',
        description: 'Flash could not be unlocked for writing.',
        category: 'Command execution error',
        behavior: 'Warning bit raised.',
    },
    {
        errorCode: 0x0007,
        name: 'Flash busy',
        description: 'Flash operation could not be executed since flash was already busy.',
        category: 'Command execution error',
        behavior: 'Warning bit raised.',
    },
    {
        errorCode: 0x0008,
        name: 'Flash loop timeout',
        description: 'A flash operation did not finish in its expected time so was aborted.',
        category: 'Command execution error',
        behavior: 'Warning bit raised.',
    },
    {
        errorCode: 0x0009,
        name: 'Reference voltage init fail',
        description: 'The reference voltage buffer could not be initialized. Signal processing did not get started.',
        category: 'Initialization error (µC Peripheral)',
        behavior: 'No signal processing is done. No valid position will be output. Error bit set. Error cannot be reset by command.',
    },
    {
        errorCode: 0x000a,
        name: 'POA nvm read fail',
        description: 'POA startup values could not be read properly.',
        category: 'Initialization error (no valid data or no flash access)',
        behavior: 'Default will be loaded. Warning bit raised.',
    },
    {
        errorCode: 0x000b,
        name: 'POA nvm write fail',
        description: 'POA startup values could not be written.',
        category: 'Command execution error',
        behavior: 'Warning bit raised.',
    },
    {
        errorCode: 0x000c,
        name: 'Signal processing nvm read fail',
        description: 'Signal processing data could not be read properly.',
        category: 'Initialization error (no valid data or no flash access)',
        behavior: 'Default will be loaded.',
    },
    {
        errorCode: 0x000d,
        name: 'Signal processing nvm write fail',
        description: 'Signal processing data could not be written.',
        category: 'Command execution error',
        behavior: 'Warning bit raised.',
    },
    {
        errorCode: 0x000e,
        name: 'Input magnitude too small',
        description: 'Analog signal input magnitude is below the accepted threshold. Process data not valid.',
        category: 'Signal error',
        behavior: 'Error bit raised. PV bit reset.',
    },
    {
        errorCode: 0x000f,
        name: 'Calibration not plausible',
        description: 'Calibration data plausibility check failed.',
        category: 'Signal error',
        behavior: 'Warning bit raised.',
    },
    {
        errorCode: 0x0010,
        name: 'Wiegand pulse missing',
        description: 'A Wiegand pulse was not detected where expected.',
        category: 'Signal error',
        behavior: 'Warning bit raised.',
    },
];
/**
 * Splits a 32-bit encoder error register value into its two logical components.
 *
 * The encoder returns a 32-bit integer where:
 * - The upper 16 bits represent the `errorCode`.
 * - The lower 16 bits contain `debugInfo` specific to that error.
 *
 * This function extracts both components and returns them as separate properties.
 *
 * @param registerValue - The 32-bit value read from the encoder error register.
 * @returns An object containing:
 *   - `errorCode`: The 16-bit error code.
 *   - `debugInfo`: The 16-bit debug information associated with the error.
 */
function splitIntegroIntegratedEncoderErrorValue(registerValue) {
    return {
        errorCode: (registerValue >>> 16) & 0xffff,
        debugInfo: registerValue & 0xffff,
    };
}
exports.splitIntegroIntegratedEncoderErrorValue = splitIntegroIntegratedEncoderErrorValue;
/**
 * Retrieves a structured description for a specific encoder error code.
 *
 * This function looks up the provided `errorCode` in the predefined list of known
 * encoder error codes and returns the corresponding description object. If the
 * error code is not recognized, a default description is returned.
 *
 * @param errorCode - The 16-bit error code extracted from the encoder register.
 * @returns The error description object matching the given code, or a default
 *          description if the code is unknown.
 */
function getIntegroIntegratedEncoderErrorDescription(errorCode) {
    var _a;
    return ((_a = exports.integroIntegratedEncoderErrorCodeDescriptions.find((e) => e.errorCode === errorCode)) !== null && _a !== void 0 ? _a : exports.integroIntegratedEncoderErrorCodeDescriptions[0]);
}
exports.getIntegroIntegratedEncoderErrorDescription = getIntegroIntegratedEncoderErrorDescription;
/**
 * Parses a 32-bit encoder error register value into a structured error object.
 *
 * This utility provides a high-level interpretation of encoder error registers by:
 * 1. Extracting the `errorCode` and `debugInfo` from the raw register value.
 * 2. Resolving a human-readable description for the extracted error code.
 * 3. Returning all relevant information in a structured format.
 *
 * If the extracted `errorCode` is not recognized, the `errorInfo` field will be `undefined`.
 *
 * @param registerValue - The 32-bit integer value read from the encoder error register.
 * @returns An object containing:
 *   - `registerValue`: The original raw register value.
 *   - `debugInfo`: Additional debug information extracted from the register.
 *   - `errorInfo`: Optional detailed description of the error, or `undefined` if unknown.
 */
function parseIntegroIntegratedEncoderError(registerValue) {
    const { errorCode, debugInfo } = splitIntegroIntegratedEncoderErrorValue(registerValue);
    return {
        registerValue,
        debugInfo,
        errorInfo: getIntegroIntegratedEncoderErrorDescription(errorCode),
    };
}
exports.parseIntegroIntegratedEncoderError = parseIntegroIntegratedEncoderError;
//# sourceMappingURL=integro.js.map