"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TextEdit = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _patternsFields = require("@wix/patterns-fields");
var _useCloseOnAncestorScroll = require("../../../../hooks/useCloseOnAncestorScroll");
var _EditStCss = require("./Edit.st.css.js");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/EditableTable/cellTypes/text/Edit.tsx";
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 getMaxLength(validation) {
  var _validation$find;
  return validation == null || (_validation$find = validation.find(r => r.type === 'maxLength')) == null ? void 0 : _validation$find.value;
}
function getSpecificValues(validation) {
  var _validation$find2;
  return validation == null || (_validation$find2 = validation.find(r => r.type === 'specificValues')) == null ? void 0 : _validation$find2.values;
}

// Plain text mode
const TextEditPlain = ({
  value,
  onChange,
  onCommit,
  onCancel,
  inputRef,
  validation
}) => {
  const maxLength = getMaxLength(validation);
  const [isFocused, setIsFocused] = (0, _react.useState)(false);
  const handleKeyDown = (0, _react.useCallback)(e => {
    if (e.key === 'Enter' && !e.shiftKey) {
      e.preventDefault();
      onCommit();
    } else if (e.key === 'Escape') {
      e.preventDefault();
      onCancel();
    }
  }, [onCommit, onCancel]);
  const handleFocus = (0, _react.useCallback)(() => setIsFocused(true), []);
  const handleBlur = (0, _react.useCallback)(() => {
    setIsFocused(false);
    onCommit();
  }, [onCommit]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _EditStCss.classes.editWrapper,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 63,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_patternsFields.CellTextInput, {
    inputRef: inputRef,
    value: value ?? '',
    onChange: onChange,
    onFocus: handleFocus,
    onBlur: handleBlur,
    onKeyDown: handleKeyDown,
    maxLength: maxLength,
    className: (0, _EditStCss.st)(_EditStCss.classes.inputArea, {
      isFocused
    }),
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 64,
      columnNumber: 7
    }
  }));
};

// Specific values mode — autocomplete with a closed list of options
const TextEditSpecificValues = ({
  value,
  onChange,
  onCommit,
  onCancel,
  inputRef,
  editTrigger,
  options
}) => {
  const acHandle = (0, _react.useRef)(null);
  const wrapperRef = (0, _react.useRef)(null);
  const closeDropdown = (0, _react.useCallback)(() => {
    var _acHandle$current;
    return (_acHandle$current = acHandle.current) == null ? void 0 : _acHandle$current.hideOptions();
  }, []);
  (0, _useCloseOnAncestorScroll.useCloseOnAncestorScroll)(wrapperRef, closeDropdown, !!editTrigger);

  // Set in capture phase on ArrowUp/Down so onOptionMarked only updates
  // editingValue for explicit arrow navigation — not for auto-highlight on open.
  const isArrowNavRef = (0, _react.useRef)(false);
  const handleSelect = (0, _react.useCallback)(id => {
    onChange(id);
    onCommit();
  }, [onChange, onCommit]);
  const handleClear = (0, _react.useCallback)(() => onChange(''), [onChange]);
  const handleOptionMarked = (0, _react.useCallback)(id => {
    if (id && isArrowNavRef.current) {
      onChange(id);
      isArrowNavRef.current = false;
    }
  }, [onChange]);

  // Capture phase: set arrow-nav flag before AutoComplete fires onOptionMarked,
  // and cancel on Escape before the dropdown processes it.
  const handleKeyDownCapture = (0, _react.useCallback)(e => {
    if (e.key === 'Escape') {
      e.preventDefault();
      e.stopPropagation();
      onCancel();
    } else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
      isArrowNavRef.current = true;
    }
  }, [onCancel]);
  return /*#__PURE__*/_react.default.createElement("div", {
    ref: wrapperRef,
    className: _EditStCss.classes.editWrapper,
    onKeyDownCapture: handleKeyDownCapture,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 136,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_patternsFields.CellAutoCompleteInput, {
    ref: acHandle,
    value: value || undefined,
    options: options,
    initialSearchText: editTrigger && typeof editTrigger === 'object' && editTrigger.kind === 'type' ? editTrigger.key : undefined,
    autoOpen: !!editTrigger,
    onSelect: handleSelect,
    onClear: handleClear,
    onOptionMarked: handleOptionMarked,
    inputRef: inputRef,
    className: _EditStCss.classes.inputArea,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 141,
      columnNumber: 7
    }
  }));
};
const TextEdit = props => {
  const specificValues = getSpecificValues(props.validation);
  if (specificValues) {
    return /*#__PURE__*/_react.default.createElement(TextEditSpecificValues, (0, _extends2.default)({}, props, {
      options: specificValues,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 166,
        columnNumber: 12
      }
    }));
  }
  return /*#__PURE__*/_react.default.createElement(TextEditPlain, (0, _extends2.default)({}, props, {
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 168,
      columnNumber: 10
    }
  }));
};
exports.TextEdit = TextEdit;
//# sourceMappingURL=Edit.js.map