import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { createRequire } from 'module';

try {
    const url = import.meta?.url;
    // Hardcode fallback path for Directus Docker environment
    const fallbackPath = '/directus/extensions/acquisition/dist/index.js';
    const filename = url ? fileURLToPath(url) : fallbackPath;
    const dir = dirname(filename);

    // @ts-ignore
    globalThis.__filename = filename;
    // @ts-ignore
    globalThis.__dirname = dir;
    // @ts-ignore
    globalThis.require = createRequire(url || `file://${fallbackPath}`);

    console.log(`[Shim] Loaded. __dirname: ${dir}`);
} catch (e) {
    console.warn("[Shim] Failed to shim __dirname/require", e);
}
