"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.singleStrategyRisk = exports.defined = void 0;
var vaults_1 = require("../vaults");
var defiSafetyUtils_1 = require("./defiSafetyUtils");
var messariUtils_1 = require("./messariUtils");
var loanLiquidation_1 = require("./configs/loanLiquidation");
var smartContractVulnerabilityConfig_1 = require("./configs/smartContractVulnerabilityConfig");
var cascadingLiquidationConfig_1 = require("./configs/cascadingLiquidationConfig");
var riskUtils_1 = require("./riskUtils");
var assetsConfig_1 = require("./configs/assetsConfig");
var depegConfig_1 = require("./configs/depegConfig");
var protocolsConfig_1 = require("./configs/protocolsConfig");
var strategyVolatilityConfig_1 = require("./configs/strategyVolatilityConfig");
// function checkIfLink(x: string[] | number | string): boolean {
//   return x != null && typeof x === 'object' && typeof x?.[0] === 'string';
// }
function getSum(categories) {
    return categories.reduce(function (acc, x) {
        if ((x === null || x === void 0 ? void 0 : x.score) == null)
            return acc;
        return (acc += x.score);
    }, 0);
}
function defined(arr) {
    return arr === null || arr === void 0 ? void 0 : arr.filter(function (x) { return x != null; });
}
exports.defined = defined;
function singleStrategyRisk(airtable, record) {
    var _a, _b, _c, _d;
    return __awaiter(this, void 0, void 0, function () {
        var Loan_Liquidation_Table, Cascading_Liquidation_Table, Smart_Contract_Vulnerability_Table, Strategy_Volatility_Table, Depeg_Table, Strategies_Table, id, Name, STRATEGIES, ASSETS, PROTOCOLS /* CHAINS */, _e, type, network, strategyCell, liqCell, cascadingCell, contractCell, stratVolCell, liqRisk, cascadingRisk, contractSecRisk, stratVolRisk, strategyRiskItems, strategyRiskScore, strategyRisk, assetCells, eachAssetRisk, _f, assetRisk, protocolCells, eachProtocolRisk, _g, protocolRisk, allItems, allScore;
        var _this = this;
        return __generator(this, function (_h) {
            switch (_h.label) {
                case 0:
                    Loan_Liquidation_Table = airtable["Loan Liquidation"], Cascading_Liquidation_Table = airtable["Cascading Liquidation"], Smart_Contract_Vulnerability_Table = airtable["Smart Contract Vulnerability"], Strategy_Volatility_Table = airtable["Strategy Volatility"], Depeg_Table = airtable.Depeg, Strategies_Table = airtable.STRATEGIES;
                    id = record.id, Name = record.Name, STRATEGIES = record.STRATEGIES, ASSETS = record.ASSETS, PROTOCOLS = record.PROTOCOLS;
                    _e = (0, vaults_1.getInfoFromName)(Name), type = _e.type, network = _e.network;
                    strategyCell = (0, riskUtils_1.getCellById)(STRATEGIES === null || STRATEGIES === void 0 ? void 0 : STRATEGIES[0], Strategies_Table);
                    liqCell = (0, riskUtils_1.getCellById)((_a = strategyCell === null || strategyCell === void 0 ? void 0 : strategyCell['Loan Liquidation']) === null || _a === void 0 ? void 0 : _a[0], Loan_Liquidation_Table);
                    cascadingCell = (0, riskUtils_1.getCellById)((_b = strategyCell === null || strategyCell === void 0 ? void 0 : strategyCell['Cascading Liquidation']) === null || _b === void 0 ? void 0 : _b[0], Cascading_Liquidation_Table);
                    contractCell = (0, riskUtils_1.getCellById)((_c = strategyCell === null || strategyCell === void 0 ? void 0 : strategyCell['Smart Contract Vulnerability']) === null || _c === void 0 ? void 0 : _c[0], Smart_Contract_Vulnerability_Table);
                    stratVolCell = (0, riskUtils_1.getCellById)((_d = strategyCell === null || strategyCell === void 0 ? void 0 : strategyCell['Strategy Volatility']) === null || _d === void 0 ? void 0 : _d[0], Strategy_Volatility_Table);
                    liqRisk = liqCell && (0, loanLiquidation_1.calcLoanLiquidationRisk)(liqCell);
                    cascadingRisk = cascadingCell && (0, cascadingLiquidationConfig_1.calcCascadingLiquidationRisk)(cascadingCell);
                    contractSecRisk = contractCell && (0, smartContractVulnerabilityConfig_1.calcSmartContractVulnerabilityRisk)(contractCell);
                    stratVolRisk = stratVolCell && (0, strategyVolatilityConfig_1.calcStrategyVolatilityRisk)(stratVolCell);
                    strategyRiskItems = defined([
                        liqRisk,
                        cascadingRisk,
                        stratVolRisk,
                        contractSecRisk,
                    ]);
                    strategyRiskScore = getSum(strategyRiskItems);
                    strategyRisk = {
                        id: 'strategyRisk',
                        name: 'Strategy Risk',
                        items: strategyRiskItems,
                        score: strategyRiskScore,
                    };
                    assetCells = ASSETS === null || ASSETS === void 0 ? void 0 : ASSETS.map(function (key) { return (0, riskUtils_1.getCellById)(key, airtable.ASSETS); });
                    _f = defined;
                    return [4 /*yield*/, Promise.all(defined(assetCells === null || assetCells === void 0 ? void 0 : assetCells.map(function (asset) { return __awaiter(_this, void 0, void 0, function () {
                            var messari_asset_id, Depeg, messariAssetData, depegCell, depegRisk;
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0:
                                        if (!asset)
                                            return [2 /*return*/];
                                        messari_asset_id = asset.messari_asset_id, Depeg = asset.Depeg;
                                        return [4 /*yield*/, (0, messariUtils_1.getMessariAssetData)(messari_asset_id)];
                                    case 1:
                                        messariAssetData = _a.sent();
                                        depegCell = (0, riskUtils_1.getCellById)(Depeg === null || Depeg === void 0 ? void 0 : Depeg[0], Depeg_Table);
                                        depegRisk = depegCell && (0, depegConfig_1.calcDepegRisk)(depegCell);
                                        return [2 /*return*/, (0, assetsConfig_1.calcAssetRisk)(asset, messariAssetData, depegRisk)];
                                }
                            });
                        }); })))];
                case 1:
                    eachAssetRisk = _f.apply(void 0, [_h.sent()]);
                    assetRisk = {
                        name: 'Asset Risk',
                        id: 'assetRisk',
                        score: getSum(eachAssetRisk),
                        items: eachAssetRisk,
                    };
                    protocolCells = PROTOCOLS === null || PROTOCOLS === void 0 ? void 0 : PROTOCOLS.map(function (key) {
                        return (0, riskUtils_1.getCellById)(key, airtable.PROTOCOLS);
                    });
                    _g = defined;
                    return [4 /*yield*/, Promise.all(defined(protocolCells === null || protocolCells === void 0 ? void 0 : protocolCells.map(function (protocol) { return __awaiter(_this, void 0, void 0, function () {
                            var defisafety_protocol_id, defiSafetyProtocolData;
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0:
                                        if (!protocol)
                                            return [2 /*return*/];
                                        defisafety_protocol_id = protocol.defisafety_protocol_id;
                                        return [4 /*yield*/, (0, defiSafetyUtils_1.getProtocolRatingData)(defisafety_protocol_id)];
                                    case 1:
                                        defiSafetyProtocolData = _a.sent();
                                        return [2 /*return*/, (0, protocolsConfig_1.calcProtocolsRisk)(protocol, defiSafetyProtocolData)];
                                }
                            });
                        }); })))];
                case 2:
                    eachProtocolRisk = _g.apply(void 0, [_h.sent()]);
                    protocolRisk = {
                        name: 'Protocol Risk',
                        id: 'protocolRisk',
                        score: getSum(eachProtocolRisk),
                        items: eachProtocolRisk,
                    };
                    allItems = defined([strategyRisk, assetRisk, protocolRisk]);
                    allScore = getSum(allItems);
                    return [2 /*return*/, {
                            id: id,
                            type: type,
                            network: network,
                            name: Name,
                            score: allScore,
                            items: allItems,
                        }];
            }
        });
    });
}
exports.singleStrategyRisk = singleStrategyRisk;
//# sourceMappingURL=singleStrategyRisk.js.map