"use strict";

import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { View, Pressable, Text, KeyboardAvoidingView, Platform, StyleSheet } from 'react-native';
import { useTheme } from "../theme/use-theme.js";
import { Portal } from '../portal';
import { PromptInputContext } from "./context.js";
import { PromptInputTextarea } from './Textarea';
import { PromptInputActions } from "./Actions.js";
import { PromptInputAttachments } from "./Attachments.js";
import { PromptInputSubmitButton } from "./SubmitButton.js";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
export function PromptInput({
  isLoading = false,
  maxHeight = 240,
  value,
  onValueChange,
  onSubmit,
  children,
  style,
  disabled = false,
  onImagePaste,
  placeholder,
  actionsLeft,
  onStop,
  emptyAction,
  attachments: controlledAttachments,
  onAddAttachment,
  onRemoveAttachment,
  onUpdateAttachment,
  disableKeyboardAvoidance = false,
  expandIcon,
  collapseIcon,
  testID
}) {
  const [internalValue, setInternalValue] = useState(value ?? '');
  const [currentHeight, setCurrentHeight] = useState(44);
  const [showFullscreen, setShowFullscreen] = useState(false);
  const [handleCompletionKey, setHandleCompletionKey] = useState(null);
  const textareaRef = useRef(null);
  const theme = useTheme();

  // Internal attachment state (used when no controlled props)
  const [internalAttachments, setInternalAttachments] = useState([]);
  const attachments = controlledAttachments ?? internalAttachments;
  const addAttachment = useCallback(a => {
    if (onAddAttachment) {
      onAddAttachment(a);
    } else {
      setInternalAttachments(prev => [...prev, a]);
    }
  }, [onAddAttachment]);
  const removeAttachment = useCallback(id => {
    if (onRemoveAttachment) {
      onRemoveAttachment(id);
    } else {
      setInternalAttachments(prev => prev.filter(a => a.id !== id));
    }
  }, [onRemoveAttachment]);
  const updateAttachment = useCallback((id, updates) => {
    if (onUpdateAttachment) {
      onUpdateAttachment(id, updates);
    } else {
      setInternalAttachments(prev => prev.map(a => a.id === id ? {
        ...a,
        ...updates
      } : a));
    }
  }, [onUpdateAttachment]);
  const handleChange = useCallback(newValue => {
    setInternalValue(newValue);
    onValueChange?.(newValue);
  }, [onValueChange]);
  const handleSubmit = useCallback(() => {
    onSubmit?.();
    if (showFullscreen) setShowFullscreen(false);
  }, [onSubmit, showFullscreen]);
  useEffect(() => {
    if (!showFullscreen) setCurrentHeight(44);
  }, [showFullscreen]);
  const showExpandIcon = currentHeight > 100;
  const isSimpleMode = !children;
  const currentValue = value ?? internalValue;
  const currentSetValue = onValueChange ?? handleChange;
  const contextValue = useMemo(() => ({
    isLoading,
    value: currentValue,
    setValue: currentSetValue,
    maxHeight,
    onSubmit: handleSubmit,
    disabled,
    textareaRef,
    currentHeight,
    setCurrentHeight,
    isFullscreen: showFullscreen,
    onImagePaste,
    attachments,
    addAttachment,
    removeAttachment,
    updateAttachment,
    handleCompletionKey,
    setHandleCompletionKey
  }), [isLoading, currentValue, currentSetValue, maxHeight, handleSubmit, disabled, currentHeight, showFullscreen, onImagePaste, attachments, addAttachment, removeAttachment, updateAttachment, handleCompletionKey]);
  const content = isSimpleMode ? /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(PromptInputAttachments, {}), /*#__PURE__*/_jsx(PromptInputTextarea, {
      placeholder: placeholder
    }), /*#__PURE__*/_jsxs(PromptInputActions, {
      style: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
        gap: 8,
        marginTop: 6,
        marginBottom: 12,
        paddingHorizontal: 12
      },
      children: [/*#__PURE__*/_jsx(View, {
        style: {
          flexDirection: 'row',
          alignItems: 'center',
          gap: 6
        },
        children: actionsLeft
      }), /*#__PURE__*/_jsx(View, {
        style: {
          flexDirection: 'row',
          alignItems: 'center',
          gap: 6
        },
        children: /*#__PURE__*/_jsx(PromptInputSubmitButton, {
          isLoading: isLoading,
          onStop: onStop,
          emptyAction: emptyAction
        })
      })]
    })]
  }) : children;
  const inputBox = /*#__PURE__*/_jsx(Pressable, {
    onPress: () => {
      if (!disabled) textareaRef.current?.focus();
    },
    disabled: disabled,
    children: /*#__PURE__*/_jsxs(View, {
      style: [{
        borderRadius: 24,
        borderWidth: 1,
        borderColor: theme.colors.border,
        backgroundColor: theme.colors.background,
        overflow: 'hidden',
        position: 'relative'
      }, disabled && {
        opacity: 0.6
      }, style],
      testID: testID,
      children: [showExpandIcon && !disabled && /*#__PURE__*/_jsx(Pressable, {
        onPress: () => setShowFullscreen(true),
        style: {
          position: 'absolute',
          top: 8,
          right: 8,
          zIndex: 10,
          backgroundColor: theme.colors.background,
          borderRadius: 9999,
          padding: 6,
          borderWidth: 1,
          borderColor: theme.colors.border
        },
        children: expandIcon ?? /*#__PURE__*/_jsx(Text, {
          style: {
            fontSize: 14,
            color: theme.colors.textSecondary
          },
          children: "\u2922"
        })
      }), content]
    })
  });
  const Wrapper = disableKeyboardAvoidance ? View : KeyboardAvoidingView;
  const wrapperProps = disableKeyboardAvoidance ? {} : {
    behavior: Platform.OS === 'ios' ? 'padding' : undefined
  };
  return /*#__PURE__*/_jsxs(PromptInputContext.Provider, {
    value: contextValue,
    children: [/*#__PURE__*/_jsx(Wrapper, {
      ...wrapperProps,
      children: inputBox
    }), showFullscreen && /*#__PURE__*/_jsx(Portal, {
      children: /*#__PURE__*/_jsxs(View, {
        style: [StyleSheet.absoluteFill, {
          zIndex: 9998,
          backgroundColor: theme.colors.background,
          // Opt back in from the Portal root's `pointer-events: none`
          // (web only — harmless on native).
          pointerEvents: 'auto'
        }],
        children: [/*#__PURE__*/_jsx(Pressable, {
          onPress: () => setShowFullscreen(false),
          style: {
            position: 'absolute',
            top: 16,
            right: 16,
            zIndex: 50,
            padding: 8,
            backgroundColor: theme.colors.background,
            borderRadius: 9999
          },
          children: collapseIcon ?? /*#__PURE__*/_jsx(Text, {
            style: {
              fontSize: 18,
              color: theme.colors.text
            },
            children: "\u2921"
          })
        }), /*#__PURE__*/_jsx(View, {
          style: {
            flex: 1
          },
          children: content
        })]
      })
    })]
  });
}
PromptInput.displayName = 'PromptInput';
//# sourceMappingURL=PromptInput.js.map