Liferay.Loader.define("portal-workflow-metrics-web@2.0.42/js/components/filter/TimeRangeFilter.es", ['module', 'exports', 'require', 'frontend-js-react-web$react', '../../shared/components/filter/Filter.es', '../../shared/components/filter/hooks/useFilterName.es', '../../shared/components/filter/hooks/useFilterStatic.es', '../../shared/components/filter/util/filterConstants.es', '../../shared/components/filter/util/filterUtil.es', '../../shared/components/router/queryString.es', '../../shared/hooks/useFilter.es', '../../shared/hooks/useRouter.es', '../../shared/hooks/useRouterParams.es', '../../shared/hooks/useStorage.es', '../AppContext.es', './CustomTimeRangeForm.es', './hooks/useCustomFormState.es', './util/timeRangeUtil.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 _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _Filter = _interopRequireDefault(require("../../shared/components/filter/Filter.es"));

    var _useFilterName = require("../../shared/components/filter/hooks/useFilterName.es");

    var _useFilterStatic2 = require("../../shared/components/filter/hooks/useFilterStatic.es");

    var _filterConstants = _interopRequireDefault(require("../../shared/components/filter/util/filterConstants.es"));

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

    var _queryString = require("../../shared/components/router/queryString.es");

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

    var _useRouter = require("../../shared/hooks/useRouter.es");

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

    var _useStorage = require("../../shared/hooks/useStorage.es");

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

    var _CustomTimeRangeForm = require("./CustomTimeRangeForm.es");

    var _useCustomFormState2 = require("./hooks/useCustomFormState.es");

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

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

    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 _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 _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 TimeRangeFilter = function TimeRangeFilter(_ref) {
      var buttonClassName = _ref.buttonClassName,
          className = _ref.className,
          disabled = _ref.disabled,
          _ref$filterKey = _ref.filterKey,
          filterKey = _ref$filterKey === void 0 ? _filterConstants["default"].timeRange.key : _ref$filterKey,
          _ref$options = _ref.options,
          options = _ref$options === void 0 ? {} : _ref$options,
          _ref$prefixKey = _ref.prefixKey,
          prefixKey = _ref$prefixKey === void 0 ? '' : _ref$prefixKey;
      options = _objectSpread({
        hideControl: true,
        multiple: false,
        position: 'left',
        withSelectionTitle: true,
        withoutRouteParams: false
      }, options);

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

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

      var _useCustomFormState = (0, _useCustomFormState2.useCustomFormState)(),
          formVisible = _useCustomFormState.formVisible,
          onClickFilter = _useCustomFormState.onClickFilter,
          setFormVisible = _useCustomFormState.setFormVisible;

      var _useSessionStorage = (0, _useStorage.useSessionStorage)('timeRanges'),
          _useSessionStorage2 = _slicedToArray(_useSessionStorage, 1),
          _useSessionStorage2$ = _useSessionStorage2[0],
          storedTimeRanges = _useSessionStorage2$ === void 0 ? {} : _useSessionStorage2$;

      var _useFilter = (0, _useFilter2.useFilter)(options),
          dispatch = _useFilter.dispatch;

      var dateEndKey = (0, _filterUtil.getCapitalizedFilterKey)(prefixKey, 'dateEnd');
      var dateStartKey = (0, _filterUtil.getCapitalizedFilterKey)(prefixKey, 'dateStart');
      var prefixedFilterKey = (0, _filterUtil.getCapitalizedFilterKey)(prefixKey, filterKey);
      var routerProps = (0, _useRouter.useRouter)();
      var dateEnd = filters[dateEndKey];
      var dateStart = filters[dateStartKey];
      var customRange = (0, _react.useMemo)(function () {
        return (0, _timeRangeUtil.getCustomTimeRange)(dateEnd, dateStart);
      }, [dateEnd, dateStart]);
      var staticItems = (0, _react.useMemo)(function () {
        return (0, _timeRangeUtil.parseDateItems)(isAmPm)((0, _filterUtil.mergeItemsArray)([customRange], storedTimeRanges.items));
      }, [customRange, storedTimeRanges.items, isAmPm]);

      var _useFilterStatic = (0, _useFilterStatic2.useFilterStatic)({
        filterKey: filterKey,
        prefixKey: prefixKey,
        propertyKey: 'id',
        staticItems: staticItems,
        withoutRouteParams: options.withoutRouteParams
      }),
          items = _useFilterStatic.items,
          selectedItems = _useFilterStatic.selectedItems;

      var defaultItem = (0, _react.useMemo)(function () {
        return items.find(function (timeRange) {
          return timeRange.defaultTimeRange;
        });
      }, [items]);

      if (defaultItem && options.withSelectionTitle && !selectedItems.length) {
        selectedItems[0] = defaultItem;
      }

      var filterName = (0, _useFilterName.useFilterName)(options.multiple, selectedItems, Liferay.Language.get('completion-period'), options.withSelectionTitle);

      var handleSelectFilter = function handleSelectFilter(filter) {
        var filterValue = _defineProperty({}, prefixedFilterKey, [filter.key]);

        var query = (0, _queryString.parse)(routerProps.location.search);

        if (!options.withoutRouteParams) {
          var _objectSpread2;

          query.filters = _objectSpread({}, query.filters, (_objectSpread2 = {}, _defineProperty(_objectSpread2, dateEndKey, filter.dateEnd), _defineProperty(_objectSpread2, dateStartKey, filter.dateStart), _objectSpread2), filterValue);
          (0, _filterUtil.replaceHistory)((0, _queryString.stringify)(query), routerProps);
        } else {
          dispatch(filterValue);
        }
      };

      return _react["default"].createElement(_Filter["default"], _extends({
        buttonClassName: buttonClassName,
        defaultItem: defaultItem,
        disabled: disabled,
        elementClasses: className,
        filterKey: filterKey,
        items: items,
        name: filterName,
        onClickFilter: onClickFilter(handleSelectFilter),
        prefixKey: prefixKey,
        preventClick: true
      }, options), formVisible && _react["default"].createElement(_CustomTimeRangeForm.CustomTimeRangeForm, {
        handleSelectFilter: handleSelectFilter,
        items: items,
        prefixKey: prefixKey,
        setFormVisible: setFormVisible,
        withoutRouteParams: options.withoutRouteParams
      }));
    };

    var _default = TimeRangeFilter;
    exports["default"] = _default;
    //# sourceMappingURL=TimeRangeFilter.es.js.map
  }
});
//# sourceMappingURL=TimeRangeFilter.es.js.map