"use strict";

exports.__esModule = true;
exports.DateInput = void 0;
var _react = _interopRequireWildcard(require("react"));
var _designSystem = require("@wix/design-system");
var _Field = require("../Field");
var _dataHooks = require("../dataHooks");
var _CustomFieldsFormContext = require("../../components/CustomFieldsFormContext");
var _datesUtil = require("../../utils/datesUtil");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/fields/DateInput/DateInput.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); }
const DateInput = ({
  dataHook,
  schema,
  initialValue,
  inputRef,
  onChange
}) => {
  const [date, setDate] = (0, _react.useState)(() => initialValue ? (0, _datesUtil.getDateWithoutTimezone)((0, _datesUtil.getISODateStringWithFixedTimezone)(initialValue)) : undefined);
  const ref = (0, _react.useRef)({});
  const [statusMessage, setStatusMessage] = (0, _react.useState)('');
  const formState = (0, _CustomFieldsFormContext.useCustomFieldsFormContext)();
  return /*#__PURE__*/_react.default.createElement(_Field.Field, {
    dataHook: dataHook,
    type: schema.type,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 32,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.FormField, {
    dataHook: _dataHooks.dataHooks.Label,
    label: schema.label,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 33,
      columnNumber: 7
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.DatePicker, {
    ref: internalRef => {
      inputRef == null || inputRef(ref.current);
      // TODO: PR to WSR needed to expose as public API
      // @ts-expect-error
      ref.current.focus = () => {
        var _internalRef$state;
        return internalRef == null || (_internalRef$state = internalRef.state) == null || (_internalRef$state = _internalRef$state.inputRef) == null || _internalRef$state.focus == null ? void 0 : _internalRef$state.focus();
      };
    },
    dataHook: _dataHooks.dataHooks.Input,
    width: "100%",
    value: date,
    onChange: newValue => {
      const formatedDate = (0, _datesUtil.getISODateStringWithFixedTimezone)(newValue);
      setDate(newValue);
      onChange(formatedDate.split('T')[0]);
    },
    onValidate: ({
      validationType,
      value,
      format
    }) => {
      if (validationType === 'valid') {
        setStatusMessage('');
        ref.current.invalid = false;
      } else if (validationType === 'formatError' && value === '') {
        setDate(undefined);
        onChange(null);
        setStatusMessage('');
        ref.current.invalid = false;
      } else {
        setStatusMessage(formState._translate('cairo.customFields.fieldType.date.invalid.value', {
          dateFormat: format ?? ''
        }));
        ref.current.invalid = true;
      }
    },
    status: ref.current.invalid ? 'error' : undefined,
    statusMessage: statusMessage,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 34,
      columnNumber: 9
    }
  })));
};
exports.DateInput = DateInput;
//# sourceMappingURL=DateInput.js.map