"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.renderCodeLine = exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _languageSelector = _interopRequireDefault(require("./language-selector"));
var _helpers = require("../helpers");
var _constant = require("./constant");
var _core = require("../../../core");
/* eslint-disable react-hooks/rules-of-hooks */

const renderCodeBlock = (_ref, editor) => {
  let {
    attributes,
    children,
    element
  } = _ref;
  const [isShowLanguageSelector, setIsShowLanguageSelector] = (0, _react.useState)(true);
  const codeBlockRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (!editor.selection) return;
    if (!(0, _helpers.isInCodeBlock)(editor)) onHideLanguageSelector();
    if ((0, _helpers.isInCodeBlock)(editor)) registerEventHandler();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [editor, editor.selection]);
  const onHideLanguageSelector = (0, _react.useCallback)(e => {
    var _codeBlockRef$current;
    if (codeBlockRef !== null && codeBlockRef !== void 0 && (_codeBlockRef$current = codeBlockRef.current) !== null && _codeBlockRef$current !== void 0 && _codeBlockRef$current.contains(e === null || e === void 0 ? void 0 : e.target) || (0, _helpers.isInCodeBlock)(editor)) return;
    setIsShowLanguageSelector(false);
    unregisterEventHandler();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [setIsShowLanguageSelector]);
  const registerEventHandler = (0, _react.useCallback)(() => {
    setIsShowLanguageSelector(true);
    document.addEventListener('click', onHideLanguageSelector, true);
  }, [onHideLanguageSelector]);
  const unregisterEventHandler = (0, _react.useCallback)(() => {
    document.removeEventListener('click', onHideLanguageSelector);
  }, [onHideLanguageSelector]);
  const handleLangSelectorChange = function () {
    let lang = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _constant.EXPLAIN_TEXT;
    const path = (0, _core.findPath)(editor, element);
    (0, _helpers.setCodeBlockLanguage)(editor, lang, path);
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    ref: codeBlockRef,
    className: "sf-code-block-container"
  }, /*#__PURE__*/_react.default.createElement("pre", attributes, /*#__PURE__*/_react.default.createElement("code", null, children)), isShowLanguageSelector && /*#__PURE__*/_react.default.createElement(_languageSelector.default, {
    lang: element.lang,
    handleLangSelectorChange: handleLangSelectorChange
  }));
};
var _default = exports.default = renderCodeBlock;
const renderCodeLine = (props, editor) => {
  const {
    element,
    attributes,
    children
  } = props;
  return /*#__PURE__*/_react.default.createElement("div", Object.assign({
    "data-id": element.id
  }, attributes, {
    className: 'sf-code-line'
  }), children);
};
exports.renderCodeLine = renderCodeLine;