"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EthercatState = exports.getExactObjectFlagName = exports.getObjectFlagNames = exports.ObjectFlags = exports.ObjectCodeDefinition = exports.findBaseDataTypeByIndex = exports.allBaseDataTypes = exports.preDefinedRecords = exports.baseDataTypesWithVariableLength = exports.baseDataTypes = void 0;
/**
 * List of base data types as defined in ETG.1020 Protocol Enhancements, Table 98.
 */
exports.baseDataTypes = [
    { index: 0x0001, name: 'BOOLEAN', baseDataType: 'BOOL\nBIT', bitSize: 1, description: "'0': FALSE\n'1': TRUE" },
    { index: 0x001e, name: 'BYTE', baseDataType: 'BYTE', bitSize: 8, description: 'Unsigned Byte, Octet' },
    { index: 0x001f, name: 'WORD', baseDataType: 'WORD', bitSize: 16, description: 'Two Octet' },
    { index: 0x0020, name: 'DWORD', baseDataType: 'DWORD', bitSize: 32, description: 'Four Octet' },
    // Bit String
    { index: 0x0030, name: 'BIT1', baseDataType: 'BIT1', bitSize: 1, description: 'Can be used as UNSIGNED1' },
    { index: 0x0031, name: 'BIT2', baseDataType: 'BIT2', bitSize: 2, description: 'Can be used as UNSIGNED2' },
    { index: 0x0032, name: 'BIT3', baseDataType: 'BIT3', bitSize: 3, description: 'Can be used as UNSIGNED3' },
    { index: 0x0033, name: 'BIT4', baseDataType: 'BIT4', bitSize: 4, description: 'Can be used as UNSIGNED4' },
    { index: 0x0034, name: 'BIT5', baseDataType: 'BIT5', bitSize: 5, description: 'Can be used as UNSIGNED5' },
    { index: 0x0035, name: 'BIT6', baseDataType: 'BIT6', bitSize: 6, description: 'Can be used as UNSIGNED6' },
    { index: 0x0036, name: 'BIT7', baseDataType: 'BIT7', bitSize: 7, description: 'Can be used as UNSIGNED7' },
    { index: 0x0037, name: 'BIT8', baseDataType: 'BIT8', bitSize: 8, description: 'Can be used as UNSIGNED8' },
    { index: 0x0038, name: 'BIT9', baseDataType: 'BIT9', bitSize: 9, description: 'Can be used as UNSIGNED9' },
    { index: 0x0039, name: 'BIT10', baseDataType: 'BIT10', bitSize: 10, description: 'Can be used as UNSIGNED10' },
    { index: 0x003a, name: 'BIT11', baseDataType: 'BIT11', bitSize: 11, description: 'Can be used as UNSIGNED11' },
    { index: 0x003b, name: 'BIT12', baseDataType: 'BIT12', bitSize: 12, description: 'Can be used as UNSIGNED12' },
    { index: 0x003c, name: 'BIT13', baseDataType: 'BIT13', bitSize: 13, description: 'Can be used as UNSIGNED13' },
    { index: 0x003d, name: 'BIT14', baseDataType: 'BIT14', bitSize: 14, description: 'Can be used as UNSIGNED14' },
    { index: 0x003e, name: 'BIT15', baseDataType: 'BIT15', bitSize: 15, description: 'Can be used as UNSIGNED15' },
    { index: 0x003f, name: 'BIT16', baseDataType: 'BIT16', bitSize: 16, description: 'Can be used as UNSIGNED16' },
    { index: 0x002d, name: 'BITARR8', baseDataType: 'BITARR8', bitSize: 8, description: '8 individual Bits' },
    { index: 0x002e, name: 'BITARR16', baseDataType: 'BITARR16', bitSize: 16, description: '16 individual Bits' },
    { index: 0x002f, name: 'BITARR32', baseDataType: 'BITARR32', bitSize: 32, description: '32 individual Bits' },
    // Signed Integer
    {
        index: 0x0002,
        name: 'INTEGER8',
        baseDataType: 'SINT',
        bitSize: 8,
        description: 'Short Integer',
        range: [-128, 127],
    },
    {
        index: 0x0003,
        name: 'INTEGER16',
        baseDataType: 'INT',
        bitSize: 16,
        description: 'Integer',
        range: [-32768, 32767],
    },
    {
        index: 0x0010,
        name: 'INTEGER24',
        baseDataType: 'INT24',
        bitSize: 24,
        description: '',
        range: [-8388608, 8388607],
    },
    { index: 0x0010, name: 'INTEGER24', baseDataType: 'INT24', bitSize: 24, range: [-8388608, 8388607] },
    {
        index: 0x0004,
        name: 'INTEGER32',
        baseDataType: 'DINT',
        bitSize: 32,
        description: 'Double Integer',
        range: [-2147483648, 2147483647],
    },
    { index: 0x0012, name: 'INTEGER40', baseDataType: 'INT40', bitSize: 40 },
    { index: 0x0013, name: 'INTEGER48', baseDataType: 'INT48', bitSize: 48 },
    { index: 0x0014, name: 'INTEGER56', baseDataType: 'INT56', bitSize: 56 },
    { index: 0x0015, name: 'INTEGER64', baseDataType: 'LINT', bitSize: 64, description: 'Long Integer' },
    // Unsigned Integer
    {
        index: 0x0005,
        name: 'UNSIGNED8',
        baseDataType: 'USINT',
        bitSize: 8,
        description: 'Unsigned Short Integer',
        range: [0, 255],
    },
    {
        index: 0x0006,
        name: 'UNSIGNED16',
        baseDataType: 'UINT',
        bitSize: 16,
        description: 'Unsigned Integer / Word',
        range: [0, 65535],
    },
    {
        index: 0x0016,
        name: 'UNSIGNED24',
        baseDataType: 'UIN24',
        bitSize: 24,
        description: '',
        range: [0, 16777215],
    },
    { index: 0x0016, name: 'UNSIGNED24', baseDataType: 'UINT24', bitSize: 24 },
    {
        index: 0x0007,
        name: 'UNSIGNED32',
        baseDataType: 'UDINT',
        bitSize: 32,
        description: 'Unsigned Double Integer',
        range: [0, 4294967295],
    },
    { index: 0x0018, name: 'UNSIGNED40', baseDataType: 'UINT40', bitSize: 40 },
    { index: 0x0019, name: 'UNSIGNED48', baseDataType: 'UINT48', bitSize: 48 },
    { index: 0x001a, name: 'UNSIGNED56', baseDataType: 'UINT56', bitSize: 56 },
    { index: 0x001b, name: 'UNSIGNED64', baseDataType: 'ULINT', bitSize: 64, description: 'Unsigned Long Integer' },
    // Floating
    { index: 0x0008, name: 'REAL32', baseDataType: 'REAL', bitSize: 32, description: 'Floating point' },
    { index: 0x0011, name: 'REAL64', baseDataType: 'LREAL', bitSize: 64, description: 'Long float' },
    // GUID
    {
        index: 0x001d,
        name: 'GUID',
        baseDataType: 'GUID',
        bitSize: 128,
        description: 'Globally unique identifier with a length of 128 Bit',
    },
];
/**
 * List of base data types with variable length, as specified in ETG.1020 Protocol Enhancements, Table 99.
 */
