// HMS Error Modal - Comprehensive error code database
// Source: https://github.com/greghesp/ha-bambulab
import { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { X, AlertTriangle, AlertCircle, Info, ExternalLink, Loader2, Trash2 } from 'lucide-react';
import type { HMSError, Permission } from '../api/client';
import { api } from '../api/client';
import { useToast } from '../contexts/ToastContext';

interface HMSErrorModalProps {
  printerName: string;
  errors: HMSError[];
  onClose: () => void;
  printerId: number;
  hasPermission: (permission: Permission) => boolean;
  // Runout guidance for a PAUSED print (#2587). When set, AMS-runout errors are
  // re-described to name the physical slot the firmware now expects, instead of
  // the generic "insert into the same slot" text (wrong under AMS Filament Backup).
  // Slot labels are pre-formatted (e.g. "AMS-A · Slot 3"); null when the slot
  // could not be resolved → an honest "check the printer" message is shown.
  runoutGuidance?: {
    expectedSlotLabel: string | null;
    ranOutSlotLabel: string | null;
  } | null;
}

// AMS per-slot filament-runout short codes (module 0x07). These pause the print
// waiting for a specific slot — the ones #2587 re-describes. Printer-side /
// external runout (0300_8004) has no AMS slot and is deliberately excluded.
const AMS_RUNOUT_SHORT_CODES = new Set([
  '0700_8011', '0701_8011', '0702_8011', '0703_8011', '0704_8011',
  '0705_8011', '0706_8011', '0707_8011', '07FF_8011',
]);

// "MQTT command verification failed" — the firmware's authorization check
// refusing a control command. Keyed by its full 16-char code on purpose: this
// error's meaning lives in attr's low half (0500) and code's high half (0001),
// both of which getShortCode() discards, so the short form is a useless
// "0500_0007". Before #2732 that meant filterKnownHMSErrors dropped it and the
// user was never shown the one message that explained why nothing printed.
export const HMS_MQTT_VERIFY_FAILED = '0500050000010007';

// Comprehensive error code database, keyed by short code (XXXX_YYYY) or, where
// the short code cannot express the error, by full code (16 hex chars).
// Short-code entries auto-generated from ha-bambulab - 853 codes
const ERROR_DESCRIPTIONS: Record<string, string> = {
  [HMS_MQTT_VERIFY_FAILED]:
    'The printer rejected a command because it could not verify it. Prints, temperature changes and filament loads sent from Bambuddy will be ignored until this is fixed.',
  '0300_4000': 'Z axis homing failed; the task has been stopped.',
  '0300_4001': 'The printer timed out waiting for the nozzle to cool down before homing.',
  '0300_4002': 'Auto Bed Leveling failed; the task has been stopped.',
  '0300_4005': 'The hotend cooling fan speed is abnormal.',
  '0300_4006': 'The nozzle is clogged.',
  '0300_4008': 'The AMS failed to change filament.',
  '0300_4009': 'Homing XY axis failed.',
  '0300_400A': 'Mechanical resonance frequency identification failed.',
  '0300_400B': 'Internal communication exception',
  '0300_400C': 'The task was canceled.',
  '0300_400D': 'Resume failed after power loss.',
  '0300_400E': 'The motor self-check failed.',
  '0300_400F': 'The power supply voltage does not match the printer.',
  '0300_4010': 'Nozzle offset calibration failed.',
  '0300_4011': 'Flow Dynamics Calibration failed; please reinitiate printing or calibration.',
  '0300_4013': 'Printing cannot be initiated while AMS is drying.',
  '0300_4014': 'Homing Z axis failed: temperature control abnormality.',
  '0300_4015': 'Nozzle clumping detection calibration failed. Please go to \'Assistant\' for troubleshooting.',
  '0300_4016': 'Nozzle cleaning failed. Please click the Assistant for troubleshooting.',
  '0300_401F': 'The hotend is not installed, and the toolhead cannot perform homing. Please install the hotend and then continue.',
  '0300_4020': 'The nozzle presence detection failed. Please check the Assistant for details.',
  '0300_4021': 'Nozzle offset calibration sensor signal abnormality detected. Please check the sensor and retry.',
  '0300_4042': 'The Laser Safety Window is not properly installed. The task has been stopped.',
  '0300_4044': 'The Flame Sensor is abnormal. The sensor may be short-circuited. Please troubleshoot the issue before starting a print job.',
  '0300_404B': 'Task aborted because the front door or top cover is open.',
  '0300_404D': 'The current temperature of the hotend, heatbed, or chamber is too high. Please wait for it to cool down to room temperature before restarting the task.',
  '0300_4050': 'Liveview Camera calibration timeout; please restart the printer.',
  '0300_4052': 'Blade Z-axis homing failed',
  '0300_4057': 'Z-axis step loss detected. The task has stopped. Please check if there are any obstructions beneath the heatbed.',
  '0300_4066': 'Calibration of motion precision failed.',
  '0300_4067': 'Calibration result is over the threshold.',
  '0300_4068': 'Step loss occurred during the motion accuracy enhancement process. Please try again.',
  '0300_8000': 'Printing was paused for unknown reason. You can select \'Resume\' to resume the print job.',
  '0300_8001': 'Printing was paused by the user. You can select \'Resume\' to continue printing.',
  '0300_8002': 'First layer defects were detected by the Micro Lidar. Please check the quality of the printed model before continuing your print.',
  '0300_8003': 'Spaghetti defects were detected by the AI Print Monitoring. Please check the quality of the printed model before continuing your print.',
  '0300_8004': 'Filament ran out. Please load new filament.',
  '0300_8005': 'Toolhead front cover fell off. Please remount the front cover and check to make sure your print is going okay.',
  '0300_8006': 'The build plate marker was not detected. Please confirm the build plate is correctly positioned on the heatbed with all four corners aligned, and the marker is visible.',
  '0300_8007': 'There was an unfinished print job when the printer lost power. If the model is still adhered to the build plate, you can try resuming the print job.',
  '0300_8008': 'Nozzle temperature malfunction',
  '0300_8009': 'Heatbed temperature malfunction',
  '0300_800A': 'A Filament pile-up was detected by AI Print Monitoring. Please clean filament from the waste chute.',
  '0300_800B': 'The cutter is stuck. Please make sure the cutter handle is out and check the filament sensor cable connection.',
  '0300_800C': 'Skipped step detected: auto-recover complete; please resume print and check if there are any layer shift problems.',
  '0300_800D': 'Detected that the extruder is not extruding normally. If the defects are acceptable, select \'Resume\' to resume the print job.',
  '0300_800E': 'The print file is not available. Please check to see if the storage media has been removed.',
  '0300_800F': 'The door seems to be open, so printing was paused.',
  '0300_8010': 'The hotend cooling fan speed is abnormal.',
  '0300_8011': 'Detected build plate is not the same as the Gcode file. Please adjust slicer settings or use the correct plate.',
  '0300_8013': 'Printing paused due to the pause command added to the printing file.',
  '0300_8014': 'The nozzle is covered with filament, or the build plate is installed incorrectly. Please cancel this print and clean the nozzle or adjust the build plate according to the actual status. You can als...',
  '0300_8015': 'The filament on external spool has run out; please load new filament. If the filament is loaded, please select \'Resume\'.',
  '0300_8016': 'The nozzle is clogged with filament. Please cancel this print and clean the nozzle or select \'Resume\' to resume the print job.',
  '0300_8017': 'Foreign objects detected on heatbed. Please check and clean the heatbed. Then, select \'Resume\' to resume the print job.',
  '0300_8018': 'Chamber temperature malfunction.',
  '0300_8019': 'No build plate is placed.',
  '0300_801A': 'Filament extrusion error; please check the assistant for troubleshooting. After resolving the issue, decide whether to cancel or resume the print job based on the actual print status.',
  '0300_801B': 'Nozzle temperature problem detected. Refer to Assistant to re-connect the hotend connector. POWER OFF the printer before this operation to avoid short circuits.',
  '0300_801C': 'The extrusion resistance is abnormal. The extruder may be clogged; please refer to the assistant. After trouble shooting, you can select \'Resume\' to resume the print job.',
  '0300_801D': 'The extruder servo motor position sensor is malfunctioning. Please power off the printer first and check if the connection cable is loose.',
  '0300_801E': 'The extrusion motor is overloaded, please check the Assistant for details.',
  '0300_8021': 'The nozzle may not be installed or not properly installed. Please ensure the nozzle is correctly installed before proceeding.',
  '0300_8022': 'The heatbed may be obstructed while moving downward. Please clear any objects beneath the heatbed and check for any resistance or jamming during its movement.',
  '0300_8028': 'Nozzle offset calibration sensor error. If using a single hotend or the calibration function is disabled, you may ignore this and continue printing; otherwise, it is recommended to check the sensor...',
  '0300_8041': 'Platform detection timeout: please restart the printer.',
  '0300_8042': 'Task paused because the door is open.',
  '0300_8043': 'The laser module is abnormal.',
  '0300_8044': 'Fire was detected inside the chamber.',
  '0300_8045': 'Material detection timeout: please restart the printer.',
  '0300_8046': 'Foreign object detect timeout: please restart the printer.',
  '0300_8047': 'Quick-release lever detection time out: please restart the printer.',
  '0300_8048': 'Laser Module unlock has timed out, and the task cannot proceed. Please restart the printer and try again.',
  '0300_8049': 'The current plate is invalid.',
  '0300_804A': 'Emergency stop button improperly installed. Please reinstall according to the Wiki before proceeding.',
  '0300_804B': 'Task paused. The Laser Safety Window is open.',
  '0300_804E': 'This is a printing task. Please detach the Laser/Cutting Module from the Toolhead.',
  '0300_804F': 'The loading/unloading process is currently ongoing. Please stop the process or remove the laser/cutting module.',
  '0300_8050': 'This device does not support the 40W Laser Module. Please remove it or replace it with a 10W Laser Module.',
  '0300_8051': 'The cutting module has dropped or the cutting module cable is disconnected; please check the module.',
  '0300_8053': 'Laser module detected. Please install the right nozzle correctly to ensure proper Laser Module Mounting Calibration.',
  '0300_8054': 'Please place the paper required for Print Then Cut.',
  '0300_8055': 'The module mounted on the toolhead does not match the task. Please install the correct module.',
  '0300_8057': 'The rotary attachment is disconnected. Please ensure it is properly installed and the cable is securely plugged in.',
  '0300_8058': 'The rotary attachment is detected. Please remove it before continuing.',
  '0300_8061': 'The mode of Airflow System failed to activate; check the air door condition.',
  '0300_8062': 'The chamber temperature is too high. It may be due to high environmental temperature.',
  '0300_8063': 'The chamber temperature is too high. Please open the top cover and front door to cool down.',
  '0300_8064': 'The chamber temperature is too high. Please open the top cover and front door to cool down. (Open door detection for this print job will be set to \'Notification\' level)',
  '0300_8065': 'The temperature of the MC module is too high. Please check the Wiki for possible explanations.',
  '0300_8071': 'The Toolhead Enhanced Cooling Fan module is malfunctioning.',
  '0300_807D': 'Fire Extinguisher not detected, the automatic extinguishing function will be unavailable.',
  '0300_807E': 'Fire Extinguisher not detected, the automatic extinguishing function will be unavailable.',
  '0300_807F': 'Fire Extinguisher is malfunctioning.',
  '0300_8080': 'Fire extinguisher motor reset failed.',
  '0300_8081': 'Fire extinguisher cylinder not installed. Please confirm on the extinguisher page.',
  '0300_8082': 'The Fire Extinguisher Gas Cylinder is empty.',
  '0300_C012': 'Please heat the nozzle to above 170°C.',
  '0300_C056': 'A minor fire was detected inside the chamber, and the Auto Fire Extinguishing process has been aborted.',
  '0300_C070': 'The fire extinguisher has been detected and is ready for use after the laser module is connected.',
  '0500_4001': 'Failed to connect to Bambu Cloud. Please check your network connection.',
  '0500_4002': 'Unsupported print file path or name. Please resend the print job.',
  '0500_4003': 'Printing stopped because the printer was unable to parse the file. Please resend your print job.',
  '0500_4004': 'Device is busy and cannot start new task. Please wait for current task to complete before sending new task.',
  '0500_4005': 'Print jobs are not allowed to be sent while updating firmware.',
  '0500_4006': 'There is not enough free storage space for the print job. Restoring to factory settings can free up available space.',
  '0500_4007': 'The device requires a repair upgrade, and printing is currently unavailable.',
  '0500_4008': 'Starting printing failed; please power cycle the printer and resend the print job.',
  '0500_4009': 'Print jobs are not allowed to be sent while updating logs.',
  '0500_400A': 'The file name is not supported. Please rename and restart the print job.',
  '0500_400B': 'There was a problem downloading a file. Please check your network connection and resend the print job.',
  '0500_400C': 'Please insert a MicroSD card and restart the print job.',
  '0500_400D': 'Please run a self-test and restart the print job.',
  '0500_400E': 'Printing was cancelled.',
  '0500_400F': 'AMS is initializing and cannot be upgraded at the moment. Please try again later.',
  '0500_4010': 'AMS is drying and cannot be upgraded at the moment. Please try again later.',
  '0500_4011': 'The printer is loading or unloading filament and cannot be upgraded at the moment. Please try again later.',
  '0500_4012': 'The device is printing and cannot be upgraded at the moment. Please try again later.',
  '0500_4013': 'AMS is in operation and cannot be upgraded at the moment. Please try again when it is idle.',
  '0500_4014': 'Slicing for the print job failed. Please check your settings and restart the print job.',
  '0500_4015': 'There is not enough free storage space for the print job. Please format or clear files from the MicroSD card to free up space.',
  '0500_4016': 'The MicroSD Card is write-protected. Please replace the MicroSD Card.',
  '0500_4017': 'Binding failed. Please retry or restart the printer and retry.',
  '0500_4018': 'Binding configuration information parsing failed; please try again.',
  '0500_4019': 'The printer has already been bound. Please unbind it and try again.',
  '0500_401A': 'Cloud access failed. Possible reasons include network instability caused by interference, inability to access the internet, or router firewall configuration restrictions. You can try moving the pri...',
  '0500_401B': 'Cloud response is invalid. If you have tried multiple times and are still failing, please contact customer support.',
  '0500_401C': 'Cloud access is rejected. If you have tried multiple times and are still failing, please contact customer support.',
  '0500_401D': 'Cloud access failed, which may be caused by network instability due to interference. You can try moving the printer closer to the router before you try again.',
  '0500_401E': 'Cloud response is invalid. If you have tried multiple times and are still failing, please contact customer support.',
  '0500_401F': 'Authorization timed out. Please make sure that your phone or PC has access to the internet, and ensure that the Bambu Studio/Bambu Handy APP is running in the foreground during the binding operation.',
  '0500_4020': 'Cloud access rejected. If you have tried multiple times and are still failing, please contact customer support.',
  '0500_4021': 'Cloud access failed, which may be caused by network instability due to interference. You can try moving the printer closer to the router before you try again.',
  '0500_4022': 'Cloud response is invalid. If you have tried multiple times and are still failing, please contact customer support.',
  '0500_4023': 'Cloud access rejected. If you have tried multiple times and are still failing, please contact customer support.',
  '0500_4024': 'Cloud access failed. Possible reasons include network instability caused by interference, inability to access the internet, or router firewall configuration restrictions. You can try moving the pri...',
  '0500_4025': 'Cloud response is invalid. If you have tried multiple times and are still failing, please contact customer support.',
  '0500_4026': 'Cloud access rejected. If you have tried multiple times and are still failing, please contact customer support.',
  '0500_4027': 'Cloud access failed; this may be caused by network instability due to interference. You can try moving the printer closer to the router before you try again.',
  '0500_4028': 'Cloud response is invalid. If you have tried multiple times and are still failing, please contact customer support.',
  '0500_4029': 'Cloud access is rejected. If you have tried multiple times and are still failing, please contact customer support.',
  '0500_402A': 'Failed to connect to the router, which may be caused by wireless interference or being too far away from the router. Please try again or move the printer closer to the router and try again.',
  '0500_402B': 'Router connection failed due to incorrect password. Please check the password and try again.',
  '0500_402C': 'Failed to obtain IP address, which may be caused by wireless interference resulting in data transmission failure or the DHCP address pool of the router being full. Please move the printer closer to...',
  '0500_402D': 'System exception',
  '0500_402E': 'System does not support the file system currently used by the USB flash drive. Please replace or format the USB flash drive to FAT32.',
  '0500_402F': 'The MicroSD card sector data is damaged. Please use the SD card repair tool to repair or format it. If it still cannot be identified, please replace the MicroSD card.',
  '0500_4030': 'The device is currently upgrading. Please try again when it is idle.',
  '0500_4031': 'The accessory firmware does not match the printer. Please update it on the \'Firmware\' page.',
  '0500_4033': 'The AMS firmware does not match the printer. Please update it on the \'Firmware\' page.',
  '0500_4034': 'The Laser Module firmware does not match the printer. Please update it on the \'Firmware\' page.',
  '0500_4035': 'The BirdsEye Camera is malfunctioning. Please try restarting the device. If the issue persists after multiple restarts, check the camera connection status or contact customer support.',
  '0500_4037': 'Your sliced file is not compatible with current printer model. This file can\'t be printed on this printer.',
  '0500_4038': 'The nozzle diameter in sliced file is not consistent with the current nozzle setting. This file can\'t be printed.',
  '0500_4039': 'The current task does not allow the installation of the laser/cutting module, and the task has been halted.',
  '0500_403A': 'The current temperature is too low. In order to protect you and your printer, printing tasks, moving an axis and other operations are disabled. Please move the printer to an environment above 10 de...',
  '0500_403B': 'Laser/cutting tasks cannot be initiated on the machine at the moment. Please use the computer software to start the task.',
  '0500_403C': 'The current nozzle setting does not match the slicing file. Continuing to print may affect print quality. It is recommended to re-slice before starting the print.',
  '0500_403D': 'The toolhead module is not set up. Please set it up before initiating the task.',
  '0500_403E': 'The current tool head does not support initialization.',
  '0500_403F': 'Failed to download print job; please check your network connection.',
  '0500_4040': 'The printer has reached its power limit. Please connect a dedicated power adapter to this AMS to enable drying.',
  '0500_4041': 'The AMS drying cannot be started during printing.',
  '0500_4042': 'Due to power limitations, starting AMS drying will pause current operations such as nozzle heating and fan running. Do you want to proceed with drying?',
  '0500_4043': 'Due to power limitations, only one AMS is allowed to use the device\'s power for drying.',
  '0500_4044': 'BirdsEye Camera malfunction: please contact customer support.',
  '0500_4045': 'Hotend check in progress. This operation is temporarily unavailable. Please wait.',
  '0500_4050': 'Error detected on the print board.',
  '0500_4052': 'Error detected on the hot end.',
  '0500_4054': 'Error detected on the mat.',
  '0500_405D': 'Laser module Serial Number error: unable to calibrate or make project.',
  '0500_4065': 'The task requires a Laser Platform, but the current one is a Cutting Platform. Please replace it, measure the material thickness in the software, and then restart the task.',
  '0500_4070': 'The laser or cutter module is connected, so the device cannot initiate a 3D printing task.',
  '0500_4075': 'No Laser Platform was detected, which may affect thickness measurement accuracy. Please place the laser platform correctly and ensure the rear markers are not blocked, then restart the thickness me...',
  '0500_4076': 'Please place the Laser Platform correctly and ensure the rear markers are not blocked, then restart the thickness measurement in the software before initiating the task.',
  '0500_4097': 'The device cannot detect the Laser Module. Please reconnect the module cable or restart the printer.',
  '0500_4098': 'The device cannot detect AMS A. Please reconnect the AMS cable or restart the printer.',
  '0500_4099': 'The firmware of Cutting Module does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0500_409A': 'The firmware of the Air Pump does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0500_409B': 'The firmware of the Laser Module does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0500_409D': 'The firmware of AMS A does not match the printer; the device cannot continue working. Please upgrade it on the \'Firmware\' page.',
  '0500_409E': 'The device cannot detect the Cutting Module. Please reconnect the module cable or restart the printer.',
  '0500_409F': 'The device cannot detect the Air Pump.  Please reconnect the module cable or restart the printer.',
  '0500_40A0': 'The Rotary Attachment module is not detected. Please reconnect the cable or restart the printer.',
  '0500_40A1': 'The Auto Fire Extinguishing System is not detected.  Please reconnect the module cable or restart the printer.',
  '0500_40A3': 'AMS(or AMS lite) A communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0500_40A4': 'The current firmware only supports 1 AMS Lite. Please remove all AMS units before reconnecting the supported AMS Lite device.',
  '0500_40A5': 'The current firmware only supports AMS/AMS 2 Pro/AMS HT, with a maximum of 4 units. Please remove all AMS units before reconnecting the supported one.',
  '0500_8013': 'The print file is not available. Please check to see if the storage media has been removed.',
  '0500_8036': 'Your sliced file is not consistent with the current printer model. Continue?',
  '0500_803C': 'The current nozzle setting does not match the slicing file. Continuing to print may affect print quality. It is recommended to re-slice before starting the print.',
  '0500_8040': 'Toolhead front cover is detached. Moving the toolhead may damage the printer. Do you want to continue?',
  '0500_8041': 'The filament in hotend is too cold. Extrusion may damage the extruder. Still feeding in/out the filament?',
  '0500_8048': 'The module on the toolhead is not calibrated. Please cancel the task to perform calibration or switch to a calibrated module.',
  '0500_8051': 'Detected build plate is not the same as the Gcode file. Please adjust slicer settings or use the correct plate.',
  '0500_8053': 'Nozzle mismatch was detected during printing. Please initiate the print after re-slicing, or continue printing after replacing with the correct nozzle. Caution: the hotend temperature is high.',
  '0500_8055': 'Laser module is installed, but a Cutting Platform is detected. Please place a Laser Platform and perform laser calibration.',
  '0500_8056': 'Cutting module is installed, but the laser platform is detected. Please place the cutting platform for calibration.',
  '0500_8058': 'Please place the light grip cutting mat correctly and ensure the marker is exposed.',
  '0500_8059': 'Cutting platform base is not correctly aligned. Please ensure that the four corners of the platform are aligned with the heatbed.',
  '0500_805A': 'Please place the cutting mat on cutting protection base.',
  '0500_805B': 'The cutting mat type is unknown; please replace it with the correct cutting mat.',
  '0500_805C': 'The grip cutting mat type does not match; please place a LightGrip cutting mat.',
  '0500_805E': 'Cutting module Serial Number error: unable to calibrate or make project.',
  '0500_8060': 'The current module on toolhead does not meet requirements. Please replace the module as per the on-screen instructions.',
  '0500_8061': 'No print plate detected. Please make sure it is placed correctly.',
  '0500_8062': 'The print plate marker was not detected. Please confirm the print plate is correctly positioned on the heatbed with all four corners aligned, and the marker is visible. If strong light is shining o...',
  '0500_8063': 'The platform is not detected during calibration; please make sure the Laser Platform is properly placed.',
  '0500_8064': 'Please place the Laser Platform correctly and ensure the rear markers are not blocked for laser calibration.',
  '0500_8066': 'The task requires a Cutting Platform, but the current one is a Laser Platform. Please replace it with a Cutting Platform (Cutting Protection Base + LightGrip cutting mat).',
  '0500_8067': 'Please place a LightGrip cutting mat on the cutting protection base.',
  '0500_8068': 'Please place the strong grip cutting mat correctly and ensure the marker is exposed.',
  '0500_8069': 'Unable to recognize the left and right hotends. They might be third party hotends, or the hotend marks may be dirty. Please manually set the hotend types.',
  '0500_806A': 'Unable to recognize the left and right hotends. They might be third party hotends, or the hotend marks may be dirty. Please set hotend types on printer screen before next print.',
  '0500_806B': 'Quick-release Lever is not locked. Please press down the external toolhead module to ensure it is properly seated, then push down the level to lock it in place.',
  '0500_806C': 'Please place the cutting platform correctly and ensure the marker is exposed.',
  '0500_806D': 'Material not detected. Please confirm placement and continue.',
  '0500_806E': 'Foreign objects detected on heatbed; please check and clean up the heatbed.',
  '0500_806F': 'The grip cutting mat type does not match; please place a StrongGrip cutting mat.',
  '0500_8071': 'No cutting platform was detected. Please confirm that it has been correctly placed.',
  '0500_8072': 'Live View camera is blocked',
  '0500_8073': 'Heatbed limit block is obstructed or contaminated. Please clean and ensure the limit block is visible, otherwise platform position offset detection may be inaccurate.',
  '0500_8074': 'The Laser Platform is offset. Please ensure that the four corners of the platform are aligned with the heatbed, and the marker is not obstructed.',
  '0500_8077': 'The visual marker was not detected. Please ensure the paper is properly placed.',
  '0500_8078': 'Current material does not match the sliced file settings. Please load the correct material and ensure the QR code on the material is not damaged or dirty.',
  '0500_8079': 'Please place the Laser Test Material (350g paperboard) and position support strips underneath to prevent material warping.',
  '0500_807A': 'The foreign object detection function is not working. You can continue the task or check the assistant for troubleshooting.',
  '0500_807B': 'Please place the cutting platform (cutting protection base + LightGrip cutting mat).',
  '0500_807C': 'Please place the cutting platform (cutting protection base + StrongGrip cutting mat).',
  '0500_807D': 'This task requires a Cutting Platform, but the current one is a Laser Platform. Please replace it with a Cutting Platform (Cutting Protection Base + StrongGrip Cutting Mat).',
  '0500_807E': 'Please place a StrongGrip cutting mat on the cutting protection base.',
  '0500_8080': 'The left and right hotends are not installed.',
  '0500_8081': 'The left and right hotends are not installed.',
  '0500_8082': 'Please remove the protective film on the Opaque Glossy Acrylic before processing',
  '0500_8083': 'Material is not allowed in Mounting Calibration. Please remove the material from the platform.',
  '0500_8084': 'The Live View Camera is dirty; please clean it and continue.',
  '0500_8085': 'Toolhead camera is obstructed',
  '0500_8086': 'Toolhead Camera is dirty, which affects the AI function; please clean the lens surface.',
  '0500_8087': 'BirdsEye camera is obstructed',
  '0500_8088': 'The Birdseye Camera is dirty',
  '0500_8089': 'Task paused due to Presence Check failed. Please check the printer to continue.',
  '0500_808A': 'The BirdsEye Camera is installed offset. Please refer to the assistant to reinstall it.',
  '0500_808B': 'The BirdsEye Camera setup failed. Please remove all objects and the mat on the heatbed to ensure the heatbed markers are visible. Meanwhile, please ensure the BirdsEye Camera is installed correctly...',
  '0500_808C': 'Detected build plate offset. Please align the build plate with the heatbed, and then continue.',
  '0500_808D': 'The Cutting Module offset calibration failed, which may result in inaccurate cuts. Please ensure the cutting material is properly positioned and check whether the cutting blade tip is worn.',
  '0500_808E': 'BirdsEye Camera initialization failed. The toolhead camera did not detect the Heatbed features. Please clean the Heatbed, remove all objects and pads, and ensure the bed markings are visible. Check...',
  '0500_808F': 'Nozzle camera lens is dirty, affecting AI monitoring. Clean the lens with a non-woven cloth and a small amount of alcohol. Beware of hotend heat; wait for it to cool before handling.',
  '0500_8090': 'Please attach the 80g White Printing Paper to the center area of the platform.',
  '0500_8091': 'The Cutting Module offset calibration failed, which may result in inaccurate cuts. Please ensure the 80g white printer paper(letter paper thickness) is properly positioned and check whether the cut...',
  '0500_8092': 'Toolhead Camera initialization failed. This print can still continue, but some AI functions will be disabled. If you encounter this issue again after restarting, please contact customer support.',
  '0500_8093': 'The nozzle silicone sleeve is not installed; there is a risk of temperature control failure. Please install it correctly and try again.',
  '0500_80A0': 'The visual encoder board was not detected. Please check if the board is properly placed and aligned at all four corners, and ensure the positioning markings are clear and free from wear.',
  '0500_C010': 'MicroSD Card read/write exception: please reinsert or replace the MicroSD Card.',
  '0500_C032': 'Laser/Cutting module connected to the toolhead. The drying process has been automatically stopped.',
  '0500_C036': 'This is a printing task. Please detach the Laser/Cutting Module from the Toolhead.',
  '0500_C07F': 'Device is busy and cannot perform this operation. To proceed, please pause or stop the current task.',
  '0501_4017': 'Binding failed. Please retry or restart the printer and retry.',
  '0501_4018': 'Binding configuration information parsing failed; please try again.',
  '0501_4019': 'The printer has already been bound. Please unbind it and try again.',
  '0501_401A': 'Cloud access failed. Possible reasons include network instability caused by interference, inability to access the internet, or router firewall configuration restrictions. You can try moving the pri...',
  '0501_401B': 'Cloud response is invalid. If you have tried multiple times and are still failing, please contact customer support.',
  '0501_401C': 'Cloud access is rejected. If you have tried multiple times and are still failing, please contact customer support.',
  '0501_401D': 'Cloud access failed, which may be caused by network instability due to interference. You can try moving the printer closer to the router before you try again.',
  '0501_401E': 'Cloud response is invalid. If you have tried multiple times and are still failing, please contact customer support.',
  '0501_401F': 'Authorization timed out. Please make sure that your phone or PC has access to the internet, and ensure that the Bambu Studio/Bambu Handy APP is running in the foreground during the binding operation.',
  '0501_4020': 'Cloud access rejected. If you have tried multiple times and are still failing, please contact customer support.',
  '0501_4021': 'Cloud access failed, which may be caused by network instability due to interference. You can try moving the printer closer to the router before you try again.',
  '0501_4022': 'Cloud response is invalid. If you have tried multiple times and are still failing, please contact customer support.',
  '0501_4023': 'Cloud access rejected. If you have tried multiple times and are still failing, please contact customer support.',
  '0501_4024': 'Cloud access failed. Possible reasons include network instability caused by interference, inability to access the internet, or router firewall configuration restrictions. You can try moving the pri...',
  '0501_4025': 'Cloud response is invalid. If you have tried multiple times and are still failing, please contact customer support.',
  '0501_4026': 'Cloud access rejected. If you have tried multiple times and are still failing, please contact customer support.',
  '0501_4027': 'Cloud access failed; this may be caused by network instability due to interference. You can try moving the printer closer to the router before you try again.',
  '0501_4028': 'Cloud response is invalid. If you have tried multiple times and are still failing, please contact customer support.',
  '0501_4029': 'Cloud access is rejected. If you have tried multiple times and are still failing, please contact customer support.',
  '0501_4031': 'Device discovery binding is in progress, and the QR code cannot be displayed on the screen. You can wait for the binding to finish or abort the device discovery binding process in the APP/Studio an...',
  '0501_4032': 'QR code binding is in progress, so device discovery binding cannot be performed. You can scan the QR code on the screen for binding or exit the QR code display page on screen and try device discove...',
  '0501_4033': 'Your APP region does not match with your printer; please download the APP in the corresponding region and register your account again.',
  '0501_4034': 'The slicing progress has not been updated for a long time, and the printing task has exited. Please confirm the parameters and reinitiate printing.',
  '0501_4035': 'The device is in the process of binding and cannot respond to new binding requests.',
  '0501_4038': 'The regional settings do not match the printer; please check the printer\'s regional settings.',
  '0501_4039': 'Device login has expired; please try to bind again.',
  '0501_4098': 'The device cannot detect AMS B. Please reconnect the AMS cable or restart the printer.',
  '0501_409D': 'The firmware of AMS B does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0501_40A3': 'AMS(or AMS lite) B communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0502_4001': 'Current filament will be used in this print job. Settings cannot be changed.',
  '0502_4002': 'Please go to “Settings > Calibration” to run the Motion Accuracy Enhancement Calibration before turning on Motion Accuracy Enhancement mode.',
  '0502_4003': 'The printer is currently printing and the motion accuracy enhancement feature cannot be turned on or off.',
  '0502_4004': 'Some features are not supported by the current device. Please check the Studio feature settings or update the firmware to the latest version.',
  '0502_4005': 'The AMS has not been calibrated yet, so printing cannot be initiated.',
  '0502_4006': 'Unknown module detected; please try updating the firmware to the latest version.',
  '0502_400D': 'Failed to start a new task: filament loading/unloading not completed.',
  '0502_400E': 'Failed to start a new task: The nozzle cold pull was not completed.',
  '0502_4013': 'This device is not compatible with the 40W laser module. Please replace it with a 10W laser module or remove it.',
  '0502_4098': 'The device cannot detect AMS C. Please reconnect the AMS cable or restart the printer.',
  '0502_409D': 'The firmware of AMS C does not match the printer; the device cannot continue working. Please upgrade it on the \'Firmware\' page.',
  '0502_40A3': 'AMS(or AMS lite) C communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0502_C00F': 'The device is busy and cannot perform nozzle identification.',
  '0502_C010': 'Due to printer power limitations, printing, calibration, controls and other actions cannot be performed during AMS drying. Please stop the drying process before proceeding with any other operation.',
  '0502_C011': 'Currently in 2D production mode. Please continue the operation on the printer',
  '0502_C012': 'The task cannot be paused.',
  '0502_C014': 'The AMS Remaining Filament Estimation is enabled by default and cannot be disabled.',
  '0502_C024': 'The flow dynamic calibration records have exceeded the storage limit. Please delete some historical records in the slicer software before adding new calibration data.',
  '0503_4098': 'The device cannot detect AMS D. Please reconnect the AMS cable or restart the printer.',
  '0503_409D': 'The firmware of AMS D does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0503_40A3': 'AMS(or AMS lite) D communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0580_4096': 'The device cannot detect AMS-HT A. Please reconnect the AMS-HT cable or restart the printer.',
  '0580_409C': 'The firmware of AMS-HT A does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0580_40A2': 'AMS-HT A communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0581_4096': 'The device cannot detect AMS-HT B. Please reconnect the AMS-HT cable or restart the printer.',
  '0581_409C': 'The firmware of AMS-HT B does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0581_40A2': 'AMS-HT B communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0582_4096': 'The device cannot detect AMS-HT C. Please reconnect the AMS-HT cable or restart the printer.',
  '0582_409C': 'The firmware of AMS-HT C does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0582_40A2': 'AMS-HT C communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0583_4096': 'The device cannot detect AMS-HT D. Please reconnect the AMS-HT cable or restart the printer.',
  '0583_409C': 'The firmware of AMS-HT D does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0583_40A2': 'AMS-HT D communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0584_4096': 'The device cannot detect AMS-HT F. Please reconnect the AMS-HT cable or restart the printer.',
  '0584_409C': 'The firmware of AMS-HT E does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0584_40A2': 'AMS-HT E communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0585_4096': 'The device cannot detect AMS-HT E. Please reconnect the AMS-HT cable or restart the printer.',
  '0585_409C': 'The firmware of AMS-HT F does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0585_40A2': 'AMS-HT F communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0586_4096': 'The device cannot detect AMS-HT G. Please reconnect the AMS-HT cable or restart the printer.',
  '0586_409C': 'The firmware of AMS-HT G does not match the printer; the device cannot continue working. Please update it on the \'Firmware\' page.',
  '0586_40A2': 'AMS-HT G communication is abnormal. Please reconnect the module cable or restart the printer.',
  '0587_4096': 'The device cannot detect AMS-HT H. Please reconnect the AMS-HT cable or restart the printer.',
  '0587_409C': 'The firmware of AMS-HT H does not match the printer; the device cannot continue working. Please upgrade it on the \'Firmware\' page.',
  '0587_40A2': 'AMS-HT H communication is abnormal. Please reconnect the module cable or restart the printer.',
  '05FE_8053': 'The left nozzle is not matched with slicing file. Please initiate the print after re-slicing, or continue printing after replacing with the correct nozzle. Caution: the hotend temperature is high.',
  '05FE_8069': 'Unable to recognize the left hotend. It might be a third party hotend, or the hotend mark may be dirty. Please manually set the hotend type.',
  '05FE_806A': 'Unable to recognize the left hotend. It might be a third party hotend, or the hotend mark may be dirty. Please set hotend type on printer screen before next print.',
  '05FE_8080': 'The left hotend is not installed.',
  '05FE_8081': 'The left hotend is not installed.',
  '05FF_8053': 'The right nozzle is not matched with slicing file. Please initiate the print after re-slicing, or continue printing after replacing with the correct nozzle. Caution: the hotend temperature is high.',
  '05FF_8069': 'Unable to recognize the right hotend. It might be a third party hotend, or the hotend mark may be dirty. Please manually set the hotend type.',
  '05FF_806A': 'Unable to recognize the right hotend. It might be a third party hotend, or the hotend mark may be dirty. Please set hotend type on printer screen before next print.',
  '05FF_8080': 'The right hotend is not installed.',
  '05FF_8081': 'The right hotend is not installed.',
  '0700_4001': 'The AMS has been disabled for a print, but it still has filament loaded. Please unload the AMS filament and switch to the spool holder filament for printing.',
  '0700_4025': 'Failed to read the filament information.',
  '0700_8001': 'Failed to cut the filament. Please check the cutter.',
  '0700_8002': 'The cutter is stuck. Please make sure the cutter handle is out.',
  '0700_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '0700_8004': 'AMS failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '0700_8005': 'The AMS failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '0700_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '0700_8007': 'Extruding filament failed. The extruder might be clogged.',
  '0700_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS A to the extruder is properly connected.',
  '0700_8010': 'The AMS assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '0700_8011': 'AMS filament ran out. Please insert a new filament into the same AMS slot.',
  '0700_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '0700_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '0700_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '0700_8017': 'AMS A is drying. Please stop drying process before loading/unloading material.',
  '0700_8021': 'AMS setup failed; please refer to the assistant.',
  '0700_8023': 'AMS A cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '0700_C069': 'An error occurred during AMS A drying. Please go to Assistant for more details.',
  '0700_C06A': 'AMS A is reading RFID. Unable to start drying. Please try again later.',
  '0700_C06B': 'AMS A is changing filament. Unable to start drying. Please try again later.',
  '0700_C06C': 'AMS A is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '0700_C06D': 'AMS A is assisting in filament insertion. Unable to start drying. Please try again later.',
  '0700_C06E': 'AMS A motor is performing self-test. Unable to start drying. Please try again later.',
  '0701_4001': 'Filament is still loaded from the AMS after it has been disabled. Please unload the filament, load from the spool holder, and restart printing.',
  '0701_4025': 'Failed to read the filament information.',
  '0701_8001': 'Failed to cut the filament. Please check the cutter.',
  '0701_8002': 'The cutter is stuck. Please make sure the cutter handle is out.',
  '0701_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '0701_8004': 'AMS failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '0701_8005': 'The AMS failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '0701_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '0701_8007': 'Extruding filament failed. The extruder might be clogged.',
  '0701_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS B to the extruder is properly connected.',
  '0701_8010': 'The AMS assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '0701_8011': 'AMS filament ran out. Please insert a new filament into the same AMS slot.',
  '0701_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '0701_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '0701_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '0701_8017': 'AMS B is drying. Please stop drying process before loading/unloading material.',
  '0701_8021': 'AMS setup failed; please refer to the assistant.',
  '0701_8023': 'AMS B cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '0701_C069': 'An error occurred during AMS B drying. Please go to Assistant for more details.',
  '0701_C06A': 'AMS B is reading RFID. Unable to start drying. Please try again later.',
  '0701_C06B': 'AMS B is changing filament. Unable to start drying. Please try again later.',
  '0701_C06C': 'AMS B is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '0701_C06D': 'AMS B is assisting in filament insertion. Unable to start drying. Please try again later.',
  '0701_C06E': 'AMS B motor is performing self-test. Unable to start drying. Please try again later.',
  '0702_4001': 'Filament is still loaded from the AMS after it has been disabled. Please unload the filament, load from the spool holder, and restart printing.',
  '0702_4025': 'Failed to read the filament information.',
  '0702_8001': 'Failed to cut the filament. Please check the cutter.',
  '0702_8002': 'The cutter is stuck. Please make sure the cutter handle is out.',
  '0702_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '0702_8004': 'AMS failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '0702_8005': 'The AMS failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '0702_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '0702_8007': 'Extruding filament failed. The extruder might be clogged.',
  '0702_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS C to the extruder is properly connected.',
  '0702_8010': 'The AMS assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '0702_8011': 'AMS filament ran out. Please insert a new filament into the same AMS slot.',
  '0702_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '0702_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '0702_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '0702_8017': 'AMS C is drying. Please stop drying process before loading/unloading material.',
  '0702_8021': 'AMS setup failed; please refer to the assistant.',
  '0702_8023': 'AMS C cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '0702_C069': 'An error occurred during AMS C drying. Please go to Assistant for more details.',
  '0702_C06A': 'AMS C is reading RFID. Unable to start drying. Please try again later.',
  '0702_C06B': 'AMS C is changing filament. Unable to start drying. Please try again later.',
  '0702_C06C': 'AMS C is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '0702_C06D': 'AMS C is assisting in filament insertion. Unable to start drying. Please try again later.',
  '0702_C06E': 'AMS C motor is performing self-test. Unable to start drying. Please try again later.',
  '0703_4001': 'Filament is still loaded from the AMS after it has been disabled. Please unload the filament, load from the spool holder, and restart printing.',
  '0703_4025': 'Failed to read the filament information.',
  '0703_8001': 'Failed to cut the filament. Please check the cutter.',
  '0703_8002': 'The cutter is stuck. Please make sure the cutter handle is out.',
  '0703_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '0703_8004': 'AMS failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '0703_8005': 'The AMS failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '0703_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '0703_8007': 'Extruding filament failed. The extruder might be clogged.',
  '0703_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS D to the extruder is properly connected.',
  '0703_8010': 'The AMS assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '0703_8011': 'AMS filament ran out. Please insert a new filament into the same AMS slot.',
  '0703_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '0703_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '0703_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '0703_8017': 'AMS D is drying. Please stop drying process before loading/unloading material.',
  '0703_8021': 'AMS setup failed; please refer to the assistant.',
  '0703_8023': 'AMS D cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '0703_C069': 'An error occurred during AMS D drying. Please go to Assistant for more details.',
  '0703_C06A': 'AMS D is reading RFID. Unable to start drying. Please try again later.',
  '0703_C06B': 'AMS D is changing filament. Unable to start drying. Please try again later.',
  '0703_C06C': 'AMS D is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '0703_C06D': 'AMS D is assisting in filament insertion. Unable to start drying. Please try again later.',
  '0703_C06E': 'AMS D motor is performing self-test. Unable to start drying. Please try again later.',
  '0704_4025': 'Failed to read the filament information.',
  '0704_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '0704_8004': 'AMS failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '0704_8005': 'The AMS failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '0704_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '0704_8007': 'Extruding filament failed. The extruder might be clogged.',
  '0704_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS E to the extruder is properly connected.',
  '0704_8010': 'The AMS assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '0704_8011': 'AMS filament ran out. Please insert a new filament into the same AMS slot.',
  '0704_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '0704_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '0704_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '0704_8021': 'AMS setup failed; please refer to the assistant.',
  '0704_8023': 'AMS E cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '0705_4025': 'Failed to read the filament information.',
  '0705_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '0705_8004': 'AMS failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '0705_8005': 'The AMS failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '0705_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '0705_8007': 'Extruding filament failed. The extruder might be clogged.',
  '0705_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS F to the extruder is properly connected.',
  '0705_8010': 'The AMS assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '0705_8011': 'AMS filament ran out. Please insert a new filament into the same AMS slot.',
  '0705_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '0705_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '0705_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '0705_8021': 'AMS setup failed; please refer to the assistant.',
  '0705_8023': 'AMS F cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '0706_4025': 'Failed to read the filament information.',
  '0706_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '0706_8004': 'AMS failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '0706_8005': 'The AMS failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '0706_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '0706_8007': 'Extruding filament failed. The extruder might be clogged.',
  '0706_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS G to the extruder is properly connected.',
  '0706_8010': 'The AMS assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '0706_8011': 'AMS filament ran out. Please insert a new filament into the same AMS slot.',
  '0706_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '0706_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '0706_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '0706_8021': 'AMS setup failed; please refer to the assistant.',
  '0706_8023': 'AMS G cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '0707_4025': 'Failed to read the filament information.',
  '0707_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '0707_8004': 'AMS failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '0707_8005': 'The AMS failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '0707_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '0707_8007': 'Extruding filament failed. The extruder might be clogged.',
  '0707_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS H to the extruder is properly connected.',
  '0707_8010': 'The AMS assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '0707_8011': 'AMS filament ran out. Please insert a new filament into the same AMS slot.',
  '0707_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '0707_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '0707_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '0707_8021': 'AMS setup failed; please refer to the assistant.',
  '0707_8023': 'AMS H cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '07FE_8001': 'Failed to cut the filament of the left extruder. Please check the cutter.',
  '07FE_8002': 'The cutter of the left extruder is stuck. Please pull out the cutter handle.',
  '07FE_8003': 'Please pull out the filament on the spool holder  of the left extruder. If this message persists, please check to see if there is filament broken in the extruder. (Connect a PTFE tube if you are ab...',
  '07FE_8004': 'Failed to pull back the filament from the left extruder. Please check whether the filament is stuck inside the extruder.',
  '07FE_8005': 'Failed to feed the filament outside the AMS. Please clip the end of the filament flat and check to see if the spool is stuck.',
  '07FE_8006': 'Please feed filament into the PTFE tube of the left extruder until it can not be pushed any farther.',
  '07FE_8007': 'Please observe the nozzle of the left extruder. If the filament has been extruded, select \'Continue\'; if it has not, please push the filament forward slightly, and then select \'Retry\'.',
  '07FE_8010': 'Check if the left external filament spool or filament is stuck.',
  '07FE_8011': 'The external filament connected to the left extruder has run out; please load a new filament.',
  '07FE_8012': 'Failed to get mapping table; please select \'Resume\' to retry.',
  '07FE_8013': 'Timeout purging old filament of the left extruder: Please check if the filament is stuck or the extruder is clogged.',
  '07FE_8020': 'Extruder change failed; please refer to the assistant.',
  '07FE_8021': 'AMS setup failed; please refer to the assistant.',
  '07FE_8024': 'Extruder position calibration failed; please refer to the assistant.',
  '07FE_8025': 'Cold pull timed out. Please promptly operate or check whether the filament is broken inside the extruder, and click the Assistant for details.',
  '07FE_8030': 'The filament specified in the slicer has been used up. Printing is paused. Please go to the machine to replace the material and resume printing.',
  '07FE_C003': 'Please pull out the filament on the spool holder of the left extruder. If this message persists, please check to see if there is filament broken in the extruder or PTFE tube. (Connect a PTFE tube i...',
  '07FE_C006': 'Please feed filament into the PTFE tube of the left extruder until it can not be pushed any farther.',
  '07FE_C008': 'Please pull out the filament on the spool holder of the left extruder. If this message persists, please check to see if there is filament broken in the extruder or PTFE tube. (Connect a PTFE tube i...',
  '07FE_C009': 'Please feed filament into the PTFE tube of the left extruder until it can not be pushed any farther.',
  '07FE_C00A': 'Please observe the nozzle of the left extruder. If the filament has been extruded, select \'Continue\'; if not, please push the filament forward slightly and then select \'Retry\'.',
  '07FE_C010': 'Insert the filament (over 30cm long) until it stops. You might see slight smoke during flushing. After insertion, close the front door and top cover.',
  '07FE_C011': 'Please manually and slowly pull out the filament from the extruder. Then click “Continue”.',
  '07FE_C012': 'Press the black PTFE tube coupler and unplug the PTFE tube. After completing the operation, click \'Continue.\'',
  '07FF_4001': 'Filament is still loaded from the AMS after it has been disabled. Please unload the filament, load from the spool holder, and restart printing.',
  '07FF_8001': 'Failed to cut the filament of the right extruder. Please check the cutter.',
  '07FF_8002': 'The cutter is stuck. Please make sure the cutter handle is out.',
  '07FF_8003': 'Please pull out the filament on the spool holder  of the right extruder. If this message persists, please check to see if there is filament broken in the extruder. (Connect a PTFE tube if you are a...',
  '07FF_8004': 'Failed to pull back the filament from the right extruder. Please check whether the filament is stuck inside the extruder.',
  '07FF_8005': 'Failed to feed the filament outside the AMS. Please clip the end of the filament flat and check to see if the spool is stuck.',
  '07FF_8006': 'Please feed filament into the PTFE tube of the right extruder until it can not be pushed any farther.',
  '07FF_8007': 'Please observe the nozzle of the right extruder. If the filament has been extruded, select \'Continue\'; if it has not, please push the filament forward slightly, and then select \'Retry\'.',
  '07FF_8010': 'Check if the external filament spool or filament is stuck.',
  '07FF_8011': 'External filament has run out; please load a new filament.',
  '07FF_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '07FF_8013': 'Timeout purging old filament of the right extruder: Please check if the filament is stuck or the extruder is clogged.',
  '07FF_8020': 'Extruder change failed; please refer to the assistant.',
  '07FF_8021': 'AMS setup failed; please refer to the assistant.',
  '07FF_8024': 'Extruder position calibration failed; please refer to the assistant.',
  '07FF_8025': 'Cold pull timed out. Please promptly operate or check whether the filament is broken inside the extruder, and click the Assistant for details.',
  '07FF_8030': 'The filament specified in the slicer has been used up. Printing is paused. Please go to the machine to replace the material and resume printing.',
  '07FF_C003': 'Please pull out the filament on the spool holder of the right extruder. If this message persists, please check to see if there is filament broken in the extruder or PTFE tube. (Connect a PTFE tube ...',
  '07FF_C006': 'Please feed filament into the PTFE tube of the right extruder until it can not be pushed any farther.',
  '07FF_C008': 'Please pull out the filament on the spool holder of the right extruder. If this message persists, please check to see if there is filament broken in the extruder or PTFE tube. (Connect a PTFE tube ...',
  '07FF_C009': 'Please feed filament into the PTFE tube of the right extruder until it can not be pushed any farther.',
  '07FF_C00A': 'Please observe the nozzle of the right extruder. If the filament has been extruded, select \'Continue\'; if not, please push the filament forward slightly and then select \'Retry\'.',
  '07FF_C010': 'Insert the filament (over 30cm long) until it stops. You might see slight smoke during flushing. After insertion, close the front door and top cover.',
  '07FF_C011': 'Hold the driven wheel bracket, slowly pull the filament from the extruder, then press \'Continue\'.',
  '07FF_C012': 'Press the black PTFE tube coupler and unplug the PTFE tube. After completing the operation, click \'Continue.\'',
  '0C00_4020': 'The setup of BirdsEye Camera failed. Please clear all objects and remove the mat. Make sure the marker is not obstructed. Meanwhile, clean both the BirdsEye Camera and Toolhead Camera, and remove a...',
  '0C00_4021': 'The setup of BirdsEye Camera failed; please reboot the printer.',
  '0C00_4022': 'The setup of BirdsEye Camera failed.  Please check if the laser module is working properly.',
  '0C00_4024': 'The Birdseye Camera is installed offset. Please refer to the assistant to reinstall it.',
  '0C00_4025': 'The Birdseye Camera is dirty. Please clean it and restart the process.',
  '0C00_4026': 'The Live View Camera initialization failed; please reboot the printer.',
  '0C00_4027': 'The Live View Camera calibration failed. Please refer to the assistant for details and recalibrate the camera after processing.',
  '0C00_4029': 'Material not detected. Please confirm placement and continue.',
  '0C00_402A': 'The visual marker was not detected. Please re-paste the paper in the correct position.',
  '0C00_402C': 'Device data link error. Please reboot the printer',
  '0C00_402D': 'The toolhead camera is not working properly; please reboot the device.',
  '0C00_403D': 'The vision encoder plate was not detected. Please confirm it is correctly positioned on the heatbed.',
  '0C00_403E': 'The high-precision nozzle offset calibration has failed, possibly due to a damaged pattern or the similarity of the colors of the two selected filaments. Please clear the printed pattern and replac...',
  '0C00_4041': 'Toolhead camera calibration failed. Please ensure the Calibration Marker on the heatbed or Height Calibration Marker on the homing area is clean and undamaged, then re-run the calibration process.',
  '0C00_8001': 'First layer defects were detected. If the defects are acceptable, select \'Resume\' to resume the print job.',
  '0C00_8005': 'Purged filament has piled up in the waste chute, which may cause a tool head collision.',
  '0C00_8009': 'Build plate localization marker was not found.',
  '0C00_800B': 'The heatbed marker was not detected. Please clear all objects and remove the mat. Make sure the marker is not obstructed.',
  '0C00_8015': 'Objects detected on the platform; please clean them up in a timely manner.',
  '0C00_8016': 'The foreign object detection function is not working. You can continue the task or check assistant for solutions.',
  '0C00_8017': 'Foreign objects detected on the platform; please clean them up on time.',
  '0C00_8018': 'The foreign object detection function is not working. You can continue the task or view the assistant for troubleshooting.',
  '0C00_8033': 'Quick-release Lever is not locked. Please push it down to secure.',
  '0C00_8034': 'Liveview Camera initialization failed. This print can still continue, but some AI functions will be disabled. If you encounter this issue again after restarting, please contact customer support.',
  '0C00_803F': 'AI detected nozzle clumping. Please check the nozzle condition. Refer to assistant for solutions.',
  '0C00_8040': 'AI detected air-printing defect. Please check the hotend extrusion status. Refer to assistant for solutions.',
  '0C00_8042': 'The AI print monitor has detected a spaghetti defect. Please check the print and take the necessary action.',
  '0C00_8043': 'AI detected nozzle clumping. Please check the nozzle condition. Refer to assistant for solutions.',
  '0C00_C003': 'Possible defects were detected in the first layer.',
  '0C00_C004': 'Possible spaghetti failure was detected.',
  '0C00_C006': 'Purged filament may have piled up in the waste chute.',
  '1000_C001': 'High bed temperature may lead to filament clogging in the nozzle. You may open the chamber door.',
  '1000_C002': 'Printing CF material with stainless steel may cause nozzle damage.',
  '1000_C003': 'Enabling Timelapse in traditional mode may cause defects; please activate this feature as needed.',
  '1001_4001': 'Timelapse is not supported as Spiral Vase mode is enabled in slicing presets.',
  '1001_4002': 'Timelapse is not supported as the Print sequence is set to \'By object\'.',
  '1001_8003': 'The time-lapse mode is set to Traditional in the slicing file. This may cause surface defects. Would you like to enable it?',
  '1001_8004': 'Prime Tower is not enabled and time-lapse mode is set to Smooth in slicing file. This may cause surface defects. Would you like to enable it?',
  '1200_4001': 'Filament is still loaded from the AMS when it has been disabled. Please unload AMS filament, load from spool holder, and restart print job.',
  '1200_8001': 'Cutting the filament failed. Please check to see if the cutter is stuck. Refer to the Assistant for solutions.',
  '1200_8002': 'The cutter is stuck. Please pull out the cutter handle.',
  '1200_8003': 'Failed to pull out the filament from the extruder. Please check whether the extruder is clogged or whether the filament is broken inside the extruder.',
  '1200_8004': 'Failed to pull back the filament from the toolhead. Please check whether the filament is stuck.',
  '1200_8005': 'The filament is not inserted. Please insert the filament.',
  '1200_8006': 'Unable to feed filament into the extruder. This could be due to tangled filament or a stuck spool. If not, please check if the AMS PTFE tube is connected.',
  '1200_8007': 'Failed to extrude the filament. This might be caused by clogged extruder or stuck filament. Refer to the Assistant for solutions.',
  '1200_8010': 'Filament or spool may be stuck.',
  '1200_8011': 'AMS filament has run out. Please insert a new filament into the same AMS slot.',
  '1200_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1200_8013': 'Timeout while purging old filament. Please check if the filament is stuck or the extruder clogged.',
  '1200_8014': 'The filament location in the toolhead was not found. Refer to the Assistant for solutions.',
  '1200_8015': 'Failed to pull out the filament from the toolhead. Please check if the filament is stuck, or if it is broken inside the extruder or PTFE tube.',
  '1200_8016': 'The extruder is not extruding normally. Refer to the Assistant for troubleshooting. There may be defects in this layer, but you may resume if the defects are acceptable.',
  '1201_4001': 'Filament is still loaded from the AMS when it has been disabled. Please unload AMS filament, load from spool holder, and restart print job.',
  '1201_8001': 'Failed to cut the filament. Please check the cutter.',
  '1201_8002': 'The cutter is stuck. Please pull out the cutter handle.',
  '1201_8003': 'Failed to pull out the filament from the extruder. Please check whether the extruder is clogged or whether the filament is broken inside the extruder.',
  '1201_8004': 'Failed to pull back the filament from the toolhead. Please check whether the filament is stuck.',
  '1201_8005': 'Failed to feed the filament. Please load the filament and then select \'Retry\'.',
  '1201_8006': 'Failed to feed the filament into the toolhead. Please check whether the filament is stuck.',
  '1201_8007': 'Failed to extrude the filament. The extruder may be clogged or the filament may be stuck; please refer to HMS.',
  '1201_8010': 'Please check if the spool or filament is stuck.',
  '1201_8011': 'AMS filament has run out. Please insert a new filament into the same AMS slot.',
  '1201_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1201_8013': 'Timeout while purging old filament. Please check if the filament is stuck or the extruder clogged.',
  '1201_8014': 'Failed to check the filament location in the tool head; please refer to the HMS.',
  '1201_8015': 'Failed to pull back the filament from the toolhead. Please check if the filament is stuck or the filament is broken inside the extruder.',
  '1201_8016': 'The extruder is not extruding normally; please refer to the HMS. After trouble shooting, if the defects are acceptable, please resume printing.',
  '1202_4001': 'Filament is still loaded from the AMS when it has been disabled. Please unload AMS filament, load from spool holder, and restart print job.',
  '1202_8001': 'Failed to cut the filament. Please check the cutter.',
  '1202_8002': 'The cutter is stuck. Please pull out the cutter handle.',
  '1202_8003': 'Failed to pull out the filament from the extruder. Please check whether the extruder is clogged or whether the filament is broken inside the extruder.',
  '1202_8004': 'Failed to pull back the filament from the toolhead. Please check whether the filament is stuck.',
  '1202_8005': 'The filament is not inserted. Please insert the filament.',
  '1202_8006': 'Failed to feed the filament into the toolhead. Please check whether the filament is stuck.',
  '1202_8007': 'Failed to extrude the filament. The extruder may be clogged or the filament may be stuck; please refer to HMS.',
  '1202_8010': 'Please check if the spool or filament is stuck.',
  '1202_8011': 'AMS filament has run out. Please insert a new filament into the same AMS slot.',
  '1202_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1202_8013': 'Timeout while purging old filament. Please check if the filament is stuck or the extruder clogged.',
  '1202_8014': 'Failed to check the filament location in the tool head; please refer to the HMS.',
  '1202_8015': 'Failed to pull back the filament from the toolhead. Please check if the filament is stuck or is broken inside the extruder.',
  '1202_8016': 'The extruder is not extruding normally; please refer to the HMS. After trouble shooting, if the defects are acceptable, please resume printing.',
  '1203_4001': 'Filament is still loaded from the AMS when it has been disabled. Please unload AMS filament, load from spool holder, and restart print job.',
  '1203_8001': 'Failed to cut the filament. Please check the cutter.',
  '1203_8002': 'The cutter is stuck. Please pull out the cutter handle.',
  '1203_8003': 'Failed to pull out the filament from the extruder. Please check whether the extruder is clogged or whether the filament is broken inside the extruder.',
  '1203_8004': 'Failed to pull back the filament from the toolhead. Please check whether the filament is stuck.',
  '1203_8005': 'The filament is not inserted. Please insert the filament.',
  '1203_8006': 'Failed to feed the filament into the toolhead. Please check whether the filament is stuck.',
  '1203_8007': 'Failed to extrude the filament. The extruder may be clogged or the filament may be stuck; please refer to HMS.',
  '1203_8010': 'Please check if the spool or filament is stuck.',
  '1203_8011': 'AMS filament has run out. Please insert a new filament into the same AMS slot.',
  '1203_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1203_8013': 'Timeout while purging old filament. Please check if the filament is stuck or the extruder clogged.',
  '1203_8014': 'Failed to check the filament location in the tool head; please refer to the HMS.',
  '1203_8015': 'Failed to pull back the filament from the toolhead. Please check if the filament is stuck or is broken inside the extruder.',
  '1203_8016': 'The extruder is not extruding normally; please refer to the HMS. After trouble shooting, if the defects are acceptable, please resume printing.',
  '12FF_4001': 'Filament is still loaded from the AMS when it has been disabled. Please unload AMS filament, load from spool holder, and restart print job.',
  '12FF_8001': 'Failed to cut the filament. Please check the cutter.',
  '12FF_8002': 'The cutter is stuck. Please pull out the cutter handle.',
  '12FF_8003': 'Please pull out the filament on the spool holder. If this message persists, please check to see if there is filament broken in the extruder or PTFE tube. (Connect a PTFE tube if you are about to us...',
  '12FF_8004': 'Failed to pull back the filament from the toolhead. Please check whether the filament is stuck.',
  '12FF_8005': 'The filament is not inserted. Please insert the filament.',
  '12FF_8006': 'Please feed filament into the PTFE tube until it can not be pushed any farther.',
  '12FF_8007': 'Check nozzle. Select \'Done\' if filament was extruded, otherwise push filament forward slightly and select \'Retry.\'',
  '12FF_8010': 'Please check if the filament or the spool is stuck.',
  '12FF_8011': 'AMS filament has run out. Please insert a new filament into the same AMS slot.',
  '12FF_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '12FF_8013': 'Timeout while purging old filament. Please check if the filament is stuck or the extruder clogged.',
  '12FF_C003': 'Please pull out the filament on the spool holder. If this message persists, please check to see if there is filament broken in the extruder or PTFE Tube. (Connect a PTFE tube if you are about to us...',
  '12FF_C006': 'Please feed filament into the PTFE tube until it can not be pushed any farther.',
  '1800_4025': 'Failed to read the filament information.',
  '1800_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '1800_8004': 'AMS-HT failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '1800_8005': 'The AMS-HT failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '1800_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '1800_8007': 'Extruding filament failed. The extruder might be clogged.',
  '1800_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS-HT A to the extruder is properly connected.',
  '1800_8010': 'The AMS-HT assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '1800_8011': 'AMS-HT filament ran out. Please insert a new filament into the same AMS-HT slot.',
  '1800_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1800_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '1800_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '1800_8017': 'AMS-HT A is drying. Please stop drying process before loading/unloading material.',
  '1800_8021': 'AMS setup failed; please refer to the assistant.',
  '1800_8023': 'AMS-HT A cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '1800_C069': 'An error occurred during AMS-HT A drying. Please go to Assistant for more details.',
  '1800_C06A': 'AMS-HT A is reading RFID. Unable to start drying. Please try again later.',
  '1800_C06B': 'AMS-HT A is changing filament. Unable to start drying. Please try again later.',
  '1800_C06C': 'AMS-HT A is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '1800_C06D': 'AMS-HT A is assisting in filament insertion. Unable to start drying. Please try again later.',
  '1800_C06E': 'AMS-HT A motor is performing self-test. Unable to start drying. Please try again later.',
  '1801_4025': 'Failed to read the filament information.',
  '1801_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '1801_8004': 'AMS-HT failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '1801_8005': 'The AMS-HT failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '1801_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '1801_8007': 'Extruding filament failed. The extruder might be clogged.',
  '1801_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS-HT B to the extruder is properly connected.',
  '1801_8010': 'The AMS-HT assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '1801_8011': 'AMS-HT filament ran out. Please insert a new filament into the same AMS-HT slot.',
  '1801_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1801_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '1801_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '1801_8017': 'AMS-HT B is drying. Please stop drying process before loading/unloading material.',
  '1801_8021': 'AMS setup failed; please refer to the assistant.',
  '1801_8023': 'AMS-HT B cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '1801_C069': 'An error occurred during AMS-HT B drying. Please go to Assistant for more details.',
  '1801_C06A': 'AMS-HT B is reading RFID. Unable to start drying. Please try again later.',
  '1801_C06B': 'AMS-HT B is changing filament. Unable to start drying. Please try again later.',
  '1801_C06C': 'AMS-HT B is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '1801_C06D': 'AMS-HT B is assisting in filament insertion. Unable to start drying. Please try again later.',
  '1801_C06E': 'AMS-HT B motor is performing self-test. Unable to start drying. Please try again later.',
  '1802_4025': 'Failed to read the filament information.',
  '1802_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '1802_8004': 'AMS-HT failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '1802_8005': 'The AMS-HT failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '1802_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '1802_8007': 'Extruding filament failed. The extruder might be clogged.',
  '1802_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS-HT C to the extruder is properly connected.',
  '1802_8010': 'The AMS-HT assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '1802_8011': 'AMS-HT filament ran out. Please insert a new filament into the same AMS-HT slot.',
  '1802_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1802_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '1802_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '1802_8017': 'AMS-HT C is drying. Please stop drying process before loading/unloading material.',
  '1802_8021': 'AMS setup failed; please refer to the assistant.',
  '1802_8023': 'AMS-HT C cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '1802_C069': 'An error occurred during AMS-HT C drying. Please go to Assistant for more details.',
  '1802_C06A': 'AMS-HT C is reading RFID. Unable to start drying. Please try again later.',
  '1802_C06B': 'AMS-HT C is changing filament. Unable to start drying. Please try again later.',
  '1802_C06C': 'AMS-HT C is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '1802_C06D': 'AMS-HT C is assisting in filament insertion. Unable to start drying. Please try again later.',
  '1802_C06E': 'AMS-HT C motor is performing self-test. Unable to start drying. Please try again later.',
  '1803_4025': 'Failed to read the filament information.',
  '1803_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '1803_8004': 'AMS-HT failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '1803_8005': 'The AMS-HT failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '1803_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '1803_8007': 'Extruding filament failed. The extruder might be clogged.',
  '1803_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS-HT D to the extruder is properly connected.',
  '1803_8010': 'The AMS-HT assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '1803_8011': 'AMS-HT filament ran out. Please insert a new filament into the same AMS-HT slot.',
  '1803_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1803_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '1803_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '1803_8017': 'AMS-HT D is drying. Please stop drying process before loading/unloading material.',
  '1803_8021': 'AMS setup failed; please refer to the assistant.',
  '1803_8023': 'AMS-HT D cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '1803_C069': 'An error occurred during AMS-HT D drying. Please go to Assistant for more details.',
  '1803_C06A': 'AMS-HT D is reading RFID. Unable to start drying. Please try again later.',
  '1803_C06B': 'AMS-HT D is changing filament. Unable to start drying. Please try again later.',
  '1803_C06C': 'AMS-HT D is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '1803_C06D': 'AMS-HT D is assisting in filament insertion. Unable to start drying. Please try again later.',
  '1803_C06E': 'AMS-HT D motor is performing self-test. Unable to start drying. Please try again later.',
  '1804_4025': 'Failed to read the filament information.',
  '1804_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '1804_8004': 'AMS-HT failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '1804_8005': 'The AMS-HT failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '1804_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '1804_8007': 'Extruding filament failed. The extruder might be clogged.',
  '1804_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS-HT E to the extruder is properly connected.',
  '1804_8010': 'The AMS-HT assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '1804_8011': 'AMS-HT filament ran out. Please insert a new filament into the same AMS-HT slot.',
  '1804_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1804_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '1804_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '1804_8021': 'AMS setup failed; please refer to the assistant.',
  '1804_8023': 'AMS-HT E cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '1804_C069': 'An error occurred during AMS-HT E drying. Please go to Assistant for more details.',
  '1804_C06A': 'AMS-HT E is reading RFID. Unable to start drying. Please try again later.',
  '1804_C06B': 'AMS-HT E is changing filament. Unable to start drying. Please try again later.',
  '1804_C06C': 'AMS-HT E is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '1804_C06D': 'AMS-HT E is assisting in filament insertion. Unable to start drying. Please try again later.',
  '1804_C06E': 'AMS-HT E motor is performing self-test. Unable to start drying. Please try again later.',
  '1805_4025': 'Failed to read the filament information.',
  '1805_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '1805_8004': 'AMS-HT failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '1805_8005': 'The AMS-HT failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '1805_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '1805_8007': 'Extruding filament failed. The extruder might be clogged.',
  '1805_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS-HT F to the extruder is properly connected.',
  '1805_8010': 'The AMS-HT assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '1805_8011': 'AMS-HT filament ran out. Please insert a new filament into the same AMS-HT slot.',
  '1805_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1805_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '1805_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '1805_8021': 'AMS setup failed; please refer to the assistant.',
  '1805_8023': 'AMS-HT F cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '1805_C069': 'An error occurred during AMS-HT F drying. Please go to Assistant for more details.',
  '1805_C06A': 'AMS-HT F is reading RFID. Unable to start drying. Please try again later.',
  '1805_C06B': 'AMS-HT F is changing filament. Unable to start drying. Please try again later.',
  '1805_C06C': 'AMS-HT F is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '1805_C06D': 'AMS-HT F is assisting in filament insertion. Unable to start drying. Please try again later.',
  '1805_C06E': 'AMS-HT F motor is performing self-test. Unable to start drying. Please try again later.',
  '1806_4025': 'Failed to read the filament information.',
  '1806_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '1806_8004': 'AMS-HT failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '1806_8005': 'The AMS-HT failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '1806_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '1806_8007': 'Extruding filament failed. The extruder might be clogged.',
  '1806_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS-HT G to the extruder is properly connected.',
  '1806_8010': 'The AMS-HT assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '1806_8011': 'AMS-HT filament ran out. Please insert a new filament into the same AMS-HT slot.',
  '1806_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1806_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '1806_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '1806_8021': 'AMS setup failed; please refer to the assistant.',
  '1806_8023': 'AMS-HT G cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '1806_C069': 'An error occurred during AMS-HT G drying. Please go to Assistant for more details.',
  '1806_C06A': 'AMS-HT G is reading RFID. Unable to start drying. Please try again later.',
  '1806_C06B': 'AMS-HT G is changing filament. Unable to start drying. Please try again later.',
  '1806_C06C': 'AMS-HT G is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '1806_C06D': 'AMS-HT G is assisting in filament insertion. Unable to start drying. Please try again later.',
  '1806_C06E': 'AMS-HT G motor is performing self-test. Unable to start drying. Please try again later.',
  '1807_4025': 'Failed to read the filament information.',
  '1807_8003': 'Failed to pull out the filament from the extruder. This might be caused by clogged extruder or filament broken inside the extruder.',
  '1807_8004': 'AMS-HT failed to pull back filament. This could be due to a stuck spool or the end of the filament being stuck in the path.',
  '1807_8005': 'The AMS-HT failed to send out filament. You can clip the end of your filament flat, and reinsert. If this message persists, please check the PTFE tubes in AMS for any signs of wear and tear.',
  '1807_8006': 'Unable to feed filament into the extruder. The AMS may be mismatched with the extruder. You can rerun the AMS Setup. This could also be due to an entangled filament or a stuck spool. If not, please...',
  '1807_8007': 'Extruding filament failed. The extruder might be clogged.',
  '1807_800A': 'PTFE tube disconnection detected. Please check if the PTFE tube from AMS-HT H to the extruder is properly connected.',
  '1807_8010': 'The AMS-HT assist motor is overloaded. This could be due to entangled filament or a stuck spool.',
  '1807_8011': 'AMS-HT filament ran out. Please insert a new filament into the same AMS-HT slot.',
  '1807_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '1807_8013': 'Timeout purging old filament: Please check if the filament is stuck or the extruder is clogged.',
  '1807_8016': 'The extruder is not extruding normally; please refer to the Assistant. After trouble shooting. If the defects are acceptable, please resume.',
  '1807_8021': 'AMS setup failed; please refer to the assistant.',
  '1807_8023': 'AMS-HT H cooling failed. The ambient temperature may be too high. Please operate the device in a suitable environment.',
  '1807_C069': 'An error occurred during AMS-HT H drying. Please go to Assistant for more details.',
  '1807_C06A': 'AMS-HT H is reading RFID. Unable to start drying. Please try again later.',
  '1807_C06B': 'AMS-HT H is changing filament. Unable to start drying. Please try again later.',
  '1807_C06C': 'AMS-HT H is in Feed Assist Mode. Unable to start drying. Please try again later.',
  '1807_C06D': 'AMS-HT H is assisting in filament insertion. Unable to start drying. Please try again later.',
  '1807_C06E': 'AMS-HT H motor is performing self-test. Unable to start drying. Please try again later.',
  '18FE_8001': 'Failed to cut the filament of the left extruder. Please check the cutter.',
  '18FE_8002': 'The cutter of the left extruder is stuck. Please pull out the cutter handle.',
  '18FE_8003': 'Please pull out the filament on the spool holder  of the left extruder. If this message persists, please check to see if there is filament broken in the extruder. (Connect a PTFE tube if you are ab...',
  '18FE_8004': 'Failed to pull back the filament from the left extruder. Please check whether the filament is stuck inside the extruder.',
  '18FE_8005': 'Failed to feed the filament outside the AMS-HT. Please clip the end of the filament flat and check to see if the spool is stuck.',
  '18FE_8006': 'Please feed filament into the PTFE tube of the left extruder until it can not be pushed any farther.',
  '18FE_8007': 'Please observe the nozzle of the left extruder. If the filament has been extruded, select \'Continue\'; if it has not, please push the filament forward slightly, and then select \'Retry\'.',
  '18FE_8011': 'The external filament connected to the left extruder has run out; please load a new filament.',
  '18FE_8012': 'Failed to get mapping table; please select \'Resume\' to retry.',
  '18FE_8013': 'Timeout purging old filament of the left extruder: Please check if the filament is stuck or the extruder is clogged.',
  '18FE_8020': 'Extruder change failed; please refer to the assistant.',
  '18FE_8021': 'AMS setup failed; please refer to the assistant.',
  '18FE_8024': 'Extruder position calibration failed; please refer to the assistant.',
  '18FE_C003': 'Please pull out the filament on the spool holder of the left extruder. If this message persists, please check to see if there is filament broken in the extruder or PTFE tube. (Connect a PTFE tube i...',
  '18FE_C006': 'Please feed filament into the PTFE tube of the left extruder until it can not be pushed any farther.',
  '18FE_C008': 'Please pull out the filament on the spool holder of the left extruder. If this message persists, please check to see if there is filament broken in the extruder or PTFE tube. (Connect a PTFE tube i...',
  '18FE_C009': 'Please feed filament into the PTFE tube of the left extruder until it can not be pushed any farther.',
  '18FE_C00A': 'Please observe the nozzle of the left extruder. If the filament has been extruded, select \'Continue\'; if not, please push the filament forward slightly and then select \'Retry\'.',
  '18FF_8001': 'Failed to cut the filament of the right extruder. Please check the cutter.',
  '18FF_8002': 'The cutter of the right extruder is stuck. Please pull out the cutter handle.',
  '18FF_8003': 'Please pull out the filament on the spool holder  of the right extruder. If this message persists, please check to see if there is filament broken in the extruder. (Connect a PTFE tube if you are a...',
  '18FF_8004': 'Failed to pull back the filament from the right extruder. Please check whether the filament is stuck inside the extruder.',
  '18FF_8005': 'Failed to feed the filament outside the AMS-HT. Please clip the end of the filament flat and check to see if the spool is stuck.',
  '18FF_8006': 'Please feed filament into the PTFE tube of the right extruder until it can not be pushed any farther.',
  '18FF_8007': 'Please observe the nozzle of the right extruder. If the filament has been extruded, select \'Continue\'; if it has not, please push the filament forward slightly, and then select \'Retry\'.',
  '18FF_8011': 'The external filament connected to the right extruder has run out; please load a new filament.',
  '18FF_8012': 'Failed to get AMS mapping table; please select \'Resume\' to retry.',
  '18FF_8013': 'Timeout purging old filament of the right extruder: Please check if the filament is stuck or the extruder is clogged.',
  '18FF_8020': 'Extruder change failed; please refer to the assistant.',
  '18FF_8021': 'AMS setup failed; please refer to the assistant.',
  '18FF_8024': 'Extruder position calibration failed; please refer to the assistant.',
  '18FF_C003': 'Please pull out the filament on the spool holder of the right extruder. If this message persists, please check to see if there is filament broken in the extruder or PTFE tube. (Connect a PTFE tube ...',
  '18FF_C006': 'Please feed filament into the PTFE tube of the right extruder until it can not be pushed any farther.',
  '18FF_C008': 'Please pull out the filament on the spool holder of the right extruder. If this message persists, please check to see if there is filament broken in the extruder or PTFE tube. (Connect a PTFE tube ...',
  '18FF_C009': 'Please feed filament into the PTFE tube of the right extruder until it can not be pushed any farther.',
  '18FF_C00A': 'Please observe the nozzle of the right extruder. If the filament has been extruded, select \'Continue\'; if not, please push the filament forward slightly and then select \'Retry\'.',
};

function getSeverityInfo(severity: number): { label: string; color: string; bgColor: string; Icon: typeof AlertTriangle } {
  switch (severity) {
    case 1:
      return { label: 'Fatal', color: 'text-red-500', bgColor: 'bg-red-500/20', Icon: AlertTriangle };
    case 2:
      return { label: 'Serious', color: 'text-red-700 dark:text-red-400', bgColor: 'bg-red-100 dark:bg-red-500/15', Icon: AlertTriangle };
    case 3:
      return { label: 'Warning', color: 'text-orange-700 dark:text-orange-400', bgColor: 'bg-orange-100 dark:bg-orange-500/20', Icon: AlertCircle };
    case 4:
    default:
      return { label: 'Info', color: 'text-blue-700 dark:text-blue-400', bgColor: 'bg-blue-100 dark:bg-blue-500/20', Icon: Info };
  }
}

function getShortCode(attr: number, code: number): string {
  // Convert attr and code to short format: XXXX_YYYY
  // attr contains the module info, code contains the error number
  const module = ((attr >> 16) & 0xFFFF) || ((attr >> 8) & 0xFF) << 8 | (attr & 0xFF);
  const codeNum = code & 0xFFFF;
  return `${module.toString(16).padStart(4, '0').toUpperCase()}_${codeNum.toString(16).padStart(4, '0').toUpperCase()}`;
}

// Catalog lookup. full_code (16 hex chars for hms[]-sourced faults) is tried
// first because it is lossless; shortCode is the fallback that the bulk of the
// catalog is keyed by. Returns undefined for an uncataloged error so callers can
// tell "no description" from "empty description".
function lookupDescription(fullCode: string | undefined, shortCode: string): string | undefined {
  if (fullCode && ERROR_DESCRIPTIONS[fullCode] !== undefined) return ERROR_DESCRIPTIONS[fullCode];
  return ERROR_DESCRIPTIONS[shortCode];
}

// Helper to filter HMS errors the UI should surface (exported for use in badge counts).
// Keeps an error if EITHER:
//   - it's in the bundled ERROR_DESCRIPTIONS catalog (known, has a description), OR
//   - it carries firmware actions (uncataloged but user-actionable — e.g. H2C 0500_809C
//     with IGNORE_RESUME/PROBLEM_SOLVED_RESUME — must surface so the button can render).
// Drops uncataloged errors WITHOUT actions: those are transient junk like the post-cancel
// 0C00_001B echo that re-introduces the FAILED-after-cancel "1 problem forever"
// regression — see PrintersPageBucketing.test.ts.
export function filterKnownHMSErrors(errors: HMSError[]): HMSError[] {
  return errors.filter((error) => {
    const codeNum = parseInt(error.code.replace('0x', ''), 16) || 0;
    const shortCode = getShortCode(error.attr, codeNum);
    if (lookupDescription(error.full_code, shortCode) !== undefined) return true;
    return (error.actions?.length ?? 0) > 0;
  });
}

function getHMSHomeUrl(): string {
  return `https://wiki.bambulab.com/en/hms/home`;
}

export function HMSErrorModal({ printerName, errors, onClose, printerId, hasPermission, runoutGuidance }: HMSErrorModalProps) {
  const { t } = useTranslation();
  const { showToast } = useToast();
  const queryClient = useQueryClient();

  const clearMutation = useMutation({
    mutationFn: () => api.clearHMSErrors(printerId),
    onSuccess: () => {
      showToast(t('hmsErrors.clearSuccess'), 'success');
      onClose();
    },
    onError: () => {
      showToast(t('hmsErrors.clearFailed'), 'error');
    },
  });

  // Surface cataloged errors and uncataloged-but-actionable errors. Mirrors
  // filterKnownHMSErrors so the modal and the badge counts agree.
  const knownErrors = filterKnownHMSErrors(errors);

  // Close on Escape key
  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      if (e.key === 'Escape') onClose();
    };
    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, [onClose]);

  // printerStatusMutation with optimistic update
  const activateActionMutation = useMutation({
    mutationFn: (data: {
      action: string,
      print_error: string,
      job_id: string | null,
    }) => api.executeHMSAction(printerId, {
      action: data.action,
      print_error: data.print_error,
      job_id: data.job_id,
    }),
    onSuccess: () => {
      // Scope the invalidation to THIS printer. The prefix form
      // `['printerStatus']` would refresh every printer card on the page,
      // which is wasteful when only one printer's state actually changed.
      queryClient.invalidateQueries({ queryKey: ['printerStatus', printerId] });
      showToast(t('hmsErrors.actionSuccess', 'Action sent to printer'), 'success');
      onClose();
    },
    onError: (error: Error) => {
      showToast(
        `${t('hmsErrors.actionFailed', 'Failed to send action')}: ${error.message}`,
        'error',
      );
    },
  });

  return (
    <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
      <div className="bg-bambu-dark-secondary rounded-lg shadow-xl max-w-lg w-full max-h-[80vh] flex flex-col">
        {/* Header */}
        <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
          <div className="flex items-center gap-2">
            <AlertTriangle className="w-5 h-5 text-orange-600 dark:text-orange-400" />
            <h2 className="text-lg font-semibold text-white">{t('hmsErrors.title', { name: printerName })}</h2>
          </div>
          <button
            onClick={onClose}
            className="p-1 hover:bg-bambu-dark-tertiary rounded-lg transition-colors"
          >
            <X className="w-5 h-5 text-bambu-gray" />
          </button>
        </div>

        {/* Content */}
        <div className="flex-1 overflow-y-auto p-4">
          {knownErrors.length === 0 ? (
            <div className="text-center py-8 text-bambu-gray">
              <AlertCircle className="w-12 h-12 mx-auto mb-3 opacity-30" />
              <p>{t('hmsErrors.noErrors')}</p>
            </div>
          ) : (
            <div className="space-y-3">
              {knownErrors.map((error, index) => {
                const { label, color, bgColor, Icon } = getSeverityInfo(error.severity);
                const codeNum = parseInt(error.code.replace('0x', ''), 16) || 0;
                const shortCode = getShortCode(error.attr, codeNum);
                // Runout guidance (#2587): for an AMS per-slot runout on a paused
                // print, name the slot the firmware now expects rather than the
                // misleading generic "insert into the same slot" text.
                const matchedFullCode =
                  !!error.full_code && ERROR_DESCRIPTIONS[error.full_code] !== undefined;
                let description =
                  lookupDescription(error.full_code, shortCode) ?? t('hmsErrors.unknownCode');
                // The remedy is Bambuddy's, not Bambu's — their wiki says "update
                // Studio or Handy", which is no help to someone printing from
                // Bambuddy. Same override shape as the runout guidance below.
                const remedy =
                  error.full_code === HMS_MQTT_VERIFY_FAILED
                    ? t('hmsErrors.mqttVerifyFailedRemedy')
                    : null;
                if (runoutGuidance && AMS_RUNOUT_SHORT_CODES.has(shortCode)) {
                  if (runoutGuidance.expectedSlotLabel && runoutGuidance.ranOutSlotLabel) {
                    description = t('hmsErrors.runoutExpectedSlot', {
                      expected: runoutGuidance.expectedSlotLabel,
                      ranOut: runoutGuidance.ranOutSlotLabel,
                    });
                  } else if (runoutGuidance.expectedSlotLabel) {
                    description = t('hmsErrors.runoutExpectedSlotOnly', {
                      expected: runoutGuidance.expectedSlotLabel,
                    });
                  } else {
                    description = t('hmsErrors.runoutSlotUnknown');
                  }
                }
                const hmsHomeUrl = getHMSHomeUrl();
                // Show the printer's own four-group notation when the short code
                // could not identify the error — for those, "0500-0007" matches
                // nothing the user can look up, while "0500-0500-0001-0007" is
                // exactly what the printer screen and the Bambu wiki show.
                const displayCode =
                  matchedFullCode && error.full_code!.length === 16
                    ? error.full_code!.match(/.{4}/g)!.join('-')
                    : shortCode.replace('_', '-');

                return (
                  <div
                    key={`${error.code}-${index}`}
                    className={`p-4 rounded-lg ${bgColor} border border-white/10`}
                  >
                    <div className="flex items-start gap-3">
                      <Icon className={`w-5 h-5 ${color} flex-shrink-0 mt-0.5`} />
                      <div className="flex-1 min-w-0">
                        <div className="flex items-center gap-2 mb-1">
                          <span className={`font-mono text-sm ${color}`}>[{displayCode}]</span>
                          <span className={`text-xs px-2 py-0.5 rounded-full ${bgColor} ${color}`}>
                            {label}
                          </span>
                        </div>
                        <p className="text-sm text-bambu-gray mb-2">{description}</p>
                        {remedy && <p className="text-sm text-bambu-gray mb-2">{remedy}</p>}
                        {error.actions && error.actions.length > 0 && (
                          <div className="flex flex-wrap gap-2 my-2">
                            {error.actions.map((action) => {
                              const pendingVars = activateActionMutation.variables;
                              const isThisPending =
                                activateActionMutation.isPending
                                && pendingVars?.action === action
                                && pendingVars?.print_error === (error.full_code || shortCode.replace('_', ''));
                              return (
                                <button
                                  key={action}
                                  onClick={() => {
                                    // full_code is the firmware-matching key (16
                                    // chars for hms[]-array faults, 8 chars for
                                    // print_error). Fall back to the 8-char
                                    // shortCode for older backends that haven't
                                    // populated it. See #1830.
                                    activateActionMutation.mutate({
                                      action,
                                      print_error: error.full_code || shortCode.replace('_', ''),
                                      job_id: error.job_id ?? null,
                                    });
                                  }}
                                  // Static hover/active classes — Tailwind's JIT
                                  // can't resolve `hover:${var}` template
                                  // literals, so the previous severity-tinted
                                  // hover never reached the compiled CSS and
                                  // the action buttons read as inert badges.
                                  // White-on-tint reads as a clear affordance
                                  // against any severity-coloured container.
                                  disabled={!hasPermission('printers:control') || activateActionMutation.isPending}
                                  className="flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium rounded-lg bg-white/10 hover:bg-white/20 active:bg-white/30 text-white border border-white/20 hover:border-white/30 transition-colors disabled:opacity-50 disabled:cursor-not-allowed flex-shrink-0"
                                >
                                  {isThisPending && <Loader2 className="w-4 h-4 animate-spin" />}
                                  {t(`hmsErrors.actions.${action}`, action)}
                                </button>
                              );
                            })}
                          </div>
                        )}
                        <a
                          href={hmsHomeUrl}
                          target="_blank"
                          rel="noopener noreferrer"
                          className="inline-flex items-center gap-1 text-xs text-bambu-green hover:underline"
                        >
                          <ExternalLink className="w-3 h-3" />
                          {t('hmsErrors.viewOnWiki')}
                        </a>
                      </div>
                    </div>
                  </div>
                );
              })}
            </div>
          )}
        </div>

        {/* Footer */}
        <div className="p-4 border-t border-bambu-dark-tertiary flex items-center justify-between gap-3">
          <p className="text-xs text-bambu-gray">
            {t('hmsErrors.clearInstructions')}
          </p>
          {knownErrors.length > 0 && (
            <button
              onClick={() => clearMutation.mutate()}
              disabled={!hasPermission('printers:control') || clearMutation.isPending}
              className="flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium rounded-lg bg-red-100 dark:bg-red-500/20 text-red-700 dark:text-red-400 hover:bg-red-200 dark:hover:bg-red-500/30 transition-colors disabled:opacity-50 disabled:cursor-not-allowed flex-shrink-0"
            >
              {clearMutation.isPending ? (
                <Loader2 className="w-4 h-4 animate-spin" />
              ) : (
                <Trash2 className="w-4 h-4" />
              )}
              {t('hmsErrors.clearErrors')}
            </button>
          )}
        </div>
      </div>
    </div>
  );
}
