"use strict";

require("core-js/modules/es.symbol.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.array.for-each.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.array.push.js");
require("core-js/modules/es.object.define-properties.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.object.get-own-property-descriptors.js");
require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.filter.js");
require("core-js/modules/esnext.iterator.for-each.js");
require("core-js/modules/esnext.weak-map.delete-all.js");
require("core-js/modules/esnext.weak-map.emplace.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MessageTextArea = exports.ButtonIcons = void 0;
Object.defineProperty(exports, "handleClickUploadImage", {
  enumerable: true,
  get: function get() {
    return _handleUploadImag.handleClickUploadImage;
  }
});
var _objectDestructuringEmpty2 = _interopRequireDefault(require("@babel/runtime/helpers/objectDestructuringEmpty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
require("./index.css");
var _iconButton = require("../icon-button");
var _type = require("../icon/type");
var _icon = require("../icon");
var _handleUploadImag = require("./handleUploadImag");
var _i18n = require("../../i18n");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(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 (var _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 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) { (0, _defineProperty2["default"])(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; }
var defaultAutoSizeParams = {
  minHeight: 45,
  maxHeight: 68
};
var MessageTextArea = exports.MessageTextArea = /*#__PURE__*/(0, _react.forwardRef)(function MessageTextArea(props, ref) {
  var t = (0, _i18n.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 = (0, _react.useState)(defaultMsg),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    textMsg = _useState2[0],
    setTextMsg = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
    focus = _useState4[0],
    setFocus = _useState4[1];
  var cls = (0, _classnames["default"])('fcr-message-input', {
    'fcr-message-input__focus': focus && textMsg.length <= limitation,
    'fcr-message-input__error': textMsg.length > limitation
  });
  var _useState5 = (0, _react.useState)(false),
    _useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
    emojiVisible = _useState6[0],
    setEmojiVisible = _useState6[1];
  var inputRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      dom: inputRef.current
    };
  });
  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) {
      inputRef.current.style.height = minHeight + 'px';
      inputRef.current.style.height = inputRef.current.scrollHeight + 'px';
    }
  };
  (0, _react.useEffect)(handleSize, [textMsg]);
  (0, _react.useEffect)(function () {
    if (textMsg.length <= limitation) {
      updateErrVisible && updateErrVisible(false);
    }
  }, [textMsg]);
  var msgSendHandler = function msgSendHandler() {
    if (textMsg.length === 0) return;
    if (textMsg.length > limitation) {
      updateErrVisible && updateErrVisible(true);
    } else {
      onSendMsg && onSendMsg(textMsg);
      setTextMsg('');
      updateChatTextMsg === null || updateChatTextMsg === void 0 || updateChatTextMsg('');
    }
  };
  var _useState7 = (0, _react.useState)(false),
    _useState8 = (0, _slicedToArray2["default"])(_useState7, 2),
    isComposing = _useState8[0],
    setIsComposing = _useState8[1];
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: cls,
    style: _objectSpread({}, styles),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "fcr-input__box",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("textarea", {
        ref: inputRef,
        style: _objectSpread(_objectSpread({}, autoSizeParams), {}, {
          height: autoSizeParams === null || autoSizeParams === void 0 ? void 0 : autoSizeParams.minHeight
        }),
        placeholder: placeholder,
        value: textMsg,
        onChange: function onChange(event) {
          setTextMsg(event.target.value);
          updateChatTextMsg === null || updateChatTextMsg === void 0 || updateChatTextMsg(event.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__*/(0, _jsxRuntime.jsxs)("div", {
      className: "fcr-input__bottom-bar",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-bar__buttons"
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "fcr-bar__confirm",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            className: textMsg.length >= limitation ? 'fcr-confirm_error' : 'fcr-confirm_normal',
            children: textMsg.length
          }), ' ', "/ ", limitation]
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_iconButton.FcrIconButton, {
          disabled: textMsg.length === 0,
          icon: _type.FcrIconType.FCR_SEND,
          size: "large",
          iconColors: {
            iconPrimary: 'var(--fcr_ui_scene_ramp_brand6)'
          },
          onClick: msgSendHandler
        })]
      })]
    })]
  });
});
var ButtonIcons = exports.ButtonIcons = function ButtonIcons(_ref) {
  (0, _objectDestructuringEmpty2["default"])(_ref);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "fcr-button-icons",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_iconButton.FcrIconButton, {
      icon: _type.FcrIconType.FCR_OPENFILE,
      iconColors: {
        iconPrimary: 'var(--fcr_web_ui_scene_mainicon1)'
      },
      size: "mini"
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: 'fcr-icon-drop',
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
        type: _type.FcrIconType.FCR_DROPDOWN,
        colors: {
          iconPrimary: 'var(--fcr_web_ui_scene_mainicon1)'
        }
      })
    })]
  });
};