import { ChargingModes } from "@ledgerhq/types-devices";
import { TransportStatusError, StatusCodes } from "@ledgerhq/errors";
import { LocalTracer } from "@ledgerhq/logs";
import { LOG_TYPE } from ".";
export var BatteryStatusTypes;
(function (BatteryStatusTypes) {
    BatteryStatusTypes[BatteryStatusTypes["BATTERY_PERCENTAGE"] = 0] = "BATTERY_PERCENTAGE";
    BatteryStatusTypes[BatteryStatusTypes["BATTERY_VOLTAGE"] = 1] = "BATTERY_VOLTAGE";
    BatteryStatusTypes[BatteryStatusTypes["BATTERY_TEMPERATURE"] = 2] = "BATTERY_TEMPERATURE";
    BatteryStatusTypes[BatteryStatusTypes["BATTERY_CURRENT"] = 3] = "BATTERY_CURRENT";
    BatteryStatusTypes[BatteryStatusTypes["BATTERY_FLAGS"] = 4] = "BATTERY_FLAGS";
})(BatteryStatusTypes || (BatteryStatusTypes = {}));
// Nb USB and BLE masks not used by implementation since we have
// them from the model.
export var FlagMasks;
(function (FlagMasks) {
    FlagMasks[FlagMasks["CHARGING"] = 1] = "CHARGING";
    FlagMasks[FlagMasks["USB"] = 2] = "USB";
    FlagMasks[FlagMasks["USB_POWERED"] = 8] = "USB_POWERED";
    FlagMasks[FlagMasks["BLE"] = 4] = "BLE";
    FlagMasks[FlagMasks["ISSUE_BATTERY"] = 128] = "ISSUE_BATTERY";
    FlagMasks[FlagMasks["ISSUE_CHARGING"] = 16] = "ISSUE_CHARGING";
    FlagMasks[FlagMasks["ISSUE_TEMPERATURE"] = 32] = "ISSUE_TEMPERATURE";
})(FlagMasks || (FlagMasks = {}));
const getBatteryStatus = async (transport, statuses) => {
    const tracer = new LocalTracer(LOG_TYPE, {
        function: "getBatteryStatus",
        statuses,
    });
    tracer.trace(`Starting`);
    const result = [];
    for (let i = 0; i < statuses.length; i++) {
        const res = await transport.send(0xe0, 0x10, 0x00, statuses[i]);
        const status = res.readUInt16BE(res.length - 2);
        if (status !== StatusCodes.OK) {
            tracer.trace(`Error: status ${status}`, { status, res });
            throw new TransportStatusError(status);
        }
        switch (statuses[i]) {
            case BatteryStatusTypes.BATTERY_PERCENTAGE: {
                // Nb values greater that 100 would mean a bad case
                // to be assessed if we want to break the flow.
                const temp = res.readUInt8(0);
                result[i] = temp > 100 ? -1 : temp;
                break;
            }
            case BatteryStatusTypes.BATTERY_VOLTAGE:
                result[i] = res.readUInt16BE(0);
                break;
            case BatteryStatusTypes.BATTERY_TEMPERATURE:
            case BatteryStatusTypes.BATTERY_CURRENT:
                // Nb turn the usigned byte into a signed int to cover
                // negative values. Two's compliment.
                result[i] = (res.readUInt8() << 24) >> 24;
                break;
            case BatteryStatusTypes.BATTERY_FLAGS: {
                const flags = res.readUInt16BE(2); // Nb Ignoring the first two bytes
                const chargingUSB = !!(flags & FlagMasks.USB_POWERED);
                const chargingQi = !chargingUSB && !!(flags & FlagMasks.CHARGING);
                result[i] = {
                    charging: chargingQi
                        ? ChargingModes.QI
                        : chargingUSB
                            ? ChargingModes.USB
                            : ChargingModes.NONE,
                    issueCharging: !!(flags & FlagMasks.ISSUE_CHARGING),
                    issueTemperature: !!(flags & FlagMasks.ISSUE_TEMPERATURE),
                    issueBattery: !!(flags & FlagMasks.ISSUE_BATTERY),
                };
                break;
            }
        }
    }
    return result;
};
export default getBatteryStatus;
//# sourceMappingURL=getBatteryStatus.js.map