import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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 "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.assign.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 classNames from 'classnames';
import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
import isNumber from 'lodash/isNumber';
import './index.css';
import { setCaretIndex } from './helper';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var defaultAutoSizeParams = {
  minHeight: 36,
  maxHeight: 140
};
export var FcrTextArea = /*#__PURE__*/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 = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    focused = _useState2[0],
    setFocused = _useState2[1];
  var inputRef = useRef(null);
  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';
      }
    }
  };
  useEffect(handleSize, [value]);
  var handleChange = function handleChange(e) {
    if (isNumber(maxCount) && e.target.value.length > maxCount) {
      return;
    }
    onChange(e.target.value);
  };
  var handleClick = function handleClick() {
    if (inputRef.current) {
      inputRef.current.focus();
    }
  };
  var cls = classNames('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__*/_jsxs("div", {
    className: cls,
    onClick: handleClick,
    children: [/*#__PURE__*/_jsx("textarea", {
      ref: inputRef,
      onKeyDown: onKeyDown,
      style: autoSizeStyle,
      disabled: disabled,
      placeholder: placeholder,
      onFocus: handleFocus,
      onBlur: handleBlur,
      value: value,
      onChange: handleChange
    }), isNumber(maxCount) && showCount && /*#__PURE__*/_jsxs("span", {
      className: "fcr-textarea-wc",
      children: [(value !== null && value !== void 0 ? value : '').length, " / ", maxCount]
    })]
  });
});
export var 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 = useRef('');
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    focused = _useState4[0],
    setFocused = _useState4[1];
  var inputRef = 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);
        setCaretIndex(window, inputRef.current, maxLength);
        contentRef.current = inputRef.current.innerText;
        onChange(contentRef.current);
      } else {
        inputRef.current.innerHTML = contentRef.current;
        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();
    }
  };
  useEffect(function () {
    if (inputRef.current && defaultValue) {
      inputRef.current.innerHTML = defaultValue;
    }
  }, []);
  var cls = classNames('fcr-textarea fcr-textarea-borderless', {
    'fcr-textarea--focused': focused
  });
  return /*#__PURE__*/_jsxs("div", {
    className: cls,
    onClick: handleClick,
    children: [/*#__PURE__*/_jsx("span", {
      className: "fcr-textarea-label",
      children: label || labelIcon
    }), /*#__PURE__*/_jsx("div", {
      className: "fcr-textarea__inner-editor",
      ref: inputRef
      //@ts-ignore
      ,
      placeholder: placeholder,
      suppressContentEditableWarning: true,
      contentEditable: true,
      onFocus: handleFocus,
      onBlur: handleBlur,
      onInput: handleInput
    })]
  });
};