import { Modal } from "@theme";

import { DEFAULT_CHAT_SETTINGS } from "./ChatSettingsContext.ts";
import ChatSettingsModalForm from "./ChatSettingsModalForm.tsx";
import type { ChatSettings } from "./types.ts";

export default function ChatSettingsModal({
  isOpen,
  onClose,
  settings,
  onChange,
  downloadedModels,
  showCloseButton,
}: {
  isOpen: boolean;
  onClose: () => void;
  settings: ChatSettings | null;
  onChange: (settings: ChatSettings) => void;
  downloadedModels: Array<string>;
  showCloseButton: boolean;
}) {
  const handleSubmit = (data: ChatSettings) => onChange(data);

  return (
    <Modal
      isOpen={isOpen}
      onClose={onClose}
      showCloseButton={showCloseButton}
      title="Conversation Settings"
    >
      <ChatSettingsModalForm
        key={JSON.stringify(settings)}
        defaultValues={
          settings
            ? {
                tools: settings.tools || DEFAULT_CHAT_SETTINGS.tools,
                modelKey: settings.modelKey || DEFAULT_CHAT_SETTINGS.modelKey,
                systemPrompt:
                  settings.systemPrompt || DEFAULT_CHAT_SETTINGS.systemPrompt,
                temperature:
                  settings.temperature || DEFAULT_CHAT_SETTINGS.temperature,
                enableThinking:
                  settings.enableThinking === null
                    ? DEFAULT_CHAT_SETTINGS.enableThinking
                    : settings.enableThinking,
              }
            : DEFAULT_CHAT_SETTINGS
        }
        onSubmit={handleSubmit}
        downloadedModels={downloadedModels}
      />
    </Modal>
  );
}
