"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.DeviceScreenType = void 0;
var axios_1 = __importDefault(require("axios"));
var os = __importStar(require("os"));
var path = __importStar(require("path"));
var fs = __importStar(require("fs"));
var fs_extra_1 = __importDefault(require("fs-extra"));
var lodash_1 = require("lodash");
var logger_1 = require("../logger");
var LOCAL_CACHE_DEVICE_PATH = path.resolve(os.homedir(), ".zepp/.zeus_devices");
var serverPath = 'https://upload-cdn.zepp.com/zeppos/devkit/zeus/devices.json';
var DeviceScreenType;
(function (DeviceScreenType) {
    DeviceScreenType["R"] = "r";
    DeviceScreenType["S"] = "s";
    DeviceScreenType["B"] = "b";
})(DeviceScreenType = exports.DeviceScreenType || (exports.DeviceScreenType = {}));
var shapeMap = {
    square: DeviceScreenType.S,
    round: DeviceScreenType.R,
    bar: DeviceScreenType.B
};
var localContent = {};
if (fs.existsSync(LOCAL_CACHE_DEVICE_PATH)) {
    try {
        localContent = fs_extra_1["default"].readJsonSync(LOCAL_CACHE_DEVICE_PATH);
    }
    catch (_e) { }
}
var now = Date.now().valueOf();
if (!((0, lodash_1.get)(localContent, 'expiredTime') > now)) {
    axios_1["default"].get(serverPath).then(function (res) {
        if (res.status === 200) {
            var data = res.data;
            if (!(0, lodash_1.isEmpty)(data)) {
                fs_extra_1["default"].writeJsonSync(LOCAL_CACHE_DEVICE_PATH, {
                    devices: data,
                    expiredTime: now + 24 * 60 * 60 * 1000
                });
            }
        }
    })["catch"](function () {
        console.log();
        logger_1.logger.error('[Fetch remote device list]: please check network status');
        process.exit(1);
    });
}
var deviceList = (0, lodash_1.get)(localContent, 'devices', []);
var deviceGroup = (0, lodash_1.groupBy)(deviceList, 'productName');
var deviceInfo = {};
Object.values(deviceGroup).forEach(function (devices) {
    var _a;
    var info = {
        deviceName: '',
        platforms: [],
        screen: {
            type: DeviceScreenType.R,
            width: 0,
            height: 0
        },
        apiLevelLimit: {
            min: '1.0.0'
        }
    };
    var deviceInnerName = devices.reduce(function (acc, cur) {
        !info.deviceName && (info.deviceName = cur.productName);
        var screenType = (0, lodash_1.get)(shapeMap, (0, lodash_1.get)(cur, 'value.shape'));
        var _a = (0, lodash_1.get)(cur, 'value.screen.size', '').split('*'), width = _a[0], height = _a[1];
        if (screenType) {
            info.screen.type = screenType;
        }
        if (width && height) {
            info.screen.width = Number(width);
            info.screen.height = Number(height);
        }
        info.platforms.push({
            name: cur.code,
            deviceSource: cur.deviceSource
        });
        Object.assign(info.apiLevelLimit, {
            min: (0, lodash_1.get)(cur, 'value.os.apiLevelLimitMin', '1.0.0'),
            max: (0, lodash_1.get)(cur, 'value.os.version', '')
        });
        return cur.code.length < acc.length ? cur.code : acc;
    }, devices[0].code);
    var lowerName = (0, lodash_1.toLower)(deviceInnerName.replace(/[^a-zA-Z]/g, ''));
    lowerName && Object.assign(deviceInfo, (_a = {},
        _a[lowerName] = info,
        _a));
});
exports["default"] = deviceInfo;
