"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PositionFetcherTemplateRegistry = void 0;
const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
const lodash_1 = require("lodash");
const build_template_registry_1 = require("../utils/build-template-registry");
const contract_interface_1 = require("./contract.interface");
const app_token_template_position_fetcher_1 = require("./template/app-token.template.position-fetcher");
const contract_position_template_position_fetcher_1 = require("./template/contract-position.template.position-fetcher");
const custom_contract_position_template_position_fetcher_1 = require("./template/custom-contract-position.template.position-fetcher");
let PositionFetcherTemplateRegistry = class PositionFetcherTemplateRegistry {
    constructor(discoveryService) {
        this.discoveryService = discoveryService;
        this.appTokenTemplateRegistry = new Map();
        this.contractPositionTemplateRegistry = new Map();
        this.customContractPositionTemplateRegistry = new Map();
    }
    onModuleInit() {
        this.appTokenTemplateRegistry = (0, build_template_registry_1.buildTemplateRegistry)(this.discoveryService, {
            template: app_token_template_position_fetcher_1.AppTokenTemplatePositionFetcher,
            keySelector: t => [t.appId, t.network, t.groupId],
        });
        this.contractPositionTemplateRegistry = (0, build_template_registry_1.buildTemplateRegistry)(this.discoveryService, {
            template: contract_position_template_position_fetcher_1.ContractPositionTemplatePositionFetcher,
            keySelector: t => [t.appId, t.network, t.groupId],
        });
        this.customContractPositionTemplateRegistry = (0, build_template_registry_1.buildTemplateRegistry)(this.discoveryService, {
            template: custom_contract_position_template_position_fetcher_1.CustomContractPositionTemplatePositionFetcher,
            keySelector: t => [t.appId, t.network, t.groupId],
        });
    }
    getTemplateType(template) {
        return template instanceof app_token_template_position_fetcher_1.AppTokenTemplatePositionFetcher ? contract_interface_1.ContractType.APP_TOKEN : contract_interface_1.ContractType.POSITION;
    }
    getAppTokenTemplate({ network, appId, groupId }) {
        var _a, _b, _c;
        return (_c = (_b = (_a = this.appTokenTemplateRegistry.get(appId)) === null || _a === void 0 ? void 0 : _a.get(network)) === null || _b === void 0 ? void 0 : _b.get(groupId)) !== null && _c !== void 0 ? _c : null;
    }
    getAppTokenTemplates({ network, appId }) {
        var _a, _b, _c;
        return Array.from((_c = (_b = (_a = this.appTokenTemplateRegistry.get(appId)) === null || _a === void 0 ? void 0 : _a.get(network)) === null || _b === void 0 ? void 0 : _b.values()) !== null && _c !== void 0 ? _c : []);
    }
    getContractPositionTemplate({ network, appId, groupId }) {
        var _a, _b, _c;
        return (_c = (_b = (_a = this.contractPositionTemplateRegistry.get(appId)) === null || _a === void 0 ? void 0 : _a.get(network)) === null || _b === void 0 ? void 0 : _b.get(groupId)) !== null && _c !== void 0 ? _c : null;
    }
    getContractPositionTemplates({ network, appId }) {
        var _a, _b, _c;
        return Array.from((_c = (_b = (_a = this.contractPositionTemplateRegistry.get(appId)) === null || _a === void 0 ? void 0 : _a.get(network)) === null || _b === void 0 ? void 0 : _b.values()) !== null && _c !== void 0 ? _c : []);
    }
    getCustomContractPositionTemplate({ network, appId, groupId }) {
        var _a, _b, _c;
        return (_c = (_b = (_a = this.customContractPositionTemplateRegistry.get(appId)) === null || _a === void 0 ? void 0 : _a.get(network)) === null || _b === void 0 ? void 0 : _b.get(groupId)) !== null && _c !== void 0 ? _c : null;
    }
    getCustomContractPositionTemplates({ network, appId }) {
        var _a, _b, _c;
        return Array.from((_c = (_b = (_a = this.customContractPositionTemplateRegistry.get(appId)) === null || _a === void 0 ? void 0 : _a.get(network)) === null || _b === void 0 ? void 0 : _b.values()) !== null && _c !== void 0 ? _c : []);
    }
    getTemplatesForApp(appId) {
        var _a, _b, _c, _d, _e, _f;
        const appTokenNetworks = Array.from((_b = (_a = this.appTokenTemplateRegistry.get(appId)) === null || _a === void 0 ? void 0 : _a.keys()) !== null && _b !== void 0 ? _b : []);
        const contractPositionNetworks = Array.from((_d = (_c = this.contractPositionTemplateRegistry.get(appId)) === null || _c === void 0 ? void 0 : _c.keys()) !== null && _d !== void 0 ? _d : []);
        const customPositions = Array.from((_f = (_e = this.customContractPositionTemplateRegistry.get(appId)) === null || _e === void 0 ? void 0 : _e.keys()) !== null && _f !== void 0 ? _f : []);
        const networks = (0, lodash_1.uniq)([...appTokenNetworks, ...contractPositionNetworks, ...customPositions]);
        return networks.flatMap(network => this.getTemplatesForAppOnNetwork(appId, network));
    }
    getAppHasTemplates(appId) {
        var _a, _b, _c, _d, _e, _f;
        const appTokenNetworks = Array.from((_b = (_a = this.appTokenTemplateRegistry.get(appId)) === null || _a === void 0 ? void 0 : _a.keys()) !== null && _b !== void 0 ? _b : []);
        const contractPositionNetworks = Array.from((_d = (_c = this.contractPositionTemplateRegistry.get(appId)) === null || _c === void 0 ? void 0 : _c.keys()) !== null && _d !== void 0 ? _d : []);
        const customPositions = Array.from((_f = (_e = this.customContractPositionTemplateRegistry.get(appId)) === null || _e === void 0 ? void 0 : _e.keys()) !== null && _f !== void 0 ? _f : []);
        const networks = (0, lodash_1.uniq)([...appTokenNetworks, ...contractPositionNetworks, ...customPositions]);
        return networks.some(network => this.getAppHasTemplatesOnNetwork(appId, network));
    }
    getTemplatesForAppOnNetwork(appId, network) {
        var _a, _b, _c, _d, _e, _f;
        const appTokenTemplates = (_a = this.appTokenTemplateRegistry.get(appId)) === null || _a === void 0 ? void 0 : _a.get(network);
        const contractPositionTemplates = (_b = this.contractPositionTemplateRegistry.get(appId)) === null || _b === void 0 ? void 0 : _b.get(network);
        const customPositions = (_c = this.customContractPositionTemplateRegistry.get(appId)) === null || _c === void 0 ? void 0 : _c.get(network);
        return [
            ...Array.from((_d = appTokenTemplates === null || appTokenTemplates === void 0 ? void 0 : appTokenTemplates.values()) !== null && _d !== void 0 ? _d : []),
            ...Array.from((_e = contractPositionTemplates === null || contractPositionTemplates === void 0 ? void 0 : contractPositionTemplates.values()) !== null && _e !== void 0 ? _e : []),
            ...Array.from((_f = customPositions === null || customPositions === void 0 ? void 0 : customPositions.values()) !== null && _f !== void 0 ? _f : []),
        ];
    }
    getAppHasTemplatesOnNetwork(appId, network) {
        var _a, _b, _c;
        const appTokenTemplates = (_a = this.appTokenTemplateRegistry.get(appId)) === null || _a === void 0 ? void 0 : _a.get(network);
        const contractPositionTemplates = (_b = this.contractPositionTemplateRegistry.get(appId)) === null || _b === void 0 ? void 0 : _b.get(network);
        const customPositionTemplates = (_c = this.customContractPositionTemplateRegistry.get(appId)) === null || _c === void 0 ? void 0 : _c.get(network);
        return !!appTokenTemplates || !!contractPositionTemplates || !!customPositionTemplates;
    }
    getAllTemplates() {
        var _a, _b, _c;
        const appTokenAppIds = Array.from((_a = this.appTokenTemplateRegistry.keys()) !== null && _a !== void 0 ? _a : []);
        const contractAppIds = Array.from((_b = this.contractPositionTemplateRegistry.keys()) !== null && _b !== void 0 ? _b : []);
        const customAppIds = Array.from((_c = this.customContractPositionTemplateRegistry.keys()) !== null && _c !== void 0 ? _c : []);
        const appIds = (0, lodash_1.uniq)([...appTokenAppIds, ...contractAppIds, ...customAppIds]);
        return appIds.flatMap(appId => this.getTemplatesForApp(appId));
    }
};
PositionFetcherTemplateRegistry = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, common_1.Inject)(core_1.DiscoveryService)),
    __metadata("design:paramtypes", [core_1.DiscoveryService])
], PositionFetcherTemplateRegistry);
exports.PositionFetcherTemplateRegistry = PositionFetcherTemplateRegistry;
//# sourceMappingURL=position-fetcher.template-registry.js.map