"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _isHotkey = _interopRequireDefault(require("is-hotkey"));
var _clickOutside = _interopRequireDefault(require("./click-outside"));
var _fallbackEditor = _interopRequireDefault(require("./fallback-editor"));
var _normalEditor = _interopRequireDefault(require("./normal-editor"));
var _getBrowserInfo = _interopRequireDefault(require("../../utils/get-browser-Info"));
var _constants = require("../../constants");
require("./index.css");
const LongTextInlineEditor = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
  let {
    isAlwaysEnableEdit = false,
    autoFocus = true,
    autoSave,
    isCheckBrowser,
    saveDelay,
    value,
    lang,
    headerName,
    isImageUploadOnly = true,
    isSupportMultipleFiles = false,
    onClick,
    onSaveEditorValue,
    editorApi
  } = _ref;
  const [enableEdit, setEnableEdit] = (0, _react.useState)(isAlwaysEnableEdit);
  const valueRef = (0, _react.useRef)(typeof value === 'string' ? {
    text: value
  } : value);
  const longTextValueChangedRef = (0, _react.useRef)(false);
  const editorRef = (0, _react.useRef)(null);
  const {
    isWindowsWechat
  } = (0, _react.useMemo)(() => {
    return (0, _getBrowserInfo.default)(isCheckBrowser);
  }, [isCheckBrowser]);
  const openEditor = (0, _react.useCallback)(() => {
    if (isAlwaysEnableEdit) return;
    setEnableEdit(true);
  }, [isAlwaysEnableEdit]);
  const closeEditor = (0, _react.useCallback)(() => {
    if (longTextValueChangedRef.current) {
      onSaveEditorValue(valueRef.current);
    }
    if (!isAlwaysEnableEdit) {
      setEnableEdit(false);
    }
  }, [isAlwaysEnableEdit, longTextValueChangedRef, valueRef, onSaveEditorValue]);
  const onEditorValueChanged = (0, _react.useCallback)(value => {
    valueRef.current = value;
    longTextValueChangedRef.current = true;
    if (isAlwaysEnableEdit) {
      onSaveEditorValue(valueRef.current);
    }
  }, [isAlwaysEnableEdit, onSaveEditorValue]);
  const onHotKey = (0, _react.useCallback)(event => {
    if (!enableEdit) return;
    const keyCode = event.keyCode;
    const isModP = (0, _isHotkey.default)('mod+p', event);
    if (keyCode === _constants.KeyCodes.Esc || isModP) {
      event.preventDefault();
      !isModP && event.stopPropagation();
      closeEditor();
      return;
    }
  }, [enableEdit, closeEditor]);
  (0, _react.useImperativeHandle)(ref, () => {
    var _editorRef$current;
    return {
      enableEdit: enableEdit,
      openEditor: openEditor,
      closeEditor: closeEditor,
      getEditor: ((_editorRef$current = editorRef.current) === null || _editorRef$current === void 0 ? void 0 : _editorRef$current.getEditor) || (() => null)
    };
  }, [enableEdit, openEditor, closeEditor, editorRef]);
  const handelEnableEdit = (0, _react.useCallback)(() => {
    onClick && onClick();
    openEditor();
  }, [openEditor, onClick]);
  return /*#__PURE__*/_react.default.createElement(_clickOutside.default, {
    onClickOutside: closeEditor
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "w-100",
    onKeyDown: onHotKey
  }, isWindowsWechat ? /*#__PURE__*/_react.default.createElement(_fallbackEditor.default, {
    ref: editorRef,
    enableEdit: enableEdit,
    value: valueRef.current.text,
    onChange: onEditorValueChanged,
    closeEditor: closeEditor
  }) : /*#__PURE__*/_react.default.createElement(_normalEditor.default, {
    ref: editorRef,
    enableEdit: enableEdit,
    handelEnableEdit: handelEnableEdit,
    lang: lang,
    headerName: headerName,
    value: valueRef.current.text,
    autoSave: autoSave,
    saveDelay: saveDelay,
    isCheckBrowser: isCheckBrowser,
    isImageUploadOnly: isImageUploadOnly,
    isSupportMultipleFiles: isSupportMultipleFiles,
    autoFocus: autoFocus,
    editorApi: editorApi,
    onSaveEditorValue: onSaveEditorValue,
    onEditorValueChanged: onEditorValueChanged
  })));
});
var _default = exports.default = LongTextInlineEditor;