"use strict";

import React, { useCallback, useImperativeHandle, useRef } from 'react';
import { TextInput, View } from 'react-native';
import { useTheme } from "../theme/use-theme.js";
import { usePromptInput } from "./context.js";
import { jsx as _jsx } from "react/jsx-runtime";
export function PromptInputTextarea({
  placeholder,
  style,
  inputStyle,
  textInputProps,
  testID
}) {
  const {
    value,
    setValue,
    onSubmit,
    disabled,
    textareaRef,
    setCurrentHeight,
    isFullscreen,
    maxHeight,
    handleCompletionKey
  } = usePromptInput();
  const theme = useTheme();
  const inputRef = useRef(null);
  useImperativeHandle(textareaRef, () => inputRef.current);
  const handleKeyPress = useCallback(e => {
    textInputProps?.onKeyPress?.(e);
    const key = e.nativeEvent.key;
    if (handleCompletionKey && ['ArrowUp', 'ArrowDown', 'Enter', 'Escape'].includes(key)) {
      if (handleCompletionKey(key)) {
        e.preventDefault();
        return;
      }
    }
  }, [handleCompletionKey, textInputProps]);
  const handleContentSizeChange = useCallback(e => {
    textInputProps?.onContentSizeChange?.(e);
  }, [textInputProps]);
  const textInput = /*#__PURE__*/_jsx(View, {
    style: [{
      width: '100%'
    }, isFullscreen && {
      flex: 1
    }],
    onLayout: e => setCurrentHeight(e.nativeEvent.layout.height),
    children: /*#__PURE__*/_jsx(TextInput, {
      ref: inputRef,
      accessibilityLabel: "Message input",
      value: value,
      onChangeText: setValue,
      onSubmitEditing: onSubmit,
      onKeyPress: handleKeyPress,
      onContentSizeChange: handleContentSizeChange,
      multiline: true,
      editable: !disabled,
      scrollEnabled: isFullscreen,
      placeholder: placeholder,
      placeholderTextColor: theme.colors.textTertiary,
      style: [{
        width: '100%',
        fontSize: 16,
        color: theme.colors.text,
        paddingHorizontal: 14,
        backgroundColor: 'transparent',
        borderWidth: 0
      }, isFullscreen ? {
        flex: 1,
        paddingTop: 16,
        paddingBottom: 100
      } : {
        minHeight: 44,
        paddingVertical: 12,
        maxHeight,
        overflow: 'scroll'
      }, inputStyle],
      testID: testID,
      ...textInputProps
    })
  });
  if (isFullscreen) {
    return /*#__PURE__*/_jsx(View, {
      style: [{
        flex: 1
      }, style],
      children: textInput
    });
  }
  return /*#__PURE__*/_jsx(View, {
    style: style,
    children: textInput
  });
}
PromptInputTextarea.displayName = 'PromptInputTextarea';
//# sourceMappingURL=Textarea.js.map