import { readdirSync, readFileSync, statSync } from 'node:fs';
import { join, relative } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';
// The cms-core main barrel (`@actuate-media/cms-core`) re-exports server-only
// code (getDB, pg, sharp, graphql, codegen). Importing a runtime *value* from
// it inside cms-admin drags that server graph into the client admin bundle —
// the bug Opal hit where Turbopack pulled `pg`/`fs` into the browser build.
//
// Type-only imports (`import type` / `export type`) are erased by tsc and are
// safe. Page-builder runtime utilities must come from the client-safe
// `@actuate-media/cms-core/page-builder` subpath instead. This test fails if
// any source file reintroduces a value import from the bare barrel.
const SRC_DIR = fileURLToPath(new URL('../', import.meta.url));
function collectSourceFiles(dir, acc = []) {
    for (const entry of readdirSync(dir)) {
        const full = join(dir, entry);
        if (statSync(full).isDirectory()) {
            if (entry === '__tests__' || entry === 'node_modules' || entry === 'dist')
                continue;
            collectSourceFiles(full, acc);
        }
        else if (/\.(ts|tsx)$/.test(entry)) {
            acc.push(full);
        }
    }
    return acc;
}
// Matches `import ... from '@actuate-media/cms-core'` and the `export ... from`
// form, capturing whether the `type` modifier follows the keyword. The import
// clause is restricted to a brace block / namespace / default binding so the
// match can't span across adjacent statements. The closing quote immediately
// after `cms-core` excludes subpath imports like `.../page-builder`.
const BARE_BARREL = /\b(import|export)\s+(type\s+)?(?:\{[^}]*\}|\*\s+as\s+[A-Za-z_$][\w$]*|[A-Za-z_$][\w$]*(?:\s*,\s*\{[^}]*\})?)\s+from\s+['"]@actuate-media\/cms-core['"]/g;
describe('cms-admin client/server bundle boundary', () => {
    it('never imports runtime values from the bare @actuate-media/cms-core barrel', () => {
        const offenders = [];
        for (const file of collectSourceFiles(SRC_DIR)) {
            const content = readFileSync(file, 'utf-8');
            for (const match of content.matchAll(BARE_BARREL)) {
                const isTypeOnly = Boolean(match[2]);
                if (!isTypeOnly) {
                    offenders.push(`${relative(SRC_DIR, file)}: ${match[0].replace(/\s+/g, ' ').trim()}`);
                }
            }
        }
        expect(offenders, `Use '@actuate-media/cms-core/page-builder' (or 'import type') instead:\n${offenders.join('\n')}`).toEqual([]);
    });
});
//# sourceMappingURL=no-server-barrel-imports.test.js.map