"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parsePdoMappingEntry = exports.isPdoMappingEntry = void 0;
// Matches a hex string like "0x60400010" or "60400010"
// Captures: Group 1 (Index), Group 2 (Subindex), Group 3 (Bit Length)
const pdoMappingEntryRegExp = /^(?:0x)?([0-9a-fA-F]{4})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/;
/**
 * Checks if a string is a valid representation of a CANopen PDO mapping entry.
 *
 * A valid entry is a 32-bit hexadecimal value representing:
 * - **Index** (16 bits)
 * - **Subindex** (8 bits)
 * - **Bit Length** (8 bits)
 *
 * It accepts formats with or without the '0x' prefix (e.g., "0x60400010" or "60400010").
 *
 * @param value The string to validate.
 * @returns `true` if the string follows the PDO mapping entry hex format, `false` otherwise.
 *
 * @example
 * isPdoMappingEntry('0x60400010'); // true
 * isPdoMappingEntry('60400010');   // true
 * isPdoMappingEntry('invalid');    // false
 */
function isPdoMappingEntry(value) {
    return pdoMappingEntryRegExp.test(value);
}
exports.isPdoMappingEntry = isPdoMappingEntry;
/**
 * Parses a CANopen PDO Mapping Entry (32-bit value) into its components.
 *
 * Structure: Index (16-bit) | Subindex (8-bit) | Bit Length (8-bit)
 *
 * @param entry The mapping entry as a raw number or hex string (e.g., 0x60400010).
 * @returns An object containing the parsed index, subindex, and bit length.
 * @throws Error if the input string is not a valid hex number.
 */
function parsePdoMappingEntry(entry) {
    let value;
    if (typeof entry === 'string') {
        // Determine base: if it starts with 0x, parse as 16, otherwise assume it might be 16 or 10.
        // For PDO mapping strings, we safely assume Hex if it looks like standard "60400010".
        // Using parseInt with radix 16 handles "0x" prefix automatically.
        value = parseInt(entry, 16);
        if (isNaN(value)) {
            throw new Error(`Invalid PDO mapping entry string: "${entry}"`);
        }
    }
    else {
        value = entry;
    }
    return {
        // Use zero-fill right shift (>>>) to ensure we handle unsigned 32-bit correctly
        index: (value >>> 16) & 0xffff,
        subindex: (value >>> 8) & 0xff,
        bitLength: value & 0xff,
    };
}
exports.parsePdoMappingEntry = parsePdoMappingEntry;
//# sourceMappingURL=pdo.js.map