import "core-js/modules/es.symbol.js";
import "core-js/modules/es.array.filter.js";
import "core-js/modules/es.array.for-each.js";
import "core-js/modules/es.array.push.js";
import "core-js/modules/es.object.define-properties.js";
import "core-js/modules/es.object.define-property.js";
import "core-js/modules/es.object.get-own-property-descriptor.js";
import "core-js/modules/es.object.get-own-property-descriptors.js";
import "core-js/modules/es.object.keys.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/esnext.iterator.constructor.js";
import "core-js/modules/esnext.iterator.filter.js";
import "core-js/modules/esnext.iterator.for-each.js";
import "core-js/modules/web.dom-collections.for-each.js";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import "core-js/modules/es.number.constructor.js";
import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.string.replace.js";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import classNames from 'classnames';
import React, { useState, useEffect, useRef, useImperativeHandle, forwardRef } from 'react';
import './index.css';
import { FcrIconButton } from '../icon-button';
import { FcrIconType } from '../icon/type';
import { useI18n } from '../../i18n';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var defaultAutoSizeParams = {
  minHeight: 20,
  maxHeight: 160,
  initHeight: 20
};
export var MessageTextArea = /*#__PURE__*/forwardRef(function MessageTextArea(props, ref) {
  var _autoSizeParams$initH;
  var t = useI18n();
  var updateErrVisible = props.updateErrVisible,
    onSendMsg = props.onMsg,
    _props$limitation = props.limitation,
    limitation = _props$limitation === void 0 ? 200 : _props$limitation,
    styles = props.styles,
    _props$placeholder = props.placeholder,
    placeholder = _props$placeholder === void 0 ? t('fmt_chat_label_enter_message') : _props$placeholder,
    onEmoji = props.onEmoji,
    onFile = props.onFile,
    handleClickUploadImage = props.handleClickUploadImage,
    _props$defaultMsg = props.defaultMsg,
    defaultMsg = _props$defaultMsg === void 0 ? '' : _props$defaultMsg,
    updateChatTextMsg = props.updateChatTextMsg,
    _props$autoSizeParams = props.autoSizeParams,
    autoSizeParams = _props$autoSizeParams === void 0 ? _objectSpread({}, defaultAutoSizeParams) : _props$autoSizeParams;
  var _useState = useState(defaultMsg),
    _useState2 = _slicedToArray(_useState, 2),
    textMsg = _useState2[0],
    setTextMsg = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    focus = _useState4[0],
    setFocus = _useState4[1];
  var cls = classNames('fcr-message-input', {
    'fcr-message-input__focus': focus && textMsg.length <= limitation,
    'fcr-message-input__error': textMsg.length > limitation
  });
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    emojiVisible = _useState6[0],
    setEmojiVisible = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    limitationVisible = _useState8[0],
    setLimitationVisible = _useState8[1];
  var inputRef = useRef(null);
  useImperativeHandle(ref, function () {
    return {
      dom: inputRef.current
    };
  });
  var msgHeight = useRef((_autoSizeParams$initH = autoSizeParams === null || autoSizeParams === void 0 ? void 0 : autoSizeParams.initHeight) !== null && _autoSizeParams$initH !== void 0 ? _autoSizeParams$initH : defaultAutoSizeParams.initHeight);
  var handleSize = function handleSize() {
    var _autoSizeParams$minHe = autoSizeParams.minHeight,
      minHeight = _autoSizeParams$minHe === void 0 ? 100 : _autoSizeParams$minHe;
    if (inputRef !== null && inputRef !== void 0 && inputRef.current) {
      var _inputRef$current$sty;
      if (textMsg && Number(inputRef === null || inputRef === void 0 || (_inputRef$current$sty = inputRef.current.style.height) === null || _inputRef$current$sty === void 0 ? void 0 : _inputRef$current$sty.replace('px', '')) > minHeight && inputRef.current.scrollHeight === 0) {
        inputRef.current.style.height = msgHeight + 'px';
        return;
      }
      inputRef.current.style.height = minHeight + 'px';
      inputRef.current.style.height = inputRef.current.scrollHeight + 'px';
      msgHeight.current = inputRef.current.scrollHeight;
    }
  };
  var handleLimitationVisible = function handleLimitationVisible() {
    var _inputRef$current;
    var perHeight = 20;
    var line = 3;
    if (Number(inputRef === null || inputRef === void 0 || (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || (_inputRef$current = _inputRef$current.style.height) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.replace('px', '')) > perHeight * line) {
      setLimitationVisible(true);
    } else {
      setLimitationVisible(false);
    }
  };
  useEffect(function () {
    handleSize();
    handleLimitationVisible();
  }, [textMsg]);
  useEffect(function () {
    if (textMsg.length <= limitation) {
      updateErrVisible && updateErrVisible(false);
    }
  }, [textMsg]);
  useEffect(function () {
    return function () {
      updateChatTextMsg === null || updateChatTextMsg === void 0 || updateChatTextMsg({
        textMsg: textMsg,
        height: msgHeight.current
      });
    };
  }, [textMsg]);
  var msgSendHandler = function msgSendHandler() {
    if (textMsg.length === 0) return;
    if (textMsg.length > limitation) {
      updateErrVisible && updateErrVisible(true);
    } else {
      onSendMsg && onSendMsg(textMsg);
      setTextMsg('');
    }
  };
  var _useState9 = useState(false),
    _useState0 = _slicedToArray(_useState9, 2),
    isComposing = _useState0[0],
    setIsComposing = _useState0[1];
  return /*#__PURE__*/_jsxs("div", {
    className: cls,
    style: _objectSpread({}, styles),
    children: [/*#__PURE__*/_jsx("div", {
      className: classNames('fcr-input-textarea-wrapper', {
        'fcr-input-textarea-wrapper__pd_large': limitationVisible
      }),
      children: /*#__PURE__*/_jsx("div", {
        className: "fcr-input__box",
        children: /*#__PURE__*/_jsx("textarea", {
          className: classNames('fcr-input-textarea', {
            'not-empty': (textMsg === null || textMsg === void 0 ? void 0 : textMsg.length) > 0 ? true : false
          }),
          ref: inputRef,
          style: _objectSpread(_objectSpread({}, autoSizeParams), {}, {
            height: msgHeight.current
          }),
          placeholder: placeholder,
          value: textMsg,
          onChange: function onChange(target) {
            setTextMsg(target.target.value);
          },
          onFocus: function onFocus() {
            setFocus(true);
          },
          onBlur: function onBlur() {
            setFocus(false);
          },
          onCompositionStart: function onCompositionStart(_) {
            setIsComposing(true);
          },
          onCompositionEnd: function onCompositionEnd(_) {
            setIsComposing(false);
          },
          onKeyDown: function onKeyDown(event) {
            if (event.key === 'Enter' && !isComposing) {
              if (!event.shiftKey) {
                msgSendHandler();
                event.preventDefault();
              }
            }
          }
        })
      })
    }), /*#__PURE__*/_jsxs("div", {
      className: classNames('fcr-chat-send-btn-wrapper', {
        'fcr-chat-send-btn-wrapper__only-btn': !limitationVisible
      }),
      children: [limitationVisible ? /*#__PURE__*/_jsx("div", {
        style: {
          display: 'flex',
          flex: 1,
          alignItems: 'center'
        },
        children: /*#__PURE__*/_jsxs("span", {
          children: [' ', /*#__PURE__*/_jsx("span", {
            className: textMsg.length >= limitation ? 'fcr-confirm_error' : 'fcr-confirm_normal',
            children: textMsg.length
          }), ' ', "/ ", limitation]
        })
      }) : null, /*#__PURE__*/_jsx("div", {
        className: "fcr-bar__confirm-mobile",
        children: /*#__PURE__*/_jsx(FcrIconButton, {
          disabled: textMsg.length === 0,
          icon: FcrIconType.FCR_SEND,
          size: "large",
          iconColors: {
            iconPrimary: 'var(--fcr_ui_scene_white10)'
          },
          onClick: msgSendHandler
        })
      })]
    })]
  });
});