// Brand-foreign premium-bundle filter for the boot-time agents/ walk.
//
// The manager walks `<premium-plugins>/<bundle>/agents/` for every bundle
// present on disk. On a brand whose installer ships every premium bundle but
// only intends to USE a subset, foreign-bundle specialist files (e.g.
// real-agent's listing-curator on a maxy-code install) reach the drift
// assertion and refuse boot the moment any of their declared tools is not in
// the active brand's tool surface.
//
// The fix: brand.json#shipsPremiumBundles is the same field paths.ts already
// resolves for the UI. Three modes, matching the canonical resolver:
//
//   true         → 'all'       — ship every premium bundle on disk
//   string[]     → 'allowlist' — ship only the named bundle directories
//   false/absent → 'none'      — ship no premium bundles
//
// Premium bundles outside the resolved mode are dropped at agents/ walk time
// with one `agents-dir-skipped path=<p> reason=brand-foreign-bundle bundle=<n>`
// line per drop. `platform/plugins/` candidates are never filtered.
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
/** Read brand.json and resolve `shipsPremiumBundles` into one of the three
 *  modes paths.ts already enforces for the UI. Missing file, malformed JSON,
 *  or missing field defaults to `'none'` (fail-closed). */
export function readBrandShipsPremiumBundles(platformRoot, logger) {
    const path = join(platformRoot, 'config', 'brand.json');
    let raw;
    try {
        raw = readFileSync(path, 'utf-8');
    }
    catch {
        logger(`brand-config-missing path=${path} ships-premium-bundles=none`);
        return { mode: 'none' };
    }
    let parsed;
    try {
        parsed = JSON.parse(raw);
    }
    catch (err) {
        logger(`brand-config-malformed path=${path} err=${err instanceof Error ? err.message : String(err)} ships-premium-bundles=none`);
        return { mode: 'none' };
    }
    if (!parsed || typeof parsed !== 'object')
        return { mode: 'none' };
    const v = parsed.shipsPremiumBundles;
    if (v === true) {
        logger(`brand-config-loaded path=${path} ships-premium-bundles=all`);
        return { mode: 'all' };
    }
    if (Array.isArray(v)) {
        const allow = new Set();
        for (const x of v) {
            if (typeof x === 'string' && x.length > 0)
                allow.add(x);
        }
        logger(`brand-config-loaded path=${path} ships-premium-bundles=${allow.size === 0 ? 'none' : Array.from(allow).sort().join(',')}`);
        return { mode: 'allowlist', allow };
    }
    // false, null, missing — fail-closed: no premium bundles.
    return { mode: 'none' };
}
/** Filter candidate agents/ dirs against the resolved premium-bundle mode.
 *  Non-premium candidates (platform/plugins/) are always kept. Premium
 *  candidates are kept if mode is 'all', or if mode is 'allowlist' and the
 *  bundle name is in the allow set; otherwise dropped. */
export function filterBrandForeignAgentDirs(candidates, premiumMode) {
    const kept = [];
    const skipped = [];
    for (const c of candidates) {
        if (!c.premium) {
            kept.push(c);
            continue;
        }
        if (premiumMode.mode === 'all') {
            kept.push(c);
        }
        else if (premiumMode.mode === 'allowlist' && premiumMode.allow.has(c.bundle)) {
            kept.push(c);
        }
        else {
            skipped.push(c);
        }
    }
    return { kept, skipped };
}
//# sourceMappingURL=brand-foreign-filter.js.map