"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatInput = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const box_1 = __importDefault(require("../box"));
const button_1 = __importDefault(require("../button"));
const core_1 = require("../core");
const icon_1 = __importDefault(require("../icon"));
const consts_1 = require("../input/consts");
const helpText_1 = __importDefault(require("../input/helpText"));
const utils_1 = require("../utils");
const chat_types_1 = require("./chat.types");
const chatInputButton_1 = __importDefault(require("./chatInputButton"));
const CustomTextarea = core_1.styled.textareaBox `
  border-radius: none;
  border-width: 0;
  outline: none;
  padding: 0 8px;
  resize: none;
  transition-duration: 0;
  width: 100%;
  min-height: 1.5em;
  overflow-y: hidden;

  &::placeholder {
    padding-top: 8px;
  }

  /* Disabled state styles */
  &[aria-disabled='true'] {
    opacity: 0.5;
    cursor: not-allowed;
    background-color: sandstone.79;
    border-color: sandstone.79;
  }
`;
/**
 * Displays a chat input interface.
 */
exports.ChatInput = (0, core_1.vui)((props, ref) => {
    const { helpText, placeholder = 'Type your message', status = chat_types_1.ChatInputStatus.Default, maxTextareaHeight = 120, processingMessage = 'Generating message', onNewTopicClick, onSend, onStop } = props, rest = __rest(props, ["helpText", "placeholder", "status", "maxTextareaHeight", "processingMessage", "onNewTopicClick", "onSend", "onStop"]);
    const [value, setValue] = (0, react_1.useState)('');
    const styles = (0, core_1.useStyleConfig)('Chat', props);
    (0, react_1.useEffect)(() => {
        if (status === chat_types_1.ChatInputStatus.Default)
            setValue('');
    }, [status]);
    const onKeyDown = (e) => {
        if (e.key === core_1.KeyboardKeys.Enter && e.ctrlKey) {
            e.stopPropagation();
            if ((value === null || value === void 0 ? void 0 : value.length) && status === chat_types_1.ChatInputStatus.Default)
                onSend === null || onSend === void 0 ? void 0 : onSend(value);
        }
        if (e.key === core_1.KeyboardKeys.Escape) {
            e.stopPropagation();
            if (status === chat_types_1.ChatInputStatus.Processing)
                onStop === null || onStop === void 0 ? void 0 : onStop();
        }
    };
    const handleTextareaChange = (e) => {
        const textarea = e.target;
        textarea.placeholder = '';
        // Dynamically adjust the height of the textarea
        if (textarea.scrollHeight <= maxTextareaHeight) {
            textarea.style.height = 'auto';
            textarea.style.maxHeight = 'auto';
            textarea.style.height = `${textarea.scrollHeight}px`;
            textarea.style.maxHeight = `${textarea.scrollHeight}px`;
            textarea.style.lineHeight = '2.2';
        }
        if (status === chat_types_1.ChatInputStatus.Default)
            setValue(textarea.value);
    };
    const isDisabled = status === chat_types_1.ChatInputStatus.Disabled;
    return ((0, jsx_runtime_1.jsxs)(box_1.default, Object.assign({ className: (0, utils_1.cs)('vui-chat-input'), justifyContent: "space-between", ref: ref }, styles === null || styles === void 0 ? void 0 : styles.input, rest, { children: [!!onNewTopicClick && status === chat_types_1.ChatInputStatus.Default && ((0, jsx_runtime_1.jsx)(button_1.default, { animation: "fadeUp", icon: "falPlus", mr: 1, onClick: () => onNewTopicClick === null || onNewTopicClick === void 0 ? void 0 : onNewTopicClick() })), status === chat_types_1.ChatInputStatus.Processing ? ((0, jsx_runtime_1.jsxs)(box_1.default, { borderColor: consts_1.inputColors.border, borderWidth: 1, center: true, justifyContent: "flex-start", py: 0.5, w: "100%", children: [(0, jsx_runtime_1.jsx)(icon_1.default, { mx: 1, name: "fadSpinnerThird", pathFill: ['blue.40', 'blue.60'], size: "lg", variant: "spinning" }), (0, jsx_runtime_1.jsx)(box_1.default, { children: processingMessage })] })) : ((0, jsx_runtime_1.jsxs)(box_1.default, { animation: "fadeUp", border: "0", column: true, outline: "none", w: "100%", children: [(0, jsx_runtime_1.jsxs)(box_1.default, { borderColor: consts_1.inputColors.border, borderWidth: 1, center: true, focusWithinRingColor: consts_1.inputColors.focus, justifyContent: "flex-start", position: "relative", w: "100%", children: [(0, jsx_runtime_1.jsx)(CustomTextarea, { disabled: isDisabled, onChange: handleTextareaChange, onFocus: handleTextareaChange, onKeyDown: onKeyDown, placeholder: placeholder, value: value }), (0, jsx_runtime_1.jsx)(chatInputButton_1.default, { disabled: !(value === null || value === void 0 ? void 0 : value.length), h: "100%", onSend: () => {
                                    if (value === null || value === void 0 ? void 0 : value.length)
                                        onSend === null || onSend === void 0 ? void 0 : onSend(value);
                                }, onStop: onStop, status: status })] }), !!helpText && (0, jsx_runtime_1.jsx)(helpText_1.default, { children: helpText })] }))] })));
});
exports.ChatInput.displayName = 'ChatInput';
exports.default = exports.ChatInput;
//# sourceMappingURL=chatInput.js.map