import { i as openBoundaryFileSync } from "./boundary-file-read-yTFhgtfy.js";
import { t as resolveBundledPluginsDir } from "./bundled-dir-B6aeFduu.js";
import { i as isBundledPluginExtensionPath, l as resolveLoaderPackageRoot, n as buildPluginLoaderJitiOptions, t as buildPluginLoaderAliasMap, u as resolvePluginLoaderJitiConfig } from "./sdk-alias-Czt6OgXf.js";
import { n as resolveBundledPluginPublicSurfacePath } from "./public-surface-runtime-D0G15uBG.js";
import { createRequire } from "node:module";
import { fileURLToPath } from "node:url";
import fs from "node:fs";
import path from "node:path";
import { createJiti } from "jiti";
//#region src/plugins/public-surface-loader.ts
const KAIJIBOT_PACKAGE_ROOT = resolveLoaderPackageRoot({
	modulePath: fileURLToPath(import.meta.url),
	moduleUrl: import.meta.url
}) ?? fileURLToPath(new URL("../..", import.meta.url));
const loadedPublicSurfaceModules = /* @__PURE__ */ new Map();
const sourceArtifactRequire = createRequire(import.meta.url);
const publicSurfaceLocations = /* @__PURE__ */ new Map();
const jitiLoaders = /* @__PURE__ */ new Map();
const sharedBundledPublicSurfaceJitiLoaders = /* @__PURE__ */ new Map();
function isSourceArtifactPath(modulePath) {
	switch (path.extname(modulePath).toLowerCase()) {
		case ".ts":
		case ".tsx":
		case ".mts":
		case ".cts":
		case ".mtsx":
		case ".ctsx": return true;
		default: return false;
	}
}
function canUseSourceArtifactRequire(params) {
	return !params.tryNative && isSourceArtifactPath(params.modulePath) && typeof sourceArtifactRequire.extensions?.[".ts"] === "function";
}
function createResolutionKey(params) {
	const bundledPluginsDir = resolveBundledPluginsDir();
	return `${params.dirName}::${params.artifactBasename}::${bundledPluginsDir ? path.resolve(bundledPluginsDir) : "<default>"}`;
}
function resolvePublicSurfaceLocationUncached(params) {
	const bundledPluginsDir = resolveBundledPluginsDir();
	const modulePath = resolveBundledPluginPublicSurfacePath({
		rootDir: KAIJIBOT_PACKAGE_ROOT,
		...bundledPluginsDir ? { bundledPluginsDir } : {},
		dirName: params.dirName,
		artifactBasename: params.artifactBasename
	});
	if (!modulePath) return null;
	return {
		modulePath,
		boundaryRoot: bundledPluginsDir && modulePath.startsWith(path.resolve(bundledPluginsDir) + path.sep) ? path.resolve(bundledPluginsDir) : KAIJIBOT_PACKAGE_ROOT
	};
}
function resolvePublicSurfaceLocation(params) {
	const key = createResolutionKey(params);
	if (publicSurfaceLocations.has(key)) return publicSurfaceLocations.get(key) ?? null;
	const resolved = resolvePublicSurfaceLocationUncached(params);
	publicSurfaceLocations.set(key, resolved);
	return resolved;
}
function getJiti(modulePath) {
	const { tryNative, aliasMap, cacheKey } = resolvePluginLoaderJitiConfig({
		modulePath,
		argv1: process.argv[1],
		moduleUrl: import.meta.url,
		preferBuiltDist: true
	});
	const sharedLoader = getSharedBundledPublicSurfaceJiti(modulePath, tryNative);
	if (sharedLoader) return sharedLoader;
	const cached = jitiLoaders.get(cacheKey);
	if (cached) return cached;
	const loader = createJiti(import.meta.url, {
		...buildPluginLoaderJitiOptions(aliasMap),
		tryNative
	});
	jitiLoaders.set(cacheKey, loader);
	return loader;
}
function loadPublicSurfaceModule(modulePath) {
	const { tryNative } = resolvePluginLoaderJitiConfig({
		modulePath,
		argv1: process.argv[1],
		moduleUrl: import.meta.url,
		preferBuiltDist: true
	});
	if (canUseSourceArtifactRequire({
		modulePath,
		tryNative
	})) return sourceArtifactRequire(modulePath);
	return getJiti(modulePath)(modulePath);
}
function getSharedBundledPublicSurfaceJiti(modulePath, tryNative) {
	const bundledPluginsDir = resolveBundledPluginsDir();
	if (!isBundledPluginExtensionPath({
		modulePath,
		kaijiBotPackageRoot: KAIJIBOT_PACKAGE_ROOT,
		...bundledPluginsDir ? { bundledPluginsDir } : {}
	})) return null;
	const cacheKey = tryNative ? "bundled:native" : "bundled:source";
	const cached = sharedBundledPublicSurfaceJitiLoaders.get(cacheKey);
	if (cached) return cached;
	const aliasMap = buildPluginLoaderAliasMap(modulePath, process.argv[1], import.meta.url);
	const loader = createJiti(import.meta.url, {
		...buildPluginLoaderJitiOptions(aliasMap),
		tryNative
	});
	sharedBundledPublicSurfaceJitiLoaders.set(cacheKey, loader);
	return loader;
}
function loadBundledPluginPublicArtifactModuleSync(params) {
	const location = resolvePublicSurfaceLocation(params);
	if (!location) throw new Error(`Unable to resolve bundled plugin public surface ${params.dirName}/${params.artifactBasename}`);
	const cached = loadedPublicSurfaceModules.get(location.modulePath);
	if (cached) return cached;
	const opened = openBoundaryFileSync({
		absolutePath: location.modulePath,
		rootPath: location.boundaryRoot,
		boundaryLabel: location.boundaryRoot === KAIJIBOT_PACKAGE_ROOT ? "KaijiBot package root" : "bundled plugin directory",
		rejectHardlinks: false
	});
	if (!opened.ok) throw new Error(`Unable to open bundled plugin public surface ${params.dirName}/${params.artifactBasename}`, { cause: opened.error });
	fs.closeSync(opened.fd);
	const sentinel = {};
	loadedPublicSurfaceModules.set(location.modulePath, sentinel);
	try {
		const loaded = loadPublicSurfaceModule(location.modulePath);
		Object.assign(sentinel, loaded);
		return sentinel;
	} catch (error) {
		loadedPublicSurfaceModules.delete(location.modulePath);
		throw error;
	}
}
//#endregion
export { loadBundledPluginPublicArtifactModuleSync as t };
