"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PromptInputTextarea = PromptInputTextarea;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _useTheme = require("../theme/use-theme.js");
var _context = require("./context.js");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function PromptInputTextarea({
  placeholder,
  style,
  inputStyle,
  textInputProps,
  testID
}) {
  const {
    value,
    setValue,
    onSubmit,
    disabled,
    textareaRef,
    setCurrentHeight,
    isFullscreen,
    maxHeight,
    onImagePaste,
    handleCompletionKey
  } = (0, _context.usePromptInput)();
  const theme = (0, _useTheme.useTheme)();
  const inputRef = (0, _react.useRef)(null);
  const wrapperRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(textareaRef, () => inputRef.current);

  // Web-only: paste event listener for images
  (0, _react.useEffect)(() => {
    if (!onImagePaste || typeof document === 'undefined') return;
    const handlePaste = e => {
      const activeElement = document.activeElement;
      const wrapper = wrapperRef.current;
      if (!wrapper || !wrapper.contains(activeElement)) return;
      const items = e.clipboardData?.items;
      if (!items) return;
      const imageFiles = [];
      for (let i = 0; i < items.length; i++) {
        const item = items[i];
        if (item.type.indexOf('image') !== -1) {
          const file = item.getAsFile();
          if (file) imageFiles.push(file);
        }
      }
      if (imageFiles.length > 0) {
        e.preventDefault();
        onImagePaste(imageFiles);
      }
    };
    document.addEventListener('paste', handlePaste);
    return () => document.removeEventListener('paste', handlePaste);
  }, [onImagePaste]);
  const handleKeyPress = (0, _react.useCallback)(e => {
    textInputProps?.onKeyPress?.(e);
    const key = e.nativeEvent.key;

    // Autocomplete key navigation
    if (handleCompletionKey && ['ArrowUp', 'ArrowDown', 'Enter', 'Escape'].includes(key)) {
      if (handleCompletionKey(key)) {
        e.preventDefault();
        return;
      }
    }

    // Enter-to-submit (without Shift) when not in fullscreen
    if (key === 'Enter' && !isFullscreen) {
      const nativeEvent = e.nativeEvent;
      if (!nativeEvent.shiftKey) {
        e.preventDefault();
        onSubmit?.();
      }
    }
  }, [handleCompletionKey, isFullscreen, onSubmit, textInputProps]);
  const handleContentSizeChange = (0, _react.useCallback)(e => {
    textInputProps?.onContentSizeChange?.(e);
  }, [textInputProps]);
  const fieldSizingStyle = !isFullscreen ? {
    fieldSizing: 'content',
    minHeight: 44,
    maxHeight,
    overflow: 'auto'
  } : undefined;
  const textInput = /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    ref: wrapperRef,
    style: [{
      width: '100%'
    }, isFullscreen && {
      flex: 1
    }],
    onLayout: e => setCurrentHeight(e.nativeEvent.layout.height),
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.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
      },
      // Web-only: remove focus outline
      {
        outlineStyle: 'none'
      }, isFullscreen ? {
        flex: 1,
        paddingTop: 16,
        paddingBottom: 100
      } : {
        paddingVertical: 12
      }, fieldSizingStyle, inputStyle],
      testID: testID,
      ...textInputProps
    })
  });
  if (isFullscreen) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: [{
        flex: 1
      }, style],
      children: textInput
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    style: style,
    children: textInput
  });
}
PromptInputTextarea.displayName = 'PromptInputTextarea';
//# sourceMappingURL=Textarea.web.js.map