import { createContext, useContext } from 'react';
import type { ElementSettings, WidgetSettings } from '@/types/appearance';

interface DefaultPresetContextValue {
    isDefaultPreset: boolean;
    defaultElementSettings: ElementSettings | null;
    defaultWidgetSettings: WidgetSettings | null;
}

const DefaultPresetContext = createContext<DefaultPresetContextValue>({
    isDefaultPreset: false,
    defaultElementSettings: null,
    defaultWidgetSettings: null,
});

export const DefaultPresetProvider = DefaultPresetContext.Provider;

export function useDefaultPreset() {
    return useContext(DefaultPresetContext);
}

/**
 * Resolve a dot-notation path against an object tree.
 * Returns the value at the path, or undefined if not found.
 */
export function getNestedValue(obj: unknown, path: string): unknown {
    if (!obj || typeof obj !== 'object' || !path) return undefined;
    const parts = path.split('.');
    let current: any = obj;
    for (const part of parts) {
        if (current === null || current === undefined || typeof current !== 'object') return undefined;
        current = current[part];
    }
    return current;
}
