"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SidebarMenuItemEditForm = SidebarMenuItemEditForm;
var _react = _interopRequireWildcard(require("react"));
var _faCircleXmark = require("@fortawesome/free-solid-svg-icons/faCircleXmark");
var _input = require("../input");
var _useFocusWithin = require("../hooks/use-focus-within");
var _formField = require("../form-field");
var _jsxRuntime = require("react/jsx-runtime");
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 SidebarMenuItemEditForm({
  initValue,
  itemKey,
  onExit,
  onSubmit,
  onChange,
  level,
  validity,
  editItemInputPlaceholder
}) {
  const [value, setInputValue] = (0, _react.useState)(initValue);
  const hasError = validity?.type === _formField.VALIDATION_RULE_TYPES.ERROR && Boolean(validity?.value);
  const extraProps = {
    validity: {
      type: validity?.type,
      value: hasError
    },
    rightIcon: hasError ? _faCircleXmark.faCircleXmark : null
  };
  const validationValues = (0, _formField.useDefaultValidationValues)();
  const validityMessages = (0, _formField.getValidityMessages)({
    validity,
    validationValues
  }).join('. ');
  if (validityMessages) {
    extraProps.iconTooltipText = validityMessages;
  }
  const handleCancel = (0, _react.useCallback)(() => {
    setInputValue(initValue);
    onExit();
  }, [initValue]);
  const handleSubmit = (0, _react.useCallback)(() => {
    if (hasError) {
      return;
    }
    onSubmit({
      key: itemKey,
      val: value,
      level
    });
    onExit();
  }, [value, validity]);
  const handleKeyDown = (0, _react.useCallback)(e => {
    if (e.key === 'Esc' || e.key === 'Escape') {
      handleCancel();
    }
  }, [initValue]);
  const focusWithinProps = (0, _useFocusWithin.useFocusWithin)({
    onBlur: handleCancel
  });
  return (
    /*#__PURE__*/
    // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
    (0, _jsxRuntime.jsx)("form", {
      "test-id": `sidebar-menu-item-form-${itemKey}`,
      onKeyDown: handleKeyDown,
      onBlur: focusWithinProps.onBlur,
      onSubmit: e => {
        e.preventDefault();
        handleSubmit();
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_input.Input, {
        shouldDisplayControls: true,
        fieldClass: "sidebar-menu-item-input",
        className: "controllable-field",
        placeholder: editItemInputPlaceholder,
        autoFocus: true,
        value: value,
        onCancel: handleCancel,
        onChange: e => {
          setInputValue(e.target.value);
          onChange?.({
            key: itemKey,
            val: e.target.value,
            level
          });
        },
        fieldTestId: `sidebar-menu-item-input-${itemKey}`,
        onSubmit: handleSubmit,
        inputSize: _input.INPUT_SIZES.SM,
        ...extraProps
      })
    })
  );
}