"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.FcrTextAreaBorderLess = exports.FcrTextArea = void 0;
require("core-js/modules/es.object.assign.js");
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _isNumber = _interopRequireDefault(require("lodash/isNumber"));
require("./index.css");
var _helper = require("./helper");
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: 36,
  maxHeight: 140
};
var FcrTextArea = exports.FcrTextArea = /*#__PURE__*/(0, _react.forwardRef)(function TextAreaComponent(props, ref) {
  var _props$value = props.value,
    value = _props$value === void 0 ? '' : _props$value,
    placeholder = props.placeholder,
    resizable = props.resizable,
    disabled = props.disabled,
    maxCount = props.maxCount,
    _props$onChange = props.onChange,
    onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
    _props$showCount = props.showCount,
    showCount = _props$showCount === void 0 ? true : _props$showCount,
    onFocusChange = props.onFocusChange,
    autoSize = props.autoSize,
    onKeyDown = props.onKeyDown;
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    focused = _useState2[0],
    setFocused = _useState2[1];
  var inputRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      dom: inputRef.current
    };
  });
  var autoSizeParams = Object.assign(defaultAutoSizeParams, typeof autoSize === 'boolean' ? {} : autoSize);
  var handleFocus = function handleFocus() {
    setFocused(true);
    onFocusChange === null || onFocusChange === void 0 || onFocusChange(true);
  };
  var handleBlur = function handleBlur() {
    setFocused(false);
    onFocusChange === null || onFocusChange === void 0 || onFocusChange(false);
  };
  var handleSize = function handleSize() {
    if (autoSize) {
      var _autoSizeParams$minHe = autoSizeParams.minHeight,
        minHeight = _autoSizeParams$minHe === void 0 ? 36 : _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, [value]);
  var handleChange = function handleChange(e) {
    if ((0, _isNumber["default"])(maxCount) && e.target.value.length > maxCount) {
      return;
    }
    onChange(e.target.value);
  };
  var handleClick = function handleClick() {
    if (inputRef.current) {
      inputRef.current.focus();
    }
  };
  var cls = (0, _classnames["default"])('fcr-textarea', {
    'fcr-textarea--focused': focused,
    'fcr-textarea--non-resizable': !resizable,
    'fcr-textarea--non-count': !showCount,
    'fcr-textarea--disabled': disabled
  });
  var autoSizeStyle = autoSize ? _objectSpread(_objectSpread({}, autoSizeParams), {}, {
    height: autoSizeParams.minHeight
  }) : {};
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: cls,
    onClick: handleClick,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("textarea", {
      ref: inputRef,
      onKeyDown: onKeyDown,
      style: autoSizeStyle,
      disabled: disabled,
      placeholder: placeholder,
      onFocus: handleFocus,
      onBlur: handleBlur,
      value: value,
      onChange: handleChange
    }), (0, _isNumber["default"])(maxCount) && showCount && /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
      className: "fcr-textarea-wc",
      children: [(value !== null && value !== void 0 ? value : '').length, " / ", maxCount]
    })]
  });
});
var FcrTextAreaBorderLess = exports.FcrTextAreaBorderLess = function FcrTextAreaBorderLess(_ref) {
  var placeholder = _ref.placeholder,
    _ref$onChange = _ref.onChange,
    onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange,
    label = _ref.label,
    labelIcon = _ref.labelIcon,
    defaultValue = _ref.defaultValue,
    maxLength = _ref.maxLength;
  var contentRef = (0, _react.useRef)('');
  var _useState3 = (0, _react.useState)(false),
    _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
    focused = _useState4[0],
    setFocused = _useState4[1];
  var inputRef = (0, _react.useRef)(null);
  var handleFocus = function handleFocus() {
    setFocused(true);
  };
  var handleBlur = function handleBlur() {
    setFocused(false);
  };
  var handleInput = function handleInput() {
    var _inputRef$current;
    if (maxLength && (((_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.innerHTML.length) || 0) > maxLength && inputRef.current) {
      if (contentRef.current.length < maxLength) {
        inputRef.current.innerHTML = inputRef.current.innerHTML.substring(0, maxLength);
        (0, _helper.setCaretIndex)(window, inputRef.current, maxLength);
        contentRef.current = inputRef.current.innerText;
        onChange(contentRef.current);
      } else {
        inputRef.current.innerHTML = contentRef.current;
        (0, _helper.setCaretIndex)(window, inputRef.current, maxLength);
        onChange(contentRef.current);
      }
    } else {
      var _inputRef$current2, _inputRef$current$inn, _inputRef$current3;
      contentRef.current = ((_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.innerText) || '';
      onChange((_inputRef$current$inn = (_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 ? void 0 : _inputRef$current3.innerText) !== null && _inputRef$current$inn !== void 0 ? _inputRef$current$inn : '');
    }
  };
  var handleClick = function handleClick() {
    if (inputRef.current) {
      inputRef.current.focus();
    }
  };
  (0, _react.useEffect)(function () {
    if (inputRef.current && defaultValue) {
      inputRef.current.innerHTML = defaultValue;
    }
  }, []);
  var cls = (0, _classnames["default"])('fcr-textarea fcr-textarea-borderless', {
    'fcr-textarea--focused': focused
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: cls,
    onClick: handleClick,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "fcr-textarea-label",
      children: label || labelIcon
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "fcr-textarea__inner-editor",
      ref: inputRef
      //@ts-ignore
      ,
      placeholder: placeholder,
      suppressContentEditableWarning: true,
      contentEditable: true,
      onFocus: handleFocus,
      onBlur: handleBlur,
      onInput: handleInput
    })]
  });
};