import { t as sameFileIdentity } from "./file-identity-OkAKtUrG.js";
import { i as openBoundaryFileSync } from "./boundary-file-read-6rU9DotD.js";
import { n as resolveBundledPluginsDir } from "./bundled-dir-bm4_3ZBE.js";
import { i as resolveBundledPluginPublicSurfacePath } from "./public-surface-runtime-CmuW440e.js";
import { o as getCachedPluginJitiLoader } from "./bundled-plugin-metadata-Dp24VtU2.js";
import { d as resolvePluginLoaderJitiTryNative, i as isBundledPluginExtensionPath, l as resolveLoaderPackageRoot } from "./sdk-alias-NX2hE68D.js";
import { t as prepareBuiltBundledPluginPublicSurfaceLocation } from "./bundled-public-surface-runtime-root-CJia1OB6.js";
import { createRequire } from "node:module";
import { fileURLToPath } from "node:url";
import fs from "node:fs";
import path from "node:path";
//#region src/plugins/public-surface-loader.ts
const OPENCLAW_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: OPENCLAW_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) : OPENCLAW_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 sharedLoader = getSharedBundledPublicSurfaceJiti(modulePath, resolvePluginLoaderJitiTryNative(modulePath, { preferBuiltDist: true }));
	if (sharedLoader) return sharedLoader;
	return getCachedPluginJitiLoader({
		cache: jitiLoaders,
		modulePath,
		importerUrl: import.meta.url,
		preferBuiltDist: true,
		jitiFilename: import.meta.url
	});
}
function loadPublicSurfaceModule(modulePath) {
	if (canUseSourceArtifactRequire({
		modulePath,
		tryNative: resolvePluginLoaderJitiTryNative(modulePath, { preferBuiltDist: true })
	})) return sourceArtifactRequire(modulePath);
	return getJiti(modulePath)(modulePath);
}
function getSharedBundledPublicSurfaceJiti(modulePath, tryNative) {
	const bundledPluginsDir = resolveBundledPluginsDir();
	if (!isBundledPluginExtensionPath({
		modulePath,
		openClawPackageRoot: OPENCLAW_PACKAGE_ROOT,
		...bundledPluginsDir ? { bundledPluginsDir } : {}
	})) return null;
	const cacheKey = tryNative ? "bundled:native" : "bundled:source";
	return getCachedPluginJitiLoader({
		cache: sharedBundledPublicSurfaceJitiLoaders,
		modulePath,
		importerUrl: import.meta.url,
		jitiFilename: import.meta.url,
		cacheScopeKey: cacheKey,
		tryNative
	});
}
function loadBundledPluginPublicArtifactModuleSync(params) {
	const location = resolvePublicSurfaceLocation(params);
	if (!location) throw new Error(`Unable to resolve bundled plugin public surface ${params.dirName}/${params.artifactBasename}`);
	const preparedLocation = prepareBuiltBundledPluginPublicSurfaceLocation({
		location,
		pluginId: params.dirName,
		installRuntimeDeps: params.installRuntimeDeps
	});
	const cached = loadedPublicSurfaceModules.get(location.modulePath) ?? loadedPublicSurfaceModules.get(preparedLocation.modulePath);
	if (cached) return cached;
	const opened = openBoundaryFileSync({
		absolutePath: preparedLocation.modulePath,
		rootPath: preparedLocation.boundaryRoot,
		boundaryLabel: preparedLocation.boundaryRoot === OPENCLAW_PACKAGE_ROOT ? "OpenClaw package root" : "plugin root",
		rejectHardlinks: true
	});
	if (!opened.ok) throw new Error(`Unable to open bundled plugin public surface ${params.dirName}/${params.artifactBasename}`, { cause: opened.error });
	const validatedPath = opened.path;
	const validatedStat = opened.stat;
	fs.closeSync(opened.fd);
	if (!sameFileIdentity(validatedStat, fs.statSync(validatedPath))) throw new Error(`Bundled plugin public surface changed after validation: ${params.dirName}/${params.artifactBasename}`);
	const sentinel = {};
	loadedPublicSurfaceModules.set(location.modulePath, sentinel);
	loadedPublicSurfaceModules.set(preparedLocation.modulePath, sentinel);
	loadedPublicSurfaceModules.set(validatedPath, sentinel);
	try {
		const loaded = loadPublicSurfaceModule(validatedPath);
		Object.assign(sentinel, loaded);
		return sentinel;
	} catch (error) {
		loadedPublicSurfaceModules.delete(location.modulePath);
		loadedPublicSurfaceModules.delete(preparedLocation.modulePath);
		loadedPublicSurfaceModules.delete(validatedPath);
		throw error;
	}
}
//#endregion
export { loadBundledPluginPublicArtifactModuleSync as t };