exports.baseDataTypesWithVariableLength = [
    // Strings
    {
        index: 0x0009,
        name: 'VISIBLE_STRING',
        baseDataType: 'STRING(n)',
        bitSize: 0,
        description: 'VisibleString\n(1 octet per character)',
    },
    // Octet field
    {
        index: 0x000a,
        name: 'OCTET_STRING',
        baseDataType: 'ARRAY[0..n] OF BYTE',
        bitSize: 0,
        description: 'Sequence of octets (data type BYTE)',
    },
    {
        index: 0x000b,
        name: 'UNICODE_STRING',
        baseDataType: 'ARRAY[0..n] OF UINT',
        bitSize: 0,
        description: 'Sequence of UINT',
    },
    {
        index: 0x0260,
        name: 'ARRAY_OF_INT',
        baseDataType: 'ARRAY[0..n] OF INT',
        bitSize: 0,
        description: 'Sequence of INT',
    },
    {
        index: 0x0261,
        name: 'ARRAY_OF_SINT',
        baseDataType: 'ARRAY[0..n] OF SINT',
        bitSize: 0,
        description: 'Sequence of SINT',
    },
    {
        index: 0x0262,
        name: 'ARRAY_OF_DINT',
        baseDataType: 'ARRAY[0..n] OF DINT',
        bitSize: 0,
        description: 'Sequence of DINT',
    },
    {
        index: 0x0263,
        name: 'ARRAY_OF_UDINT',
        baseDataType: 'ARRAY[0..n] OF UDINT',
        bitSize: 0,
        description: 'Sequence of UDINT',
    },
];
exports.preDefinedRecords = [
    {
        index: 0x0021,
        name: 'PDO_MAPPING',
        baseDataType: '',
        bitSize: 0,
        description: 'For PDO_MAPPING definition see ETG.1000',
    },
    {
        index: 0x0023,
        name: 'IDENTITY',
        baseDataType: '',
        bitSize: 0,
        description: 'For IDENTITY definition see ETG.1000',
    },
    {
        index: 0x0025,
        name: 'COMMAND_PAR',
        baseDataType: '',
        bitSize: 0,
        description: 'For COMMAND_PAR definition see ETG.1000',
    },
    {
        index: 0x0027,
        name: 'PDO_PARAMETER',
        baseDataType: '',
        bitSize: 0,
        description: 'For PDO_PARAMETER definition see ETG.1020',
    },
    {
        index: 0x0028,
        name: 'ENUM',
        baseDataType: '',
        bitSize: 0,
        description: 'For DEFTYPE_ENUM definition see ETG.1020',
    },
    {
        index: 0x0029,
        name: 'SM_SYNCHRONIZATION',
        baseDataType: '',
        bitSize: 0,
        description: 'For SYNC_PAR definition see ETG.1000',
    },
    {
        index: 0x002a,
        name: 'RECORD',
        baseDataType: '',
        bitSize: 0,
        description: 'No pre-defined Record structure. Can be used as a general data type for Records.',
    },
    {
        index: 0x002b,
        name: 'BACKUP_PARAMETER',
        baseDataType: '',
        bitSize: 0,
        description: 'For BACKUP_PARAMETE definition see ETG.1020, Object 0x10F0',
    },
    {
        index: 0x002c,
        name: 'MODULAR_DEVICE_PROFILE',
        baseDataType: '',
        bitSize: 0,
        description: 'For MODULAR_DEVICE_PROFILE definition see ETG.5001, Object 0xF000',
    },
    {
        index: 0x0281,
        name: 'ERROR_SETTING',
        baseDataType: '',
        bitSize: 0,
        description: 'For DEFTYPE_ERRORHANDLING definition see ETG.1020, Object 0x10F1',
    },
    {
        index: 0x0282,
        name: 'DIAGNOSIS_HISTORY',
        baseDataType: '',
        bitSize: 0,
        description: 'For DEFTYPE_DIAGHISTORY definition see ETG.1020, Object 0x10F3',
    },
    {
        index: 0x0283,
        name: 'EXTERNAL_SYNC_STATUS',
        baseDataType: '',
        bitSize: 0,
        description: 'For DEFTYPE_SYNCSTATUS definition see ETG.1020, Object 0x10F4',
    },
    {
        index: 0x0284,
        name: 'EXTERNAL_SYNC_SETTINGS',
        baseDataType: '',
        bitSize: 0,
        description: 'For DEFTYPE_SYNCSETTINGS definition see ETG.1020, Object 0x10F5',
    },
    {
        index: 0x0285,
        name: 'DEFTYPE_FSOEFRAME',
        baseDataType: '',
        bitSize: 0,
        description: 'For DEFTYPE_FSOEFRAME definition see ETG.5120',
    },
    {
        index: 0x0286,
        name: 'DEFTYPE_FSOECOMMPAR',
        baseDataType: '',
        bitSize: 0,
        description: 'For DEFTYPE_FSOECOMMPA definition see ETG.5120',
    },
];
/**
 * Combines all base data types, including those from Table 98 and Table 99.
 */
