import { atom } from 'jotai';

import { WidgetWrapperVariant } from '@envive-ai/react-toolkit-v3/WidgetWrapper';
/**
 * Data structure for ChatPreviewLoading component props that need to be stored.
 */
export type ChatPreviewLoadingData = {
  titleLabel?: string;
  textFieldPlaceholder?: string;
  logoSrc?: string;
  hideLogo?: boolean;
  variant?: WidgetWrapperVariant;
  hexCardColor?: string;
  hideTextField?: boolean;
};

/**
 * Atom that stores ChatPreviewLoading data by widgetConfigId.
 * This allows widgets to access the most recent loading state information
 * even when the widget is not actively rendering.
 */
const internalChatPreviewLoadingDataAtom = atom<Record<string, ChatPreviewLoadingData>>({});

/**
 * Atom to read ChatPreviewLoading data for a specific widgetConfigId.
 */
export const chatPreviewLoadingDataAtom = atom(
  get => get(internalChatPreviewLoadingDataAtom),
  (get, set, update: { widgetConfigId: string; data: ChatPreviewLoadingData }) => {
    const current = get(internalChatPreviewLoadingDataAtom);
    set(internalChatPreviewLoadingDataAtom, {
      ...current,
      [update.widgetConfigId]: update.data,
    });
  },
);
