export const USER_STATE_KEY_PREFIX = 'InteractiveComponents_UserState_Root/';
export function resolveUserStateScope(explicitScope, namespace, name) {
    const explicit = explicitScope?.trim();
    if (explicit) {
        return explicit.toLowerCase();
    }
    const cleanName = name?.trim();
    if (!cleanName) {
        return null;
    }
    const cleanNamespace = namespace?.trim();
    const scope = cleanNamespace ? `${cleanNamespace}/${cleanName}` : cleanName;
    return scope.toLowerCase();
}
export function userStateStorageKey(scope) {
    return scope ? `${USER_STATE_KEY_PREFIX}${scope}` : null;
}
export function parseStoredUserSettings(raw) {
    if (!raw) {
        return {};
    }
    try {
        const parsed = JSON.parse(raw);
        if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
            return parsed;
        }
        return {};
    }
    catch {
        return {};
    }
}
export function mergeUserSettings(hostDefaults, stored) {
    return { ...(hostDefaults ?? {}), ...(stored ?? {}) };
}
export function applyUserSettingsUpdate(current, incoming) {
    const next = { ...(current ?? {}) };
    for (const [key, value] of Object.entries(incoming ?? {})) {
        if (value === null || value === undefined) {
            delete next[key];
        }
        else {
            next[key] = value;
        }
    }
    return next;
}
//# sourceMappingURL=user-state.js.map