exports.allBaseDataTypes = [
    ...exports.baseDataTypes,
    ...exports.baseDataTypesWithVariableLength,
    ...exports.preDefinedRecords,
];
/**
 * Finds a base data type that matches the specified index.
 * @param index - The index to search for.
 * @returns The matching base data type, or undefined if not found.
 */
function findBaseDataTypeByIndex(index) {
    return exports.allBaseDataTypes.find((baseDataType) => baseDataType.index === index);
}
exports.findBaseDataTypeByIndex = findBaseDataTypeByIndex;
/**
 * Enum for object code definitions, based on ETG.1000.6 EtherCAT Specification - Part 6, Table 62.
 */
var ObjectCodeDefinition;
(function (ObjectCodeDefinition) {
    ObjectCodeDefinition[ObjectCodeDefinition["DOMAIN"] = 2] = "DOMAIN";
    ObjectCodeDefinition[ObjectCodeDefinition["DEFTYPE"] = 5] = "DEFTYPE";
    ObjectCodeDefinition[ObjectCodeDefinition["DEFSTRUCT"] = 6] = "DEFSTRUCT";
    ObjectCodeDefinition[ObjectCodeDefinition["VAR"] = 7] = "VAR";
    ObjectCodeDefinition[ObjectCodeDefinition["ARRAY"] = 8] = "ARRAY";
    ObjectCodeDefinition[ObjectCodeDefinition["RECORD"] = 9] = "RECORD";
})(ObjectCodeDefinition = exports.ObjectCodeDefinition || (exports.ObjectCodeDefinition = {}));
/**
 * Flags used to describe object access rights, mapping, and startup behavior.
 * These can be combined using bitwise OR to form compound flags.
 */
