/**
 * Server-side auto-wiring for the `platform.storage` shorthand.
 *
 * `actuate.config.ts` is imported by the admin client bundle, so it must stay
 * free of platform adapters (they pull in `node:*` and `@vercel/blob`, which
 * break the client build). The shorthand `{ provider: 'vercel-blob' }` is a
 * plain, client-safe object — but on its own it registers nothing, so uploads
 * fail.
 *
 * This bridges that gap: when the API handler boots (server-only) and sees a
 * remote `provider` shorthand with no adapter registered, it dynamically imports
 * the matching platform package and registers its storage adapter. The import
 * specifier is computed (never a literal) so bundlers don't try to statically
 * resolve an optional peer that may not be installed; a missing/failed package
 * just leaves no adapter and the caller emits the existing guidance warning.
 */
import { getStorageAdapter, setStorageAdapter } from './index.js';
/** Shorthand `provider` → platform package + adapter factory name. */
const PROVIDER_PACKAGES = {
    'vercel-blob': { spec: '@actuate-media/platform-vercel', factory: 'vercel' },
    s3: { spec: '@actuate-media/platform-aws', factory: 'aws' },
};
const defaultImporter = (specifier) => import(/* webpackIgnore: true */ /* @vite-ignore */ specifier);
/** True when `provider` is a remote shorthand we know how to auto-wire. */
export function isAutoWireableStorageProvider(provider) {
    return typeof provider === 'string' && provider in PROVIDER_PACKAGES;
}
/**
 * Attempt to register a storage adapter for a remote `provider` shorthand.
 * Returns `true` when an adapter is (or was already) registered, `false` when
 * the provider is unknown/`local` or the platform package can't be loaded. Never
 * throws — storage wiring must not take down the API.
 *
 * `importer` is injectable for tests; production uses a computed dynamic import.
 */
export async function autoWireStorageAdapter(provider, importer = defaultImporter) {
    if (!provider || provider === 'local')
        return false;
    if (getStorageAdapter())
        return true;
    const entry = PROVIDER_PACKAGES[provider];
    if (!entry)
        return false;
    try {
        const mod = await importer(entry.spec);
        const factory = mod?.[entry.factory];
        const storage = typeof factory === 'function' ? factory()?.storage : undefined;
        if (storage && typeof storage.upload === 'function') {
            // Another caller may have registered one while we awaited the import.
            if (!getStorageAdapter())
                setStorageAdapter(storage);
            return true;
        }
    }
    catch {
        /* optional peer not installed / failed to load — caller warns */
    }
    return false;
}
//# sourceMappingURL=auto-wire.js.map