"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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCoreModuleNames = exports.getDynamicModuleNamesBasedOnMetadata = exports.getDynamicModuleNames = void 0;
const common_1 = require("@nestjs/common");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const logger = new common_1.Logger('module.helper');
const getDynamicModuleNames = () => {
    const dynamicModulesToExclude = process.env.SOLID_DYNAMIC_MODULES_TO_EXCLUDE?.split(',') || [];
    const srcPath = path.join(process.cwd(), 'src');
    const coreModuleNames = (0, exports.getCoreModuleNames)();
    const allExcludedModules = [...new Set([...coreModuleNames, ...dynamicModulesToExclude])];
    const directories = fs.readdirSync(srcPath, { withFileTypes: true });
    const enabledModules = directories
        .filter(dirent => {
        const isValidDirectory = dirent.isDirectory() && !allExcludedModules.includes(dirent.name);
        if (!isValidDirectory)
            return false;
        const moduleManifestPath = path.join(srcPath, dirent.name, `${dirent.name}.module.ts`);
        const stats = fs.statSync(moduleManifestPath, { throwIfNoEntry: false });
        return !!stats && stats.isFile();
    })
        .map(dirent => dirent.name);
    return enabledModules;
};
exports.getDynamicModuleNames = getDynamicModuleNames;
const getDynamicModuleNamesBasedOnMetadata = () => {
    const dynamicModulesToExclude = process.env.SOLID_DYNAMIC_MODULES_TO_EXCLUDE?.split(',') || [];
    const moduleMetadataPath = path.join(process.cwd(), 'module-metadata');
    const coreModuleNames = (0, exports.getCoreModuleNames)();
    const allExcludedModules = [...new Set([...coreModuleNames, ...dynamicModulesToExclude])];
    if (!fs.existsSync(moduleMetadataPath)) {
        logger.warn(`Module metadata path does not exist: ${moduleMetadataPath}`);
        return [];
    }
    const moduleMetadataDirectories = fs.readdirSync(moduleMetadataPath, { withFileTypes: true });
    const enabledModules = moduleMetadataDirectories
        .filter(dirent => {
        const isValidDirectory = dirent.isDirectory() && !allExcludedModules.includes(dirent.name);
        if (!isValidDirectory)
            return false;
        const fullPath = path.join(moduleMetadataPath, dirent.name, `${dirent.name}-metadata.json`);
        const stats = fs.statSync(fullPath, { throwIfNoEntry: false });
        return !!stats && stats.isFile();
    })
        .map(dirent => dirent.name);
    return enabledModules;
};
exports.getDynamicModuleNamesBasedOnMetadata = getDynamicModuleNamesBasedOnMetadata;
const getCoreModuleNames = () => {
    return ['solid-core'];
};
exports.getCoreModuleNames = getCoreModuleNames;
//# sourceMappingURL=module.helper.js.map