"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PromptInput = PromptInput;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _useTheme = require("../theme/use-theme.js");
var _portal = require("../portal");
var _context = require("./context.js");
var _Textarea = require("./Textarea");
var _Actions = require("./Actions.js");
var _Attachments = require("./Attachments.js");
var _SubmitButton = require("./SubmitButton.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 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] = (0, _react.useState)(value ?? '');
  const [currentHeight, setCurrentHeight] = (0, _react.useState)(44);
  const [showFullscreen, setShowFullscreen] = (0, _react.useState)(false);
  const [handleCompletionKey, setHandleCompletionKey] = (0, _react.useState)(null);
  const textareaRef = (0, _react.useRef)(null);
  const theme = (0, _useTheme.useTheme)();

  // Internal attachment state (used when no controlled props)
  const [internalAttachments, setInternalAttachments] = (0, _react.useState)([]);
  const attachments = controlledAttachments ?? internalAttachments;
  const addAttachment = (0, _react.useCallback)(a => {
    if (onAddAttachment) {
      onAddAttachment(a);
    } else {
      setInternalAttachments(prev => [...prev, a]);
    }
  }, [onAddAttachment]);
  const removeAttachment = (0, _react.useCallback)(id => {
    if (onRemoveAttachment) {
      onRemoveAttachment(id);
    } else {
      setInternalAttachments(prev => prev.filter(a => a.id !== id));
    }
  }, [onRemoveAttachment]);
  const updateAttachment = (0, _react.useCallback)((id, updates) => {
    if (onUpdateAttachment) {
      onUpdateAttachment(id, updates);
    } else {
      setInternalAttachments(prev => prev.map(a => a.id === id ? {
        ...a,
        ...updates
      } : a));
    }
  }, [onUpdateAttachment]);
  const handleChange = (0, _react.useCallback)(newValue => {
    setInternalValue(newValue);
    onValueChange?.(newValue);
  }, [onValueChange]);
  const handleSubmit = (0, _react.useCallback)(() => {
    onSubmit?.();
    if (showFullscreen) setShowFullscreen(false);
  }, [onSubmit, showFullscreen]);
  (0, _react.useEffect)(() => {
    if (!showFullscreen) setCurrentHeight(44);
  }, [showFullscreen]);
  const showExpandIcon = currentHeight > 100;
  const isSimpleMode = !children;
  const currentValue = value ?? internalValue;
  const currentSetValue = onValueChange ?? handleChange;
  const contextValue = (0, _react.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__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Attachments.PromptInputAttachments, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Textarea.PromptInputTextarea, {
      placeholder: placeholder
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Actions.PromptInputActions, {
      style: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
        gap: 8,
        marginTop: 6,
        marginBottom: 12,
        paddingHorizontal: 12
      },
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: {
          flexDirection: 'row',
          alignItems: 'center',
          gap: 6
        },
        children: actionsLeft
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: {
          flexDirection: 'row',
          alignItems: 'center',
          gap: 6
        },
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_SubmitButton.PromptInputSubmitButton, {
          isLoading: isLoading,
          onStop: onStop,
          emptyAction: emptyAction
        })
      })]
    })]
  }) : children;
  const inputBox = /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
    onPress: () => {
      if (!disabled) textareaRef.current?.focus();
    },
    disabled: disabled,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.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__*/(0, _jsxRuntime.jsx)(_reactNative.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__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
          style: {
            fontSize: 14,
            color: theme.colors.textSecondary
          },
          children: "\u2922"
        })
      }), content]
    })
  });
  const Wrapper = disableKeyboardAvoidance ? _reactNative.View : _reactNative.KeyboardAvoidingView;
  const wrapperProps = disableKeyboardAvoidance ? {} : {
    behavior: _reactNative.Platform.OS === 'ios' ? 'padding' : undefined
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_context.PromptInputContext.Provider, {
    value: contextValue,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapper, {
      ...wrapperProps,
      children: inputBox
    }), showFullscreen && /*#__PURE__*/(0, _jsxRuntime.jsx)(_portal.Portal, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: [_reactNative.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__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
          onPress: () => setShowFullscreen(false),
          style: {
            position: 'absolute',
            top: 16,
            right: 16,
            zIndex: 50,
            padding: 8,
            backgroundColor: theme.colors.background,
            borderRadius: 9999
          },
          children: collapseIcon ?? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
            style: {
              fontSize: 18,
              color: theme.colors.text
            },
            children: "\u2921"
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
          style: {
            flex: 1
          },
          children: content
        })]
      })
    })]
  });
}
PromptInput.displayName = 'PromptInput';
//# sourceMappingURL=PromptInput.js.map