//#region src/plugin-sdk/provider-web-search-config-contract.ts
function ensureObject(target, key) {
	const current = target[key];
	if (current && typeof current === "object" && !Array.isArray(current)) return current;
	const next = {};
	target[key] = next;
	return next;
}
function getTopLevelCredentialValue(searchConfig) {
	return searchConfig?.apiKey;
}
function setTopLevelCredentialValue(searchConfigTarget, value) {
	searchConfigTarget.apiKey = value;
}
function getScopedCredentialValue(searchConfig, key) {
	const scoped = searchConfig?.[key];
	if (!scoped || typeof scoped !== "object" || Array.isArray(scoped)) return;
	return scoped.apiKey;
}
function setScopedCredentialValue(searchConfigTarget, key, value) {
	const scoped = searchConfigTarget[key];
	if (!scoped || typeof scoped !== "object" || Array.isArray(scoped)) {
		searchConfigTarget[key] = { apiKey: value };
		return;
	}
	scoped.apiKey = value;
}
function mergeScopedSearchConfig(searchConfig, key, pluginConfig, options) {
	if (!pluginConfig) return searchConfig;
	const currentScoped = searchConfig?.[key] && typeof searchConfig[key] === "object" && !Array.isArray(searchConfig[key]) ? searchConfig[key] : {};
	const next = {
		...searchConfig,
		[key]: {
			...currentScoped,
			...pluginConfig
		}
	};
	if (options?.mirrorApiKeyToTopLevel && pluginConfig.apiKey !== void 0) next.apiKey = pluginConfig.apiKey;
	return next;
}
function resolveProviderWebSearchPluginConfig(config, pluginId) {
	const entry = config?.plugins?.entries?.[pluginId];
	if (!entry || typeof entry !== "object" || Array.isArray(entry)) return;
	const pluginConfig = "config" in entry ? entry.config : void 0;
	if (!pluginConfig || typeof pluginConfig !== "object" || Array.isArray(pluginConfig)) return;
	const webSearch = pluginConfig.webSearch;
	if (!webSearch || typeof webSearch !== "object" || Array.isArray(webSearch)) return;
	return webSearch;
}
function setProviderWebSearchPluginConfigValue(configTarget, pluginId, key, value) {
	const entry = ensureObject(ensureObject(ensureObject(configTarget, "plugins"), "entries"), pluginId);
	if (entry.enabled === void 0) entry.enabled = true;
	const webSearch = ensureObject(ensureObject(entry, "config"), "webSearch");
	webSearch[key] = value;
}
function createSearchCredentialFields(credential) {
	switch (credential.type) {
		case "scoped": return {
			getCredentialValue: (searchConfig) => getScopedCredentialValue(searchConfig, credential.scopeId),
			setCredentialValue: (value, searchConfig) => {
				if (!searchConfig) return;
				setScopedCredentialValue(searchConfig, credential.scopeId, value);
			}
		};
		case "top-level": return {
			getCredentialValue: getTopLevelCredentialValue,
			setCredentialValue: (value, searchConfig) => {
				if (!searchConfig) return;
				setTopLevelCredentialValue(searchConfig, value);
			}
		};
		case "none": return {
			getCredentialValue: () => void 0,
			setCredentialValue: () => {}
		};
	}
}
function createConfiguredCredentialFields(configuredCredential) {
	if (!configuredCredential) return null;
	const field = configuredCredential.field ?? "apiKey";
	return {
		getConfiguredCredentialValue: (config) => resolveProviderWebSearchPluginConfig(config, configuredCredential.pluginId)?.[field],
		setConfiguredCredentialValue: (configTarget, value) => {
			setProviderWebSearchPluginConfigValue(configTarget, configuredCredential.pluginId, field, value);
		}
	};
}
function createWebSearchProviderContractFields(options) {
	const configuredCredentialFields = createConfiguredCredentialFields(options.configuredCredential);
	return {
		inactiveSecretPaths: options.inactiveSecretPaths ?? (options.credentialPath ? [options.credentialPath] : []),
		...createSearchCredentialFields(options.searchCredential),
		...configuredCredentialFields
	};
}

//#endregion
export { createWebSearchProviderContractFields, getScopedCredentialValue, getTopLevelCredentialValue, mergeScopedSearchConfig, resolveProviderWebSearchPluginConfig, setProviderWebSearchPluginConfigValue, setScopedCredentialValue, setTopLevelCredentialValue };