import { i as openBoundaryFileSync } from "./boundary-file-read-yTFhgtfy.js";
import { t as resolveBundledPluginsDir } from "./bundled-dir-B6aeFduu.js";
import { l as resolveLoaderPackageRoot, n as buildPluginLoaderJitiOptions, u as resolvePluginLoaderJitiConfig } from "./sdk-alias-Czt6OgXf.js";
import { n as resolveBundledPluginPublicSurfacePath, t as normalizeBundledPluginArtifactSubpath } 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";
//#region src/plugin-sdk/facade-loader.ts
const CURRENT_MODULE_PATH = fileURLToPath(import.meta.url);
const PUBLIC_SURFACE_SOURCE_EXTENSIONS = [
	".ts",
	".mts",
	".js",
	".mjs",
	".cts",
	".cjs"
];
const nodeRequire = createRequire(import.meta.url);
const jitiLoaders = /* @__PURE__ */ new Map();
const loadedFacadeModules = /* @__PURE__ */ new Map();
const loadedFacadePluginIds = /* @__PURE__ */ new Set();
const cachedFacadeModuleLocationsByKey = /* @__PURE__ */ new Map();
let facadeLoaderJitiFactory;
let cachedKaijiBotPackageRoot;
function getJitiFactory() {
	if (facadeLoaderJitiFactory) return facadeLoaderJitiFactory;
	const { createJiti } = nodeRequire("jiti");
	facadeLoaderJitiFactory = createJiti;
	return facadeLoaderJitiFactory;
}
function getKaijiBotPackageRoot() {
	if (cachedKaijiBotPackageRoot) return cachedKaijiBotPackageRoot;
	cachedKaijiBotPackageRoot = resolveLoaderPackageRoot({
		modulePath: fileURLToPath(import.meta.url),
		moduleUrl: import.meta.url
	}) ?? fileURLToPath(new URL("../..", import.meta.url));
	return cachedKaijiBotPackageRoot;
}
function createFacadeResolutionKey(params) {
	const bundledPluginsDir = resolveBundledPluginsDir();
	return `${params.dirName}::${params.artifactBasename}::${bundledPluginsDir ? path.resolve(bundledPluginsDir) : "<default>"}`;
}
function resolveSourceFirstPublicSurfacePath(params) {
	const sourceBaseName = normalizeBundledPluginArtifactSubpath(params.artifactBasename).replace(/\.js$/u, "");
	const sourceRoot = params.bundledPluginsDir ?? path.resolve(getKaijiBotPackageRoot(), "extensions");
	for (const ext of PUBLIC_SURFACE_SOURCE_EXTENSIONS) {
		const candidate = path.resolve(sourceRoot, params.dirName, `${sourceBaseName}${ext}`);
		if (fs.existsSync(candidate)) return candidate;
	}
	return null;
}
function resolveFacadeModuleLocationUncached(params) {
	const bundledPluginsDir = resolveBundledPluginsDir();
	if (!CURRENT_MODULE_PATH.includes(`${path.sep}dist${path.sep}`)) {
		const modulePath = resolveSourceFirstPublicSurfacePath({
			...params,
			...bundledPluginsDir ? { bundledPluginsDir } : {}
		}) ?? resolveSourceFirstPublicSurfacePath(params) ?? resolveBundledPluginPublicSurfacePath({
			rootDir: getKaijiBotPackageRoot(),
			...bundledPluginsDir ? { bundledPluginsDir } : {},
			dirName: params.dirName,
			artifactBasename: params.artifactBasename
		});
		if (modulePath) return {
			modulePath,
			boundaryRoot: bundledPluginsDir && modulePath.startsWith(path.resolve(bundledPluginsDir) + path.sep) ? path.resolve(bundledPluginsDir) : getKaijiBotPackageRoot()
		};
		return null;
	}
	const modulePath = resolveBundledPluginPublicSurfacePath({
		rootDir: getKaijiBotPackageRoot(),
		...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) : getKaijiBotPackageRoot()
	};
}
function resolveFacadeModuleLocation(params) {
	const key = createFacadeResolutionKey(params);
	if (cachedFacadeModuleLocationsByKey.has(key)) return cachedFacadeModuleLocationsByKey.get(key) ?? null;
	const resolved = resolveFacadeModuleLocationUncached(params);
	cachedFacadeModuleLocationsByKey.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 cached = jitiLoaders.get(cacheKey);
	if (cached) return cached;
	const loader = getJitiFactory()(import.meta.url, {
		...buildPluginLoaderJitiOptions(aliasMap),
		tryNative
	});
	jitiLoaders.set(cacheKey, loader);
	return loader;
}
function createLazyFacadeValueLoader(load) {
	let loaded = false;
	let value;
	return () => {
		if (!loaded) {
			value = load();
			loaded = true;
		}
		return value;
	};
}
function createLazyFacadeProxyValue(params) {
	const resolve = createLazyFacadeValueLoader(params.load);
	return new Proxy(params.target, {
		defineProperty(_target, property, descriptor) {
			return Reflect.defineProperty(resolve(), property, descriptor);
		},
		deleteProperty(_target, property) {
			return Reflect.deleteProperty(resolve(), property);
		},
		get(_target, property, receiver) {
			return Reflect.get(resolve(), property, receiver);
		},
		getOwnPropertyDescriptor(_target, property) {
			return Reflect.getOwnPropertyDescriptor(resolve(), property);
		},
		getPrototypeOf() {
			return Reflect.getPrototypeOf(resolve());
		},
		has(_target, property) {
			return Reflect.has(resolve(), property);
		},
		isExtensible() {
			return Reflect.isExtensible(resolve());
		},
		ownKeys() {
			return Reflect.ownKeys(resolve());
		},
		preventExtensions() {
			return Reflect.preventExtensions(resolve());
		},
		set(_target, property, value, receiver) {
			return Reflect.set(resolve(), property, value, receiver);
		},
		setPrototypeOf(_target, prototype) {
			return Reflect.setPrototypeOf(resolve(), prototype);
		}
	});
}
function createLazyFacadeObjectValue(load) {
	return createLazyFacadeProxyValue({
		load,
		target: {}
	});
}
function createLazyFacadeArrayValue(load) {
	return createLazyFacadeProxyValue({
		load,
		target: []
	});
}
function loadFacadeModuleAtLocationSync(params) {
	const cached = loadedFacadeModules.get(params.location.modulePath);
	if (cached) return cached;
	const opened = openBoundaryFileSync({
		absolutePath: params.location.modulePath,
		rootPath: params.location.boundaryRoot,
		boundaryLabel: params.location.boundaryRoot === getKaijiBotPackageRoot() ? "KaijiBot package root" : (() => {
			const bundledDir = resolveBundledPluginsDir();
			return bundledDir && path.resolve(params.location.boundaryRoot) === path.resolve(bundledDir) ? "bundled plugin directory" : "plugin root";
		})(),
		rejectHardlinks: false
	});
	if (!opened.ok) throw new Error(`Unable to open bundled plugin public surface ${params.location.modulePath}`, { cause: opened.error });
	fs.closeSync(opened.fd);
	const sentinel = {};
	loadedFacadeModules.set(params.location.modulePath, sentinel);
	let loaded;
	try {
		loaded = params.loadModule?.(params.location.modulePath) ?? getJiti(params.location.modulePath)(params.location.modulePath);
		Object.assign(sentinel, loaded);
		loadedFacadePluginIds.add(typeof params.trackedPluginId === "function" ? params.trackedPluginId() : params.trackedPluginId);
	} catch (err) {
		loadedFacadeModules.delete(params.location.modulePath);
		throw err;
	}
	return sentinel;
}
function loadBundledPluginPublicSurfaceModuleSync(params) {
	const location = resolveFacadeModuleLocation(params);
	if (!location) throw new Error(`Unable to resolve bundled plugin public surface ${params.dirName}/${params.artifactBasename}`);
	return loadFacadeModuleAtLocationSync({
		location,
		trackedPluginId: params.trackedPluginId ?? params.dirName
	});
}
function listImportedBundledPluginFacadeIds() {
	return [...loadedFacadePluginIds].toSorted((left, right) => left.localeCompare(right));
}
function resetFacadeLoaderStateForTest() {
	loadedFacadeModules.clear();
	loadedFacadePluginIds.clear();
	jitiLoaders.clear();
	cachedFacadeModuleLocationsByKey.clear();
	facadeLoaderJitiFactory = void 0;
	cachedKaijiBotPackageRoot = void 0;
}
//#endregion
export { loadFacadeModuleAtLocationSync as a, loadBundledPluginPublicSurfaceModuleSync as i, createLazyFacadeObjectValue as n, resetFacadeLoaderStateForTest as o, listImportedBundledPluginFacadeIds as r, createLazyFacadeArrayValue as t };
