/*
 * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { foundryModel } from "@osdk/aip-core";
import { useChat } from "@osdk/react/experimental/aip";
import * as React from "react";
import { BaseAipAgentChat } from "./BaseAipAgentChat.js";
import { AipAgentChatModelPicker } from "./components/AipAgentChatModelPicker.js";
const FALLBACK_MODEL_API_NAME = "gpt-4o";

/**
 * OSDK-aware chat surface backed by Foundry's Language Model Service.
 * Constructs the LMS-backed model internally and uses `useChat` to
 * manage conversation state, so consumers never need to import `useChat`,
 * `streamText`, or `foundryModel` themselves.
 */
export function AipAgentChat({
  client,
  model: controlledModel,
  defaultModel,
  availableModels,
  onModelChange,
  system,
  initialMessages,
  className,
  placeholder,
  enableAutoScroll,
  onError,
  onFinish,
  renderEmptyState,
  renderMessage
}) {
  // Internal state used only in uncontrolled mode. Initialized once
  // from the first available source: `controlledModel` → `defaultModel`
  // → first entry of `availableModels` → `FALLBACK_MODEL_API_NAME`.
  const [internalModel, setInternalModel] = React.useState(() => controlledModel ?? defaultModel ?? availableModels?.[0] ?? FALLBACK_MODEL_API_NAME);
  const isControlled = controlledModel != null;
  const activeModel = isControlled ? controlledModel : internalModel;
  const handleModelChange = React.useCallback(next => {
    if (!isControlled) {
      setInternalModel(next);
    }
    onModelChange?.(next);
  }, [isControlled, onModelChange]);
  const model = React.useMemo(() => foundryModel({
    client,
    model: activeModel
  }), [client, activeModel]);
  const {
    messages,
    status,
    error,
    sendMessage,
    stop,
    clearError
  } = useChat({
    model,
    system,
    messages: initialMessages,
    onError,
    onFinish
  });

  // Snap the uncontrolled model to the first available option whenever the
  // current selection isn't in `availableModels` (e.g. on first async resolve).
  React.useEffect(() => {
    if (isControlled || availableModels == null || availableModels.length === 0) {
      return;
    }
    if (!availableModels.includes(internalModel)) {
      setInternalModel(availableModels[0]);
    }
  }, [isControlled, availableModels, internalModel]);
  const handleSendMessage = React.useCallback(text => {
    return sendMessage({
      text
    });
  }, [sendMessage]);
  const composerFooter = availableModels != null && availableModels.length > 0 ? /*#__PURE__*/React.createElement(AipAgentChatModelPicker, {
    activeModel: activeModel,
    models: availableModels,
    onModelChange: handleModelChange,
    disabled: status === "submitted" || status === "streaming"
  }) : undefined;
  return /*#__PURE__*/React.createElement(BaseAipAgentChat, {
    className: className,
    composerFooter: composerFooter,
    enableAutoScroll: enableAutoScroll,
    messages: messages,
    status: status,
    error: error,
    onClearError: clearError,
    onSendMessage: handleSendMessage,
    onStop: stop,
    placeholder: placeholder,
    renderEmptyState: renderEmptyState,
    renderMessage: renderMessage
  });
}
//# sourceMappingURL=AipAgentChat.js.map