import { isJsonObject } from './hub/types/json.js';
import { ensureProtocolStateWithNative, getProtocolStateWithNative } from '../router/virtual-router/engine-selection/native-shared-conversion-semantics.js';
const PROTOCOL_STATE_KEY = 'protocolState';
export function ensureProtocolState(metadata, protocol) {
    const native = ensureProtocolStateWithNative(metadata, protocol);
    for (const key of Object.keys(metadata)) {
        if (!Object.prototype.hasOwnProperty.call(native.metadata, key)) {
            delete metadata[key];
        }
    }
    Object.assign(metadata, native.metadata);
    if (!metadata[PROTOCOL_STATE_KEY] || !isJsonObject(metadata[PROTOCOL_STATE_KEY])) {
        metadata[PROTOCOL_STATE_KEY] = {};
    }
    const container = metadata[PROTOCOL_STATE_KEY];
    if (!isJsonObject(container[protocol])) {
        container[protocol] = native.node;
    }
    return container[protocol];
}
export function getProtocolState(metadata, protocol) {
    if (!metadata) {
        return undefined;
    }
    const node = getProtocolStateWithNative(metadata, protocol);
    return node && isJsonObject(node) ? node : undefined;
}
