import { DEFAULT_ANIMATIONS } from '../main/main.types';
import { resolveExclusiveLoadingStyle } from './animation-exclusivity';
/**
 * Resolve the effective preference baseline for a fresh store, applying the
 * precedence (lowest → highest):
 *
 *   slice defaults  <  `chatConfig.ui` defaults  <  stored user preferences
 *
 * The `chatConfig.ui` layer is the fallback when the user has no stored prefs
 * (e.g. a first-ever load, or after the persistence backend is wiped) — the case
 * the old "read config once on connect" path got wrong whenever `chatConfig` had
 * not bound yet. Stored prefs, when present, override only the fields they carry;
 * absent fields keep the config default. Pure, so the precedence is unit-testable
 * independent of the element/store wiring.
 *
 * Config-supplied animations are normalised through {@link resolveExclusiveLoadingStyle}
 * (a raw config list may name both loading styles); stored animations are applied
 * as-is, since they were already normalised when saved.
 *
 * @internal
 */
export function resolvePreferenceBaseline(ui, stored) {
    var _a, _b;
    const u = ui !== null && ui !== void 0 ? ui : {};
    // Layer 1 — chatConfig.ui defaults.
    const baseline = {
        showToolCalls: u.showToolCalls === true,
        showThinkingSteps: u.showThinkingSteps === true,
        showNarration: u.showNarration === true,
        showAgentSwitchIndicator: u.showAgentSwitchIndicator === true,
        enabledAnimations: resolveExclusiveLoadingStyle((_b = (_a = u.animations) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : (u.animations ? [...DEFAULT_ANIMATIONS] : [])),
    };
    // Layer 2 — stored user preferences override the fields they carry.
    if (stored) {
        if (stored.enabled !== undefined)
            baseline.enabled = stored.enabled;
        if (stored.showToolCalls !== undefined)
            baseline.showToolCalls = stored.showToolCalls;
        if (stored.showThinkingSteps !== undefined)
            baseline.showThinkingSteps = stored.showThinkingSteps;
        if (stored.showNarration !== undefined)
            baseline.showNarration = stored.showNarration;
        if (stored.showAgentSwitchIndicator !== undefined) {
            baseline.showAgentSwitchIndicator = stored.showAgentSwitchIndicator;
        }
        if (Array.isArray(stored.animations)) {
            // De-dupe defensively — historical blobs may hold duplicates from the old
            // settings-modal append bug (now fixed at the `setEnabledAnimations` source).
            baseline.enabledAnimations = [...new Set(stored.animations)];
        }
    }
    return baseline;
}
