"use strict";
const Q = require("q");
const boardIdInfo = require("./board_ids.json");
function detectDevice(device, _options) {
    return readBoardId(device.usbDevice)
        .then((boardId) => decodeBoardId(boardId));
}
exports.detectDevice = detectDevice;
function readBoardId(device) {
    const deferred = Q.defer();
    try {
        device.open();
        device.getStringDescriptor(3, (err, buf) => {
            device.close();
            if (err) {
                deferred.reject(err);
            }
            else {
                deferred.resolve(buf.toString());
            }
        });
    }
    catch (err) {
        device.close();
        deferred.reject(err);
    }
    return deferred.promise;
}
exports.readBoardId = readBoardId;
function decodeBoardId(boardId) {
    const key = boardId.slice(0, 4);
    if (key in boardIdInfo) {
        return boardIdInfo[key];
    }
    else {
        return {
            name: undefined,
        };
    }
}