/**
 * Flags used to describe object access rights, mapping, and startup behavior.
 * These can be combined using bitwise OR to form compound flags.
 */
var ObjectFlags;
(function (ObjectFlags) {
    /**
     * No flags set.
     */
    ObjectFlags[ObjectFlags["NONE"] = 0] = "NONE";
    // ───── Read Access Flags ─────
    /**
     * Pre-operational read access.
     */
    ObjectFlags[ObjectFlags["PO_RD"] = 1] = "PO_RD";
    /**
     * Safe-operational read access.
     */
    ObjectFlags[ObjectFlags["SO_RD"] = 2] = "SO_RD";
    /**
     * Operational read access.
     */
    ObjectFlags[ObjectFlags["OP_RD"] = 4] = "OP_RD";
    /**
     * All read access (pre-op, safe-op, operational).
     */
    ObjectFlags[ObjectFlags["ALL_RD"] = 7] = "ALL_RD";
    // ───── Write Access Flags ─────
    /**
     * Pre-operational write access.
     */
    ObjectFlags[ObjectFlags["PO_WR"] = 8] = "PO_WR";
    /**
     * Safe-operational write access.
     */
    ObjectFlags[ObjectFlags["SO_WR"] = 16] = "SO_WR";
    /**
     * Operational write access.
     */
    ObjectFlags[ObjectFlags["OP_WR"] = 32] = "OP_WR";
    /**
     * All write access (pre-op, safe-op, operational).
     */
    ObjectFlags[ObjectFlags["ALL_WR"] = 56] = "ALL_WR";
    // ───── Read/Write Combinations ─────
    /**
     * Pre-operational read/write access.
     */
    ObjectFlags[ObjectFlags["PO_RDWR"] = 9] = "PO_RDWR";
    /**
     * Safe-operational read/write access.
     */
    ObjectFlags[ObjectFlags["SO_RDWR"] = 18] = "SO_RDWR";
    /**
     * Operational read/write access.
     */
    ObjectFlags[ObjectFlags["OP_RDWR"] = 36] = "OP_RDWR";
    /**
     * All read/write access (pre-op, safe-op, operational).
     */
    ObjectFlags[ObjectFlags["ALL_RDWR"] = 63] = "ALL_RDWR";
    // ───── PDO Mapping Flags ─────
    /**
     * Mapped to Receive PDO.
     */
    ObjectFlags[ObjectFlags["RXPDO_MAP"] = 64] = "RXPDO_MAP";
    /**
     * Mapped to Transmit PDO.
     */
    ObjectFlags[ObjectFlags["TXPDO_MAP"] = 128] = "TXPDO_MAP";
    /**
     * Mapped to both Receive and Transmit PDO.
     */
    ObjectFlags[ObjectFlags["RXTXPDO_MAP"] = 192] = "RXTXPDO_MAP";
    // ───── Other Flags ─────
    /**
     * Include in device backup.
     */
    ObjectFlags[ObjectFlags["BACKUP"] = 256] = "BACKUP";
    /**
     * Set on device startup.
     */
    ObjectFlags[ObjectFlags["STARTUP"] = 512] = "STARTUP";
    /**
     * All list flags (mapping, backup, and startup).
     */
    ObjectFlags[ObjectFlags["ALL_LIST_FLAGS"] = 960] = "ALL_LIST_FLAGS";
})(ObjectFlags = exports.ObjectFlags || (exports.ObjectFlags = {}));
/**
 * Returns the names of individual base flags that are set in a given bitmask.
 *
 * This function filters out composite flags (e.g., `ALL_RD`, `PO_RDWR`)
 * and returns only the names of "base" flags — flags that represent a single bit.
 *
 * @param flagsValue - The bitmask containing one or more `ObjectFlags`.
 * @returns An array of `ObjectFlags` key names corresponding to individual set flags.
 *
 * @example
 * ```ts
 * const flags = ObjectFlags.PO_RD | ObjectFlags.SO_WR | ObjectFlags.TXPDO_MAP;
 * const names = getObjectFlagNames(flags);
 * // names => ["PO_RD", "SO_WR", "TXPDO_MAP"]
 * ```
 */
