"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBatteryStatus = void 0;
const types_devices_1 = require("@ledgerhq/types-devices");
const errors_1 = require("@ledgerhq/errors");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const getBatteryStatus_1 = require("../../hw/getBatteryStatus");
function getBatteryStatus({ transport, statusType, }) {
    return new rxjs_1.Observable(subscriber => {
        return (0, rxjs_1.from)(transport.send(0xe0, 0x10, 0x00, statusType))
            .pipe((0, operators_1.switchMap)((res) => {
            const status = res.readUInt16BE(res.length - 2);
            if (status !== errors_1.StatusCodes.OK) {
                throw new errors_1.TransportStatusError(status);
            }
            switch (statusType) {
                case getBatteryStatus_1.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);
                    return (0, rxjs_1.of)({
                        type: "data",
                        batteryStatus: temp > 100 ? -1 : temp,
                    });
                }
                case getBatteryStatus_1.BatteryStatusTypes.BATTERY_VOLTAGE:
                    return (0, rxjs_1.of)({
                        type: "data",
                        batteryStatus: res.readUInt16BE(0),
                    });
                case getBatteryStatus_1.BatteryStatusTypes.BATTERY_TEMPERATURE:
                case getBatteryStatus_1.BatteryStatusTypes.BATTERY_CURRENT:
                    // Nb turn the usigned byte into a signed int to cover
                    // negative values. Two's compliment.
                    return (0, rxjs_1.of)({
                        type: "data",
                        batteryStatus: (res.readUInt8() << 24) >> 24,
                    });
                case getBatteryStatus_1.BatteryStatusTypes.BATTERY_FLAGS: {
                    const flags = res.readUInt16BE(2); // Nb Ignoring the first two bytes
                    const chargingUSB = !!(flags & getBatteryStatus_1.FlagMasks.USB_POWERED);
                    const chargingQi = !chargingUSB && !!(flags & getBatteryStatus_1.FlagMasks.CHARGING);
                    return (0, rxjs_1.of)({
                        type: "data",
                        batteryStatus: {
                            charging: chargingQi
                                ? types_devices_1.ChargingModes.QI
                                : chargingUSB
                                    ? types_devices_1.ChargingModes.USB
                                    : types_devices_1.ChargingModes.NONE,
                            issueCharging: !!(flags & getBatteryStatus_1.FlagMasks.ISSUE_CHARGING),
                            issueTemperature: !!(flags & getBatteryStatus_1.FlagMasks.ISSUE_TEMPERATURE),
                            issueBattery: !!(flags & getBatteryStatus_1.FlagMasks.ISSUE_BATTERY),
                        },
                    });
                }
            }
        }))
            .subscribe(subscriber);
    });
}
exports.getBatteryStatus = getBatteryStatus;
exports.default = getBatteryStatus;
//# sourceMappingURL=getBatteryStatus.js.map