import { optionalString, requireString } from '../args.js';
import { createContext } from '../context.js';
import { CliError } from '../errors.js';
import { writeOutput } from '../output.js';
import { withMeta } from './_meta.js';
export function runSectionRef(args) {
    const anchor = requireString(args, 'anchor');
    const relation = optionalString(args, 'relation');
    const ctx = createContext(args);
    try {
        const section = ctx.reader.getSection(anchor);
        if (!section)
            throw new CliError('SECTION_NOT_FOUND', `section anchor '${anchor}'`);
        const result = ctx.registry.serializeSection('single_element', section, ctx.reader);
        const payload = withMeta(result);
        const enriched = typeof payload === 'object' && payload !== null && relation
            ? { ...payload, relation }
            : payload;
        writeOutput(enriched, args);
    }
    finally {
        ctx.close();
    }
}
//# sourceMappingURL=section-ref.js.map