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,CAAC,IAAI,KAAK,QAAQ;QACrD,CAAC,CAAC,QAAQ,CAAC,IAAI;QACf,CAAC,CAAC,IAAI,CAAC;IAEX,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,IAAI,GAAG,sBAAsB,CAAC,GAAG,CAAC,CAAC;IACzC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;AACvB,CAAC","sourcesContent":["import type { ErrorMapping } from './hardware-error-mappings';\nimport { Category, ErrorCode, Severity } from './hardware-errors-enums';\n\n/**\n * Full DMK (Device Management Kit) `_tag`-based error mappings.\n *\n * DMK is Ledger's newer SDK. Unlike legacy `@ledgerhq/errors`, which identify\n * errors via the standard `error.name` property, DMK errors carry a\n * non-standard `_tag` string (e.g. `'DeviceSessionNotFound'`,\n * `'DeviceLockedError'`). Tag values are looked up in this mapping to resolve\n * the full {@link ErrorMapping} (code, message, severity, category,\n * userMessage).\n *\n * This is the single source of truth for DMK tag → error details. The\n * code-only {@link DMK_ERROR_TAG_MAPPINGS} is derived from this object so\n * consumers that only need the `ErrorCode` (e.g. MetaMask Mobile) can use the\n * simpler mapping without duplicating data.\n *\n * These mappings are shared with legacy error names in consumers (e.g.\n * MetaMask Mobile) since both map to the same `ErrorCode` values.\n */\nexport const DMK_ERROR_MAPPINGS: Record<string, ErrorMapping> = {\n  DeviceSessionNotFound: {\n    code: ErrorCode.DeviceDisconnected,\n    message: 'DMK device session not found',\n    severity: Severity.Err,\n    category: Category.Connection,\n    userMessage:\n      'Your Ledger device was disconnected. Please reconnect and try again.',\n  },\n  ConnectionOpeningError: {\n    code: ErrorCode.BluetoothConnectionFailed,\n    message: 'DMK connection failed to open',\n    severity: Severity.Err,\n    category: Category.Connection,\n    userMessage:\n      'Failed to connect to your Ledger device. Please make sure it is nearby and try again.',\n  },\n  DeviceDisconnectedWhileSendingError: {\n    code: ErrorCode.DeviceDisconnected,\n    message: 'DMK device disconnected while sending',\n    severity: Severity.Err,\n    category: Category.Connection,\n    userMessage:\n      'Your Ledger device was disconnected. Please reconnect and try again.',\n  },\n  DeviceDisconnectedBeforeSendingApdu: {\n    code: ErrorCode.DeviceDisconnected,\n    message: 'DMK device disconnected before sending',\n    severity: Severity.Err,\n    category: Category.Connection,\n    userMessage:\n      'Your Ledger device was disconnected. Please reconnect and try again.',\n  },\n  DeviceLockedError: {\n    code: ErrorCode.AuthenticationDeviceLocked,\n    message: 'DMK device locked',\n    severity: Severity.Err,\n    category: Category.Authentication,\n    userMessage: 'Please unlock your Ledger device to continue.',\n  },\n  DeviceNotConnectedError: {\n    code: ErrorCode.DeviceDisconnected,\n    message: 'DMK device not connected',\n    severity: Severity.Err,\n    category: Category.Connection,\n    userMessage:\n      'Your Ledger device is not connected. Please connect and try again.',\n  },\n  SessionRefresherError: {\n    code: ErrorCode.DeviceDisconnected,\n    message: 'DMK session refresh failed',\n    severity: Severity.Err,\n    category: Category.Connection,\n    userMessage:\n      'Your Ledger device session has expired. Please reconnect and try again.',\n  },\n};\n\n/**\n * DMK `_tag`-to-`ErrorCode` mappings.\n *\n * Derived from {@link DMK_ERROR_MAPPINGS} so there is a single source of truth\n * for tag → code resolution. Consumers that only need the numeric `ErrorCode`\n * (e.g. MetaMask Mobile) can use this lightweight mapping directly.\n */\nexport const DMK_ERROR_TAG_MAPPINGS: Record<string, ErrorCode> =\n  Object.fromEntries(\n    Object.entries(DMK_ERROR_MAPPINGS).map(([tag, { code }]) => [tag, code]),\n  );\n\n/**\n * DMK-specific message patterns for error parsing.\n *\n * Each entry maps one or more case-insensitive message substrings to an\n * `ErrorCode`. Used as a fallback when neither `error.name` nor `_tag` is\n * recognised, but the error message contains DMK-specific phrasing.\n */\nexport const DMK_MESSAGE_PATTERNS: readonly {\n  patterns: string[];\n  code: ErrorCode;\n}[] = [\n  {\n    patterns: [\n      'session not found',\n      'sessionid is not initialized',\n      'invalid session',\n    ],\n    code: ErrorCode.DeviceDisconnected,\n  },\n  {\n    patterns: ['device action ended without completion'],\n    code: ErrorCode.DeviceUnresponsive,\n  },\n  {\n    patterns: ['ledger command failed'],\n    code: ErrorCode.DeviceNotReady,\n  },\n];\n\n/**\n * Result of resolving a DMK error from its `_tag` property.\n */\nexport type DMKTagResolution = {\n  code: ErrorCode;\n  tag: string;\n};\n\n/**\n * Parse a DMK (Device Management Kit) error by its `_tag` property.\n *\n * DMK errors carry a non-standard `_tag` string. This function extracts the\n * tag and looks it up in {@link DMK_ERROR_TAG_MAPPINGS} to resolve the\n * corresponding `ErrorCode`.\n *\n * @param error - The error object to parse.\n * @returns The resolved `ErrorCode` and the original tag string, or `null`\n * if no `_tag` is present or the tag is not recognised.\n */\nexport function getDmkErrorFromTag(error: unknown): DMKTagResolution | null {\n  if (error === null || typeof error !== 'object') {\n    return null;\n  }\n\n  const errorObj = error as Record<string, unknown>;\n  const tag =\n    '_tag' in errorObj && typeof errorObj._tag === 'string'\n      ? errorObj._tag\n      : null;\n\n  if (!tag) {\n    return null;\n  }\n\n  const code = DMK_ERROR_TAG_MAPPINGS[tag];\n  if (code === undefined) {\n    return null;\n  }\n\n  return { code, tag };\n}\n"]}