"use strict";

exports.__esModule = true;
exports.DateEdit = void 0;
var _react = _interopRequireWildcard(require("react"));
var _designSystem = require("@wix/design-system");
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/date/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 isValidDate(value) {
  if (!value) {
    return true;
  }
  const date = value instanceof Date ? value : new Date(value);
  return !isNaN(date.getTime());
}
const DateEdit = ({
  value,
  onChange,
  onCommit,
  onCancel,
  inputRef,
  editTrigger
}) => {
  const datePickerRef = (0, _react.useRef)(null);
  const validDate = isValidDate(value);

  // Auto-open calendar when entering edit mode via type-to-edit
  (0, _react.useEffect)(() => {
    if (editTrigger && typeof editTrigger === 'object' && editTrigger.kind === 'type') {
      var _datePickerRef$curren;
      (_datePickerRef$curren = datePickerRef.current) == null || _datePickerRef$curren.openCalendar();
    }
  }, [editTrigger]);
  const wrapperRef = (0, _react.useRef)(null);
  const closeCalendar = (0, _react.useCallback)(() => (0, _patternsFields.closeDatePickerCalendar)(datePickerRef.current), []);
  (0, _useCloseOnAncestorScroll.useCloseOnAncestorScroll)(wrapperRef, closeCalendar, !!editTrigger);
  const dateValue = value ? value instanceof Date ? value : new Date(value) : null;
  const handleChange = (0, _react.useCallback)(date => {
    onChange(date);
    onCommit();
  }, [onChange, onCommit]);
  const handleKeyDown = (0, _react.useCallback)(e => {
    if (e.key === 'Escape') {
      e.preventDefault();
      onCancel();
    }
  }, [onCancel]);
  return /*#__PURE__*/_react.default.createElement("div", {
    ref: wrapperRef,
    onKeyDown: handleKeyDown,
    style: {
      width: '100%',
      height: '100%',
      display: 'flex',
      alignItems: 'center'
    },
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 70,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.DatePicker, {
    ref: datePickerRef,
    size: "small",
    inputProps: {
      border: 'none',
      prefix: /*#__PURE__*/_react.default.createElement("span", {
        __self: void 0,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 85,
          columnNumber: 19
        }
      }),
      ...(validDate ? {} : {
        value: typeof value === 'string' ? value : ''
      }),
      inputRef: el => {
        if (inputRef) {
          inputRef.current = el;
        }
      }
    },
    value: dateValue && !isNaN(dateValue.getTime()) ? dateValue : undefined,
    onChange: handleChange,
    popoverProps: {
      appendTo: 'window',
      moveBy: {
        y: 4
      }
    },
    className: _EditStCss.classes.dateInput,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 80,
      columnNumber: 7
    }
  }));
};
exports.DateEdit = DateEdit;
//# sourceMappingURL=Edit.js.map