Liferay.Loader.define("dynamic-data-mapping-form-field-type@5.0.83/DatePicker/DatePicker.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/date-picker', 'dynamic-data-mapping-form-field-type$moment/min/moment-with-locales', 'frontend-js-react-web$react', 'dynamic-data-mapping-form-field-type$text-mask-addons', 'dynamic-data-mapping-form-field-type$text-mask-core', '../FieldBase/ReactFieldBase.es', '../hooks/useSyncValue.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

    var _datePicker = _interopRequireDefault(require("@frontend-taglib-clay$clayui/date-picker"));

    var _momentWithLocales = _interopRequireDefault(require("dynamic-data-mapping-form-field-type$moment/min/moment-with-locales"));

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _textMaskAddons = require("dynamic-data-mapping-form-field-type$text-mask-addons");

    var _textMaskCore = require("dynamic-data-mapping-form-field-type$text-mask-core");

    var _ReactFieldBase = require("../FieldBase/ReactFieldBase.es");

    var _useSyncValue3 = require("../hooks/useSyncValue.es");

    function _getRequireWildcardCache() {
      if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function _getRequireWildcardCache() {
        return cache;
      };return cache;
    }

    function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { default: obj };
      }var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }newObj.default = obj;if (cache) {
        cache.set(obj, newObj);
      }return newObj;
    }

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function _extends() {
      _extends = Object.assign || function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }return target;
      };return _extends.apply(this, arguments);
    }

    function _objectWithoutProperties(source, excluded) {
      if (source == null) return {};var target = _objectWithoutPropertiesLoose(source, excluded);var key, i;if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);for (i = 0; i < sourceSymbolKeys.length; i++) {
          key = sourceSymbolKeys[i];if (excluded.indexOf(key) >= 0) continue;if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;target[key] = source[key];
        }
      }return target;
    }

    function _objectWithoutPropertiesLoose(source, excluded) {
      if (source == null) return {};var target = {};var sourceKeys = Object.keys(source);var key, i;for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];if (excluded.indexOf(key) >= 0) continue;target[key] = source[key];
      }return target;
    }

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return obj;
    }

    function _slicedToArray(arr, i) {
      return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
    }

    function _nonIterableRest() {
      throw new TypeError("Invalid attempt to destructure non-iterable instance");
    }

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;_e = err;
      } finally {
        try {
          if (!_n && _i["return"] != null) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }return _arr;
    }

    function _arrayWithHoles(arr) {
      if (Array.isArray(arr)) return arr;
    }

    var DIGIT_REGEX = /\d/i;
    var LETTER_REGEX = /[a-z]/i;
    var NOT_LETTER_REGEX = /[^a-z]/gi;

    var getDateMask = function getDateMask(dateDelimiter, dateFormat) {
      var lastSymbol = dateFormat.slice(-1).match(NOT_LETTER_REGEX);
      dateFormat = lastSymbol ? dateFormat.slice(0, -1) : dateFormat;
      return dateFormat.split(dateDelimiter).map(function (item) {
        var currentFormat;

        if (item === 'YYYY') {
          currentFormat = 'yyyy';
        } else if (item === 'DD') {
          currentFormat = 'dd';
        } else {
          currentFormat = 'MM';
        }

        return currentFormat;
      }).join(dateDelimiter);
    };

    var getDelimiter = function getDelimiter(dateFormat) {
      var dateDelimiter = '/';

      if (dateFormat.indexOf('.') != -1) {
        dateDelimiter = '.';
      }

      if (dateFormat.indexOf('-') != -1) {
        dateDelimiter = '-';
      }

      return dateDelimiter;
    };

    var getLocaleDateFormat = function getLocaleDateFormat(locale) {
      var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'L';

      _momentWithLocales.default.locale(locale);

      return _momentWithLocales.default.localeData().longDateFormat(format);
    };

    var getMaskByDateFormat = function getMaskByDateFormat(format) {
      var mask = [];

      for (var i = 0; i < format.length; i++) {
        if (LETTER_REGEX.test(format[i])) {
          mask.push(DIGIT_REGEX);
        } else {
          mask.push("".concat(format[i]));
        }
      }

      return mask;
    };

    var getDateFormat = function getDateFormat(locale) {
      var dateFormat = getLocaleDateFormat(locale);
      var inputMask = getMaskByDateFormat(dateFormat);
      var dateDelimiter = getDelimiter(inputMask);
      return {
        dateMask: getDateMask(dateDelimiter, dateFormat),
        inputMask: inputMask
      };
    };

    var transformToDate = function transformToDate(defaultLanguageId, date, locale, formatInEditingLocale) {
      if (typeof date === 'string' && date.indexOf('_') === -1 && date !== '') {
        if (formatInEditingLocale) {
          return (0, _momentWithLocales.default)(date, [getLocaleDateFormat(locale), 'YYYY-MM-DD']).toDate();
        }

        return (0, _momentWithLocales.default)(date, [getLocaleDateFormat(defaultLanguageId), 'YYYY-MM-DD']).toDate();
      }

      return date;
    };

    var getInitialMonth = function getInitialMonth(value) {
      if ((0, _momentWithLocales.default)(value).isValid()) {
        return (0, _momentWithLocales.default)(value).toDate();
      }

      return (0, _momentWithLocales.default)().toDate();
    };

    var getValueForHidden = function getValueForHidden(value) {
      if ((0, _momentWithLocales.default)(value).isValid()) {
        return (0, _momentWithLocales.default)(value).format('YYYY-MM-DD');
      }

      return '';
    };

    var Months = [Liferay.Language.get('january'), Liferay.Language.get('february'), Liferay.Language.get('march'), Liferay.Language.get('april'), Liferay.Language.get('may'), Liferay.Language.get('june'), Liferay.Language.get('july'), Liferay.Language.get('august'), Liferay.Language.get('september'), Liferay.Language.get('october'), Liferay.Language.get('november'), Liferay.Language.get('december')];
    var WeekdayShort = [Liferay.Language.get('weekday-short-sunday'), Liferay.Language.get('weekday-short-monday'), Liferay.Language.get('weekday-short-tuesday'), Liferay.Language.get('weekday-short-wednesday'), Liferay.Language.get('weekday-short-thursday'), Liferay.Language.get('weekday-short-friday'), Liferay.Language.get('weekday-short-saturday')];

    var DatePicker = function DatePicker(_ref) {
      var defaultLanguageId = _ref.defaultLanguageId,
          disabled = _ref.disabled,
          formatInEditingLocale = _ref.formatInEditingLocale,
          locale = _ref.locale,
          name = _ref.name,
          onBlur = _ref.onBlur,
          onChange = _ref.onChange,
          onFocus = _ref.onFocus,
          spritemap = _ref.spritemap,
          initialValue = _ref.value;
      var inputRef = (0, _react.useRef)(null);
      var maskInstance = (0, _react.useRef)(null);

      var _useState = (0, _react.useState)(false),
          _useState2 = _slicedToArray(_useState, 2),
          expanded = _useState2[0],
          setExpand = _useState2[1];

      var _useState3 = (0, _react.useState)({}),
          _useState4 = _slicedToArray(_useState3, 2),
          localizedValue = _useState4[0],
          setLocalizedValue = _useState4[1];

      var initialValueMemoized = (0, _react.useMemo)(function () {
        return transformToDate(defaultLanguageId, initialValue, locale, formatInEditingLocale);
      }, [defaultLanguageId, formatInEditingLocale, initialValue, locale]);

      var _useSyncValue = (0, _useSyncValue3.useSyncValue)(initialValueMemoized),
          _useSyncValue2 = _slicedToArray(_useSyncValue, 2),
          value = _useSyncValue2[0],
          setValue = _useSyncValue2[1];

      var _useState5 = (0, _react.useState)(function () {
        var currentYear = new Date().getFullYear();
        return {
          end: currentYear + 5,
          start: currentYear - 5
        };
      }),
          _useState6 = _slicedToArray(_useState5, 2),
          years = _useState6[0],
          setYears = _useState6[1];

      var _getDateFormat = getDateFormat(locale),
          dateMask = _getDateFormat.dateMask,
          inputMask = _getDateFormat.inputMask;

      (0, _react.useEffect)(function () {
        if (inputRef.current && inputMask && dateMask) {
          maskInstance.current = (0, _textMaskCore.createTextMaskInputElement)({
            guide: true,
            inputElement: inputRef.current,
            keepCharPositions: true,
            mask: inputMask,
            pipe: (0, _textMaskAddons.createAutoCorrectedDatePipe)(dateMask.toLowerCase()),
            showMask: true
          });
          var currentValue = localizedValue[locale];

          if (currentValue) {
            var _currentValue = localizedValue[locale];

            if (_currentValue) {
              inputRef.current.value = /_/.test(_currentValue) && _currentValue.match(DIGIT_REGEX) ? _currentValue : (0, _momentWithLocales.default)(_currentValue).format(dateMask.toUpperCase());
            }
          } else if (initialValueMemoized) {
            inputRef.current.value = (0, _momentWithLocales.default)(initialValueMemoized).format(dateMask.toUpperCase());
          } else {
            inputRef.current.value = '';
          }

          maskInstance.current.update(inputRef.current.value);
        }
      }, [dateMask, inputMask, inputRef, initialValueMemoized, localizedValue, locale]);

      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("input", {
          "aria-hidden": "true",
          id: name + '_fieldDetails',
          name: name,
          type: "hidden",
          value: getValueForHidden(value)
        }), /*#__PURE__*/_react.default.createElement(_datePicker.default, {
          "aria-labelledby": name + '_fieldDetails',
          dateFormat: dateMask,
          disabled: disabled,
          expanded: expanded,
          initialMonth: getInitialMonth(value),
          months: Months,
          onBlur: onBlur,
          onExpandedChange: function onExpandedChange(expand) {
            setExpand(expand);
          },
          onFocus: onFocus,
          onInput: function onInput(event) {
            maskInstance.current.update(event.target.value);
            setLocalizedValue(_objectSpread(_objectSpread({}, localizedValue), {}, _defineProperty({}, locale, event.target.value)));
          },
          onNavigation: function handleNavigation(date) {
            var currentYear = date.getFullYear();
            setYears({
              end: currentYear + 5,
              start: currentYear - 5
            });
          },
          onValueChange: function onValueChange(value, eventType) {
            setLocalizedValue(_objectSpread(_objectSpread({}, localizedValue), {}, _defineProperty({}, locale, value)));
            setValue(value);

            if (eventType === 'click') {
              setExpand(false);
              inputRef.current.focus();
            }

            if (!value || value === maskInstance.current.state.previousPlaceholder) {
              return onChange('');
            }

            if ((0, _momentWithLocales.default)(value, getLocaleDateFormat(locale), true).isValid()) {
              onChange((0, _momentWithLocales.default)(value, getLocaleDateFormat(locale)).format('L'));
            }
          },
          ref: inputRef,
          spritemap: spritemap,
          value: value,
          weekdaysShort: WeekdayShort,
          years: years
        }))
      );
    };

    var Main = function Main(_ref2) {
      var defaultLanguageId = _ref2.defaultLanguageId,
          _ref2$locale = _ref2.locale,
          locale = _ref2$locale === void 0 ? themeDisplay.getDefaultLanguageId() : _ref2$locale,
          localizedValue = _ref2.localizedValue,
          name = _ref2.name,
          onBlur = _ref2.onBlur,
          _onChange = _ref2.onChange,
          onFocus = _ref2.onFocus,
          placeholder = _ref2.placeholder,
          predefinedValue = _ref2.predefinedValue,
          readOnly = _ref2.readOnly,
          spritemap = _ref2.spritemap,
          value = _ref2.value,
          otherProps = _objectWithoutProperties(_ref2, ["defaultLanguageId", "locale", "localizedValue", "name", "onBlur", "onChange", "onFocus", "placeholder", "predefinedValue", "readOnly", "spritemap", "value"]);

      return (/*#__PURE__*/_react.default.createElement(_ReactFieldBase.FieldBase, _extends({}, otherProps, {
          localizedValue: localizedValue,
          name: name,
          readOnly: readOnly,
          spritemap: spritemap
        }), /*#__PURE__*/_react.default.createElement(DatePicker, {
          defaultLanguageId: defaultLanguageId,
          disabled: readOnly,
          formatInEditingLocale: localizedValue && localizedValue[locale] != undefined,
          locale: locale,
          name: name,
          onBlur: onBlur,
          onChange: function onChange(value) {
            return _onChange({}, value);
          },
          onFocus: onFocus,
          placeholder: placeholder,
          spritemap: spritemap,
          value: value ? value : predefinedValue
        }))
      );
    };

    Main.displayName = 'DatePicker';
    var _default = Main;
    exports.default = _default;
    //# sourceMappingURL=DatePicker.es.js.map
  }
});
//# sourceMappingURL=DatePicker.es.js.map