Liferay.Loader.define("portal-workflow-metrics-web@2.0.42/js/components/filter/hooks/useCustomTimeRange.es", ['module', 'exports', 'require', 'frontend-js-react-web$react', '../../../shared/components/filter/util/filterUtil.es', '../../../shared/hooks/useFilter.es', '../../../shared/hooks/useRouterParams.es', '../../../shared/util/date.es', '../../../shared/util/moment.es', '../util/timeRangeUtil.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.useCustomTimeRange = void 0;

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

    var _filterUtil = require("../../../shared/components/filter/util/filterUtil.es");

    var _useFilter2 = require("../../../shared/hooks/useFilter.es");

    var _useRouterParams2 = require("../../../shared/hooks/useRouterParams.es");

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

    var _moment = _interopRequireDefault(require("../../../shared/util/moment.es"));

    var _timeRangeUtil = require("../util/timeRangeUtil.es");

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { "default": 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 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;
    }

    var validateDate = function validateDate(dateEndMoment, dateStartMoment) {
      var dateNow = _moment["default"].utc();

      var errors;

      if (!dateEndMoment.isValid() || dateEndMoment.isAfter(dateNow)) {
        errors = updateErrors(errors, 'dateEnd', Liferay.Language.get('please-enter-a-valid-date'));
      }

      if (!dateStartMoment.isValid() || dateStartMoment.isAfter(dateNow)) {
        errors = updateErrors(errors, 'dateStart', Liferay.Language.get('please-enter-a-valid-date'));
      }

      return errors;
    };

    var validateEarlierDate = function validateEarlierDate(dateEndMoment, dateStartMoment) {
      var earlierDate = _moment["default"].utc().date(1).month(1).year(1970);

      var errors;

      if (dateEndMoment.isBefore(earlierDate)) {
        errors = updateErrors(errors, 'dateEnd', Liferay.Language.get('the-date-cannot-be-earlier-than-1970'));
      }

      if (dateStartMoment.isBefore(earlierDate)) {
        errors = updateErrors(errors, 'dateStart', Liferay.Language.get('the-date-cannot-be-earlier-than-1970'));
      }

      return errors;
    };

    var validateRangeConsistency = function validateRangeConsistency(dateEndMoment, dateStartMoment) {
      var errors;

      if (dateEndMoment.isBefore(dateStartMoment)) {
        errors = updateErrors(errors, 'dateEnd', Liferay.Language.get('the-end-date-cannot-be-earlier-than-the-start-date'));
      }

      if (dateStartMoment.isAfter(dateEndMoment)) {
        errors = updateErrors(errors, 'dateStart', Liferay.Language.get('the-start-date-cannot-be-later-than-the-end-date'));
      }

      return errors;
    };

    var updateErrors = function updateErrors(errors, fieldName, message) {
      return _objectSpread({}, errors || {}, _defineProperty({}, fieldName, message));
    };

    var useCustomTimeRange = function useCustomTimeRange(prefixKey, withoutRouteParams) {
      var _useState = (0, _react.useState)(undefined),
          _useState2 = _slicedToArray(_useState, 2),
          errors = _useState2[0],
          setErrors = _useState2[1];

      var _useRouterParams = (0, _useRouterParams2.useRouterParams)(),
          filters = _useRouterParams.filters;

      var _useFilter = (0, _useFilter2.useFilter)({
        withoutRouteParams: withoutRouteParams
      }),
          filterValues = _useFilter.filterValues;

      var dateEndKey = (0, _filterUtil.getCapitalizedFilterKey)(prefixKey, 'dateEnd');
      var dateFormat = (0, _date.getLocaleDateFormat)();
      var dateStartKey = (0, _filterUtil.getCapitalizedFilterKey)(prefixKey, 'dateStart');
      var values = !withoutRouteParams ? filters : filterValues;

      var _useState3 = (0, _react.useState)((0, _timeRangeUtil.convertQueryDate)(values[dateEndKey], dateFormat)),
          _useState4 = _slicedToArray(_useState3, 2),
          dateEnd = _useState4[0],
          setDateEnd = _useState4[1];

      var _useState5 = (0, _react.useState)((0, _timeRangeUtil.convertQueryDate)(values[dateStartKey], dateFormat)),
          _useState6 = _slicedToArray(_useState5, 2),
          dateStart = _useState6[0],
          setDateStart = _useState6[1];

      return {
        applyCustomFilter: function applyCustomFilter(handleApply) {
          var _ref = errors || {},
              dateEndError = _ref.dateEnd,
              dateStartError = _ref.dateStart;

          if (!dateEndError && !dateStartError) {
            handleApply({
              dateEnd: (0, _timeRangeUtil.formatDateTime)(dateEnd, dateFormat, true),
              dateStart: (0, _timeRangeUtil.formatDateTime)(dateStart, dateFormat),
              key: 'custom'
            });
          }
        },
        dateEnd: dateEnd,
        dateFormat: dateFormat,
        dateStart: dateStart,
        errors: errors,
        setDateEnd: setDateEnd,
        setDateStart: setDateStart,
        validate: function validate() {
          var dateEndMoment = (0, _timeRangeUtil.parseDateMoment)(dateEnd, dateFormat);
          var dateStartMoment = (0, _timeRangeUtil.parseDateMoment)(dateStart, dateFormat);

          var errors = _objectSpread({}, validateDate(dateEndMoment, dateStartMoment), {}, validateEarlierDate(dateEndMoment, dateStartMoment), {}, validateRangeConsistency(dateEndMoment, dateStartMoment));

          setErrors(errors);
          return errors;
        }
      };
    };

    exports.useCustomTimeRange = useCustomTimeRange;
    //# sourceMappingURL=useCustomTimeRange.es.js.map
  }
});
//# sourceMappingURL=useCustomTimeRange.es.js.map