function getObjectFlagNames(flagsValue) {
    const result = [];
    for (const [key, value] of Object.entries(ObjectFlags)) {
        // Ensure value is a number (some keys come from reverse-mapping enums)
        if (typeof value === 'number') {
            // Only include base flags (power of two or 0)
            const isBaseFlag = value !== 0 && (value & (value - 1)) === 0;
            if (isBaseFlag && (flagsValue & value) !== 0) {
                result.push(key);
            }
        }
    }
    return result;
}
exports.getObjectFlagNames = getObjectFlagNames;
/**
 * Returns the name of any flag (base or compound) that exactly matches the given value.
 *
 * This includes both single-bit flags (e.g., `PO_RD`) and combined flags (e.g., `ALL_RDWR`, `RXTXPDO_MAP`).
 *
 * @param flagsValue - The exact value to match.
 * @returns The matching flag name, or `undefined` if no exact match exists.
 *
 * @example
 * ```ts
 * getExactFlagName(ObjectFlags.PO_RD);       // "PO_RD"
 * getExactFlagName(ObjectFlags.ALL_RDWR);    // "ALL_RDWR"
 * getExactFlagName(ObjectFlags.PO_RD | ObjectFlags.SO_WR); // undefined
 * ```
 */
function getExactObjectFlagName(flagsValue) {
    for (const [key, value] of Object.entries(ObjectFlags)) {
        if (typeof value === 'number' && value === flagsValue) {
            return key;
        }
    }
    return undefined;
}
exports.getExactObjectFlagName = getExactObjectFlagName;
/**
 * Represents the EtherCAT State Machine (ESM) states.
 * These states define the different phases in the EtherCAT communication process,
 * determining the level of access and functionality available at each stage.
 */
var EthercatState;
(function (EthercatState) {
    /** No specific state assigned; the device is inactive or uninitialized. */
    EthercatState[EthercatState["NONE"] = 0] = "NONE";
    /** Initialization state. Only EtherCAT Slave Controller (ESC) registers are accessible. */
    EthercatState[EthercatState["INIT"] = 1] = "INIT";
    /** Pre-operational state. Mailbox communication is available, but cyclic process data exchange is disabled. */
    EthercatState[EthercatState["PREOP"] = 2] = "PREOP";
    /** Boot state, primarily used for firmware updates or special configurations. */
    EthercatState[EthercatState["BOOT"] = 3] = "BOOT";
    /** Safe-operational state. Inputs are updated cyclically, but outputs are held in a safe (inactive) state. */
    EthercatState[EthercatState["SAFEOP"] = 4] = "SAFEOP";
    /** Operational state. Full duplex process data exchange is active; device is fully functional. */
    EthercatState[EthercatState["OP"] = 8] = "OP";
})(EthercatState = exports.EthercatState || (exports.EthercatState = {}));
//# sourceMappingURL=ethercat.js.map