/**
 * Section runtime engine resolver (M7).
 *
 * Canonical home of `resolveRuntime`, `resolveToolsConfig`, and their
 * supporting helpers/types. As of M7 PR 7 the previous duplicates in
 * `packages/section-player/src/components/shared/section-player-runtime.ts`
 * have been deleted; section-player now consumes these helpers via
 * `@pie-players/pie-assessment-toolkit/runtime/internal`.
 *
 * What is NOT absorbed in this module:
 * - `resolvePlayerRuntime` stays in section-player because it depends on
 *   `DEFAULT_PLAYER_DEFINITIONS` (which side-effect-imports the
 *   item-player package). The toolkit core stays free of that
 *   dependency by exposing a parametrized orchestrator —
 *   `resolveSectionEngineRuntimeState` — that takes a `resolvePlayerRuntime`
 *   callable. The section-player wrapper
 *   (`resolveSectionPlayerRuntimeState` in
 *   `packages/section-player/src/components/shared/section-player-host-runtime.ts`)
 *   hands its local implementation in.
 *
 * Runtime-owned configuration flows through `runtime.<key>`. Layout-only
 * inputs stay on the section-player host elements.
 */
export const DEFAULT_ASSESSMENT_ID = "section-demo-direct";
export const DEFAULT_PLAYER_TYPE = "iife";
export const DEFAULT_LAZY_INIT = true;
export const DEFAULT_ISOLATION = "inherit";
export const DEFAULT_ENV = { mode: "gather", role: "student" };
/**
 * Pick the runtime-tier value when defined, otherwise the prop/attribute
 * value. The strict mirror rule means every tier-1 surface is resolved
 * with this single helper; per-feature special-casing is forbidden.
 */
function pick(runtimeVal, attrVal) {
    return runtimeVal !== undefined ? runtimeVal : attrVal;
}
/**
 * Resolve the canonical `onFrameworkError` handler from the two-tier
 * surface. Precedence (highest first): `runtime.onFrameworkError`,
 * then top-level `onFrameworkError`. Layout CEs and the kernel call
 * this so every entry point converges on the same handler.
 */
export function resolveOnFrameworkError(args) {
    const r = args.runtime ?? {};
    if (r.onFrameworkError !== undefined)
        return r.onFrameworkError;
    return args.onFrameworkError;
}
export function resolveToolsConfig(args) {
    const runtimeTools = (args.runtime?.tools || {});
    const placement = (runtimeTools.placement || {});
    const overlayToolsConfig = { ...runtimeTools, placement: { ...placement } };
    // Keep host-provided shape intact; framework-owned validation surfaces malformed config.
    return overlayToolsConfig;
}
export function resolveRuntime(args) {
    const r = args.runtime || {};
    const runtimePlayer = r.player ? { ...r.player } : null;
    return {
        ...r,
        assessmentId: pick(r.assessmentId, args.assessmentId),
        playerType: r.playerType ?? DEFAULT_PLAYER_TYPE,
        player: runtimePlayer,
        lazyInit: r.lazyInit ?? DEFAULT_LAZY_INIT,
        accessibility: r.accessibility ?? null,
        coordinator: r.coordinator ?? null,
        createSectionController: r.createSectionController,
        isolation: r.isolation ?? DEFAULT_ISOLATION,
        env: r.env ?? DEFAULT_ENV,
        toolConfigStrictness: pick(r.toolConfigStrictness, args.toolConfigStrictness) ?? "error",
        // Runtime callback wins; top-level callback remains a layout CE event
        // convenience for hosts that do not use DOM listeners.
        onFrameworkError: resolveOnFrameworkError({
            runtime: args.runtime,
            onFrameworkError: args.onFrameworkError,
        }),
        // Runtime callback wins over the top-level callback prop.
        onStageChange: pick(r.onStageChange, args.onStageChange),
        // Runtime callback wins over the top-level callback prop.
        onLoadingComplete: pick(r.onLoadingComplete, args.onLoadingComplete),
        tools: args.effectiveToolsConfig,
    };
}
/**
 * Engine-side orchestrator that powers `resolveSectionPlayerRuntimeState`
 * from section-player and takes
 * `resolvePlayerRuntime` as an injected callable so the toolkit core
 * stays free of host-coupled defaults (`DEFAULT_PLAYER_DEFINITIONS`).
 */
export function resolveSectionEngineRuntimeState(args, deps) {
    const assessmentId = args.assessmentId ?? DEFAULT_ASSESSMENT_ID;
    const effectiveToolsConfig = resolveToolsConfig({
        runtime: args.runtime,
    });
    const effectiveRuntime = resolveRuntime({
        assessmentId,
        runtime: args.runtime,
        effectiveToolsConfig,
        toolConfigStrictness: args.toolConfigStrictness,
        onFrameworkError: args.onFrameworkError,
        onStageChange: args.onStageChange,
        onLoadingComplete: args.onLoadingComplete,
    });
    const playerRuntime = deps.resolvePlayerRuntime({
        effectiveRuntime: effectiveRuntime,
        playerType: String(effectiveRuntime.playerType ?? DEFAULT_PLAYER_TYPE),
        env: effectiveRuntime.env ?? null,
    });
    return {
        effectiveToolsConfig,
        effectiveRuntime,
        playerRuntime,
    };
}
//# sourceMappingURL=engine-resolver.js.map