/**
 * Compatibility helpers for migrating deprecated monitor threshold keys.
 * @module sync/legacy-monitor-thresholds
 */
import { getAtPath, isJsonObject, jsonEquals, setAtPath, } from "./json-path.js";
import { recordedPopulation, recordPopulation, removePopulation, } from "./sync-population.js";
const LEGACY_MONITOR_THRESHOLD_MIGRATIONS = [
    {
        legacyKey: "monitor.thresholds.sentryMinEvents24h",
        currentKey: "monitor.thresholds.minEvents24h",
    },
    {
        legacyKey: "monitor.thresholds.xrayFaultRatePct",
        currentKey: "monitor.thresholds.faultRatePct",
    },
];
/**
 * Return a copy of an object with one dot-path property removed.
 * @param root - Object to copy
 * @param dotPath - Dot-separated key path to remove
 * @returns Updated object without the path
 */
function removeAtPath(root, dotPath) {
    const dotIndex = dotPath.indexOf(".");
    const head = dotIndex === -1 ? dotPath : dotPath.slice(0, dotIndex);
    const rest = dotIndex === -1 ? "" : dotPath.slice(dotIndex + 1);
    if (rest === "") {
        const { [head]: _removed, ...remaining } = root;
        return remaining;
    }
    const child = root[head];
    if (!isJsonObject(child)) {
        return root;
    }
    return { ...root, [head]: removeAtPath(child, rest) };
}
/**
 * Move legacy monitor thresholds to provider-neutral keys when provenance
 * proves Lisa populated the legacy value.
 * @param committed - Committed config object
 * @returns Updated config and migration actions
 */
export function migrateLegacyMonitorThresholds(committed) {
    return LEGACY_MONITOR_THRESHOLD_MIGRATIONS.reduce((current, migration) => {
        const legacyValue = getAtPath(current.committed, migration.legacyKey);
        const currentValue = getAtPath(current.committed, migration.currentKey);
        const recorded = recordedPopulation(current.committed, migration.legacyKey);
        if (legacyValue === undefined ||
            currentValue !== undefined ||
            recorded === undefined ||
            !jsonEquals(legacyValue, recorded)) {
            return current;
        }
        const committed = recordPopulation(removePopulation(removeAtPath(setAtPath(current.committed, migration.currentKey, legacyValue), migration.legacyKey), migration.legacyKey), migration.currentKey, legacyValue);
        return {
            committed,
            actions: [
                ...current.actions,
                {
                    key: migration.currentKey,
                    detail: `auto-populated legacy ${migration.legacyKey} migrated to provider-neutral key`,
                },
            ],
        };
    }, { committed, actions: [] });
}
/**
 * Append legacy monitor-threshold migration actions to a sync state.
 * @param state - Sync state containing committed config and actions
 * @param entryKey - Registry entry key currently being populated
 * @returns Updated state with migration actions appended
 */
export function applyLegacyMonitorThresholdMigration(state, entryKey) {
    if (entryKey !== "monitor") {
        return state;
    }
    const migration = migrateLegacyMonitorThresholds(state.committed);
    if (migration.actions.length === 0) {
        return state;
    }
    return {
        ...state,
        committed: migration.committed,
        actions: [
            ...state.actions,
            ...migration.actions.map(action => ({
                ...action,
                kind: "default-evolved",
            })),
        ],
    };
}
//# sourceMappingURL=legacy-monitor-thresholds.js.map