"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = TextEditorHeader;
var _react = _interopRequireWildcard(require("react"));
var _faTrashCan = require("@fortawesome/free-solid-svg-icons/faTrashCan");
var _useTranslation = require("../hooks/use-translation");
var _button = require("../button");
var _fontAwesomeIcon = require("../font-awesome-icon");
var _textEditorConstants = require("./text-editor-constants");
var _textEditorContext = _interopRequireDefault(require("./text-editor-context"));
var _textEditorService = _interopRequireDefault(require("./text-editor-service"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (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 (const 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 TextEditorHeader({
  title = '',
  titlePlaceholder = '',
  isEditable = true,
  maxLength = _textEditorConstants.HEADER_TITLE_MAX_LENGTH,
  onChangeTitle = () => {},
  onDelete = () => {},
  hasDeleteButton = true,
  deleteEditorContentButtonAriaLabel,
  inputAriaLabel
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const {
    slate,
    slateReact
  } = (0, _react.useContext)(_textEditorContext.default);
  const {
    ReactEditor
  } = slateReact;
  const textEditorService = new _textEditorService.default(slate, slateReact);
  const editor = slateReact.useSlate();
  const hasReadOnlyTitle = title && !isEditable;
  const hasEditableTitle = isEditable && onChangeTitle;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("header", {
    className: "text-editor-header",
    "test-id": "text-editor-header",
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "text-editor-header-content",
      children: [hasReadOnlyTitle && /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
        className: "text-editor-header-title",
        children: title
      }), hasEditableTitle && /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
        type: "text",
        value: title,
        maxLength: maxLength,
        placeholder: titlePlaceholder,
        className: "text-editor-header-title",
        "test-id": "text-editor-header-title-input",
        onChange: newTitle => {
          onChangeTitle(newTitle.target.value);
        },
        "aria-label": inputAriaLabel
      }), hasDeleteButton && /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
        onClick: () => {
          textEditorService.deleteContent(editor);
          ReactEditor.focus(editor);
          onChangeTitle('');
          onDelete();
        },
        accentType: _button.ACCENT_TYPES.ROUND_ICON,
        tooltipText: t('Delete'),
        className: "text-editor-header-delete",
        testId: "text-editor-header-delete",
        "aria-label": deleteEditorContentButtonAriaLabel,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
          icon: _faTrashCan.faTrashCan
        })
      })]
    })
  });
}