"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 __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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var AppsModule_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppsModule = void 0;
const console_1 = require("console");
const fs_1 = require("fs");
const common_1 = require("@nestjs/common");
const chalk_1 = __importDefault(require("chalk"));
const lodash_1 = require("lodash");
const app_dynamic_module_1 = require("../app/app.dynamic-module");
const app_importer_1 = require("../app/app.importer");
let AppsModule = AppsModule_1 = class AppsModule {
    static resolveAppModules() {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const allAppIds = (0, fs_1.readdirSync)(__dirname, { withFileTypes: true })
                .filter(dirent => dirent.isDirectory())
                .filter(dirent => !['__tests__'].includes(dirent.name))
                .map(dirent => dirent.name);
            const isProdOrTest = process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'test';
            const configuredAppIds = (0, lodash_1.compact)(((_a = process.env.ENABLED_APPS) !== null && _a !== void 0 ? _a : '').split(','));
            if (!isProdOrTest && configuredAppIds.length === 0) {
                const message = (0, chalk_1.default) `{red No apps have been configured! Set {yellow ENABLED_APPS} in your {yellow .env} file, then restart. Example: {yellow ENABLED_APPS=synthetix,uniswap-v2}}`;
                this.logger.error(message);
                process.exit(1);
            }
            const enabledAppIds = isProdOrTest ? allAppIds : (0, lodash_1.intersection)(configuredAppIds, allAppIds);
            const appsModules = yield Promise.all(enabledAppIds.map((appId) => __awaiter(this, void 0, void 0, function* () { return (0, app_importer_1.importAppModule)(appId); })));
            return (0, lodash_1.compact)(appsModules);
        });
    }
    static registerAsync(opts) {
        return __awaiter(this, void 0, void 0, function* () {
            const { appToolkitModule } = opts;
            const appModules = yield this.resolveAppModules();
            (0, console_1.log)(chalk_1.default.yellow(`Enabled app modules: ${appModules.map(v => v.name).join(',')}`));
            return {
                module: AppsModule_1,
                imports: [
                    ...(0, app_dynamic_module_1.DynamicApps)({
                        apps: [...(0, lodash_1.compact)(appModules)],
                        imports: [appToolkitModule],
                    }),
                ],
            };
        });
    }
};
AppsModule.logger = new common_1.Logger(AppsModule_1.name);
AppsModule = AppsModule_1 = __decorate([
    (0, common_1.Module)({})
], AppsModule);
exports.AppsModule = AppsModule;
//# sourceMappingURL=apps.module.js.map