Liferay.Loader.define("portal-workflow-metrics-web@2.0.46/js/components/instance-list-page/modal/update-due-date/UpdateDueDateStep.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/form', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/modal', 'frontend-js-react-web$classnames', 'frontend-js-react-web$react', 'portal-workflow-metrics-web$react-text-mask', '../../../../shared/util/date.es', '../../../../shared/util/util.es', '../../../AppContext.es', '../ModalProvider.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 _form = require("@frontend-taglib-clay$clayui/form");

    var _icon = _interopRequireDefault(require("@frontend-taglib-clay$clayui/icon"));

    var _modal = _interopRequireDefault(require("@frontend-taglib-clay$clayui/modal"));

    var _classnames = _interopRequireDefault(require("frontend-js-react-web$classnames"));

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

    var _reactTextMask = _interopRequireDefault(require("portal-workflow-metrics-web$react-text-mask"));

    var _date = require("../../../../shared/util/date.es");

    var _util = require("../../../../shared/util/util.es");

    var _AppContext = require("../../../AppContext.es");

    var _ModalProvider = require("../ModalProvider.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 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;
    }

    function _toConsumableArray(arr) {
      return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
    }

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

    function _iterableToArray(iter) {
      if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
    }

    function _arrayWithoutHoles(arr) {
      if (Array.isArray(arr)) {
        for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
          arr2[i] = arr[i];
        }return arr2;
      }
    }

    var getTimeOptions = function getTimeOptions(isAmPm) {
      var parse = function parse(number) {
        return number < 10 ? "0".concat(number) : number;
      };

      if (isAmPm) {
        var _times = {
          AM: ['12:00 AM', '12:30 AM'],
          PM: ['12:00 PM', '12:30 PM']
        };
        Object.keys(_times).forEach(function (type) {
          for (var i = 1; i < 12; i++) {
            _times[type].push("".concat(parse(i), ":00 ").concat(type));

            _times[type].push("".concat(parse(i), ":30 ").concat(type));
          }
        });
        return [].concat(_toConsumableArray(_times.AM), _toConsumableArray(_times.PM));
      }

      var times = [];

      for (var i = 0; i < 24; i++) {
        times.push("".concat(parse(i), ":00"));
        times.push("".concat(parse(i), ":30"));
      }

      return times;
    };

    var UpdateDueDateStep = function UpdateDueDateStep(_ref) {
      var className = _ref.className,
          _ref$dueDate = _ref.dueDate,
          dueDate = _ref$dueDate === void 0 ? new Date() : _ref$dueDate;

      var _useContext = (0, _react.useContext)(_AppContext.AppContext),
          isAmPm = _useContext.isAmPm;

      var _useContext2 = (0, _react.useContext)(_ModalProvider.ModalContext),
          setUpdateDueDate = _useContext2.setUpdateDueDate,
          updateDueDate = _useContext2.updateDueDate;

      var dateFormat = (0, _date.getLocaleDateFormat)();
      var timeFormat = (0, _date.getLocaleDateFormat)('LT');
      var dateMask = (0, _date.getMaskByDateFormat)(dateFormat);

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

      var _useState3 = (0, _react.useState)(''),
          _useState4 = _slicedToArray(_useState3, 2),
          comment = _useState4[0],
          setComment = _useState4[1];

      var _useState5 = (0, _react.useState)((0, _date.formatDate)(dueDate, dateFormat, _date.defaultDateFormat)),
          _useState6 = _slicedToArray(_useState5, 2),
          date = _useState6[0],
          setDate = _useState6[1];

      var _useState7 = (0, _react.useState)((0, _util.toUppercase)((0, _date.formatDate)(dueDate, timeFormat, _date.defaultDateFormat))),
          _useState8 = _slicedToArray(_useState7, 2),
          time = _useState8[0],
          setTime = _useState8[1];

      (0, _react.useEffect)(function () {
        var newDueDate = null;
        var validDate = (0, _date.isValidDate)(date, dateFormat);

        if (validDate && (0, _date.isValidDate)(time, timeFormat)) {
          var newDateTime = (0, _date.formatDate)("".concat(date, " ").concat(time), _date.defaultDateFormat, "".concat(dateFormat, " ").concat(timeFormat));
          newDueDate = (0, _date.isValidDate)(newDateTime, _date.defaultDateFormat) ? newDateTime : null;
        }

        setInvalidDate(!validDate);
        setUpdateDueDate(_objectSpread(_objectSpread({}, updateDueDate), {}, {
          dueDate: newDueDate
        })); // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [date, time]);
      (0, _react.useEffect)(function () {
        setUpdateDueDate(function (currentState) {
          return _objectSpread(_objectSpread({}, currentState), {}, {
            comment: comment
          });
        });
      }, [comment, setUpdateDueDate]);
      return (/*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('bg-white', className)
        }, /*#__PURE__*/_react.default.createElement(_modal.default.Body, null, /*#__PURE__*/_react.default.createElement("div", {
          className: "form-group-autofit"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "form-group-item ".concat(invalidDate && 'has-error')
        }, /*#__PURE__*/_react.default.createElement("label", {
          htmlFor: "dateInput"
        }, Liferay.Language.get('new-due-date'), ' ', /*#__PURE__*/_react.default.createElement("span", {
          className: "reference-mark"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: "asterisk"
        }))), /*#__PURE__*/_react.default.createElement(_reactTextMask.default, {
          className: "form-control",
          mask: dateMask,
          onChange: function onChange(_ref2) {
            var target = _ref2.target;
            return setDate(target.value);
          },
          placeholder: dateFormat,
          value: date
        })), /*#__PURE__*/_react.default.createElement(UpdateDueDateStep.TimePickerInput, {
          format: timeFormat,
          isAmPm: isAmPm,
          setValue: setTime,
          value: time
        })), /*#__PURE__*/_react.default.createElement("div", {
          className: "form-group-item mb-4"
        }, /*#__PURE__*/_react.default.createElement("label", {
          htmlFor: "commentTextArea"
        }, Liferay.Language.get('comment')), /*#__PURE__*/_react.default.createElement("textarea", {
          className: "form-control",
          onChange: function onChange(_ref3) {
            var target = _ref3.target;
            return setComment(target.value);
          },
          placeholder: Liferay.Language.get('write-a-note')
        }))))
      );
    };

    var TimePickerInputWithOptions = function TimePickerInputWithOptions(_ref4) {
      var format = _ref4.format,
          isAmPm = _ref4.isAmPm,
          setValue = _ref4.setValue,
          value = _ref4.value;

      var _useState9 = (0, _react.useState)(false),
          _useState10 = _slicedToArray(_useState9, 2),
          invalidTime = _useState10[0],
          setInvalidTime = _useState10[1];

      var _useState11 = (0, _react.useState)(false),
          _useState12 = _slicedToArray(_useState11, 2),
          showOptions = _useState12[0],
          setShowOptions = _useState12[1];

      var inputRef = (0, _react.useRef)();
      var options = (0, _react.useMemo)(function () {
        return getTimeOptions(isAmPm);
      }, [isAmPm]);
      var popoverStyle = (0, _react.useMemo)(function () {
        var _inputRef$current = inputRef.current;
        _inputRef$current = _inputRef$current === void 0 ? {} : _inputRef$current;
        var _inputRef$current$off = _inputRef$current.offsetWidth,
            offsetWidth = _inputRef$current$off === void 0 ? 270 : _inputRef$current$off;
        return {
          left: "".concat((offsetWidth - 120) / 2, "px")
        }; // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [inputRef.current]);
      (0, _react.useEffect)(function () {
        setInvalidTime(!(0, _date.isValidDate)(value, format));
      }, [format, value]);
      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "form-group-item form-group-item-label-spacer ".concat(invalidTime ? 'has-error' : '')
        }, /*#__PURE__*/_react.default.createElement(_form.ClayInput, {
          onBlur: function onBlur() {
            return setShowOptions(false);
          },
          onChange: function onChange(_ref5) {
            var target = _ref5.target;
            return setValue(target.value);
          },
          onFocus: function onFocus() {
            return setShowOptions(true);
          },
          placeholder: isAmPm ? 'HH:mm am/pm' : 'HH:mm',
          ref: inputRef,
          value: value
        }), showOptions && /*#__PURE__*/_react.default.createElement("div", {
          className: "clay-popover-bottom custom-time-select fade popover show",
          style: popoverStyle
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "arrow"
        }), /*#__PURE__*/_react.default.createElement("div", {
          className: "inline-scroller"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "popover-body"
        }, options.map(function (option, index) {
          return (/*#__PURE__*/_react.default.createElement("li", {
              key: index,
              onMouseDown: function onMouseDown() {
                return setValue(option);
              }
            }, option)
          );
        })))))
      );
    };

    UpdateDueDateStep.TimePickerInput = TimePickerInputWithOptions;
    var _default = UpdateDueDateStep;
    exports.default = _default;
    //# sourceMappingURL=UpdateDueDateStep.es.js.map
  }
});
//# sourceMappingURL=UpdateDueDateStep.es.js.map