import { isUndefinedOrNull } from '../../helper/index.js';
import { Logger } from '../../config/logger.js';
const MODULE_ID = '@zintrust/cloudflare-kv-proxy';
let cached = null;
const load = async () => {
    if (cached !== null)
        return cached;
    try {
        // Non-literal specifier to avoid tsconfig path alias rewriting in dist builds.
        cached = (await import(MODULE_ID));
        return cached;
    }
    catch (error) {
        Logger.error(`Optional dependency not installed: ${MODULE_ID}. Install it to use ZintrustKvProxy.`, { error: error instanceof Error ? error.message : String(error) });
    }
    return undefined;
};
export const ZintrustKvProxy = new Proxy({}, {
    get(_target, prop) {
        if (prop === Symbol.toStringTag)
            return 'ZintrustKvProxy';
        return async (...args) => {
            const mod = await load();
            if (isUndefinedOrNull(mod) || typeof mod !== 'object')
                return undefined;
            const target = mod.ZintrustKvProxy ?? mod.default;
            if (!target || typeof target !== 'object') {
                Logger.error(`Invalid module export from ${MODULE_ID}: missing ZintrustKvProxy`);
                return undefined;
            }
            const value = target[prop];
            if (typeof value !== 'function')
                return value;
            return value(...args);
        };
    },
});
export default ZintrustKvProxy;
