'use strict';
import { ensureWithIndexer } from '../../../../fd-toolbox/types/ensure-type.js';
import { relative, join } from 'path';
import { pathToFileURL } from 'url';
import { folders } from '../../../../base-repo/app-constants/project-paths.js';
import { tryGetFromMemoryCache, setMemoryCache, memoryCacheHasKey, removeFromMemoryCache } from '../../../../fd-toolbox/server/cachings/fd-memory-cache.js';
import { normalizePath } from '../../../../fd-toolbox/path.js';
import { getHandlerMap, getGlobalHandlerMap } from './handler-map.js';
import { fileSuffixes } from '../../../../base-repo/constants/internal-extensions.js';
import { ensurePathSeparatorAtTheStart } from '../../../../fd-toolbox/paths/ensure.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
const resourceFileEnding = fileSuffixes.resource;
const resourceExtFileEnding = fileSuffixes.extendedResource;
async function dynamicImportResourceHandlers(relativePath) {
  const moduleFromCache = await tryGetFromMemoryCache(relativePath);
  let result = ensureWithIndexer({});
  const resourceHandlerDynamicImportMap = getHandlerMap();
  const importFn = resourceHandlerDynamicImportMap[relativePath];
  if (moduleFromCache) {
    result = ensureWithIndexer(moduleFromCache);
  } else if (typeof importFn === "function") {
    const importedModule = ensureWithIndexer(await importFn());
    setMemoryCache(relativePath, importedModule);
    result = importedModule;
  }
  return result;
}
__name(dynamicImportResourceHandlers, "dynamicImportResourceHandlers");
async function dynamicImportResource(relativePath) {
  return dynamicImport(relativePath, resourceFileEnding);
}
__name(dynamicImportResource, "dynamicImportResource");
async function dynamicImportResourceExt(relativePath) {
  const resourceExt = import(buildImportPath(relativePath, resourceExtFileEnding));
  return resourceExt;
}
__name(dynamicImportResourceExt, "dynamicImportResourceExt");
function formatRelativePathForImport(absolutePath) {
  const relativePath = normalizePath(relative(getProjectRootPath(), absolutePath));
  const dotCount = relativePath.match(/\./g)?.length ?? 0;
  if (dotCount >= 2) return relativePath.slice(0, relativePath.lastIndexOf(".", relativePath.lastIndexOf(".") - 1));
  return relativePath;
}
__name(formatRelativePathForImport, "formatRelativePathForImport");
async function removeDynamicModuleCache(relativePath) {
  if (memoryCacheHasKey(relativePath)) {
    removeFromMemoryCache(relativePath);
  }
}
__name(removeDynamicModuleCache, "removeDynamicModuleCache");
function removeDynamicModulesFromCoreCache() {
  const resourceHandlerDynamicImportMap = getGlobalHandlerMap();
  if (resourceHandlerDynamicImportMap) {
    const dynamicModulePaths = Object.keys(resourceHandlerDynamicImportMap);
    for (const path of dynamicModulePaths) {
      removeDynamicModuleCache(path);
    }
  }
}
__name(removeDynamicModulesFromCoreCache, "removeDynamicModulesFromCoreCache");
async function dynamicImport(relativePath, fileEnding) {
  const importedModule = import(buildImportPath(relativePath, fileEnding));
  return importedModule;
}
__name(dynamicImport, "dynamicImport");
function buildImportPath(relativePath, fileEnding) {
  const absolutePath = join(getProjectRootPath(), `${relativePath}${fileEnding}`);
  return pathToFileURL(absolutePath).href;
}
__name(buildImportPath, "buildImportPath");
function getProjectRootPath() {
  const projectRootPath = normalizePath(process.cwd().replace(folders.dist, folders.packages));
  const packagesFolderIndex = projectRootPath.lastIndexOf(folders.packages);
  let result = projectRootPath;
  if (packagesFolderIndex >= 0) {
    result = projectRootPath.slice(0, packagesFolderIndex);
  } else if (projectRootPath.endsWith(ensurePathSeparatorAtTheStart(folders.packages))) {
    result = projectRootPath.slice(0, -(folders.packages.length + 1));
  }
  return result;
}
__name(getProjectRootPath, "getProjectRootPath");

export { dynamicImportResource, dynamicImportResourceExt, dynamicImportResourceHandlers, formatRelativePathForImport, removeDynamicModuleCache, removeDynamicModulesFromCoreCache };
