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; }

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
AUI.add('liferay-search-modified-facet', function (A) {
  var DEFAULTS_FORM_VALIDATOR = A.config.FormValidator;
  var FacetUtil = Liferay.Search.FacetUtil;
  var Util = Liferay.Util;

  var ModifiedFacetFilter = function ModifiedFacetFilter(config) {
    var instance = this;
    instance.form = config.form;
    instance.fromInputDatePicker = config.fromInputDatePicker;
    instance.fromInputName = config.fromInputName;
    instance.namespace = config.namespace;
    instance.searchCustomRangeButton = config.searchCustomRangeButton;
    instance.toInputDatePicker = config.toInputDatePicker;
    instance.toInputName = config.toInputName;
    instance.fromInput = A.one('#' + instance.fromInputName);
    instance.toInput = A.one('#' + instance.toInputName);

    instance._initializeFormValidator();

    if (instance.searchCustomRangeButton) {
      instance.searchCustomRangeButton.on('click', A.bind(instance.filter, instance));
    }
  };

  var ModifiedFacetFilterUtil = {
    clearSelections: function clearSelections() {
      var param = this.getParameterName();
      var paramFrom = param + 'From';
      var paramTo = param + 'To';
      var parameterArray = document.location.search.substr(1).split('&');
      parameterArray = FacetUtil.removeURLParameters(param, parameterArray);
      parameterArray = FacetUtil.removeURLParameters(paramFrom, parameterArray);
      parameterArray = FacetUtil.removeURLParameters(paramTo, parameterArray);
      this.submitSearch(parameterArray.join('&'));
    },
    getParameterName: function getParameterName() {
      return 'modified';
    },
    submitSearch: function submitSearch(parameterString) {
      document.location.search = parameterString;
    },

    /**
     * Formats a date to 'YYYY-MM-DD' format.
     * @param {Date} date The date to format.
     * @returns {String} The date string.
     */
    toLocaleDateStringFormatted: function toLocaleDateStringFormatted(date) {
      var localDate = new Date(date);
      localDate.setMinutes(date.getMinutes() - date.getTimezoneOffset());
      return localDate.toISOString().split('T')[0];
    }
  };
  A.mix(ModifiedFacetFilter.prototype, {
    _initializeFormValidator: function _initializeFormValidator() {
      var _rules;

      var instance = this;
      var dateRangeRuleName = instance.namespace + 'dateRange';
      A.mix(DEFAULTS_FORM_VALIDATOR.STRINGS, _defineProperty({}, dateRangeRuleName, Liferay.Language.get('search-custom-range-invalid-date-range')), true);
      A.mix(DEFAULTS_FORM_VALIDATOR.RULES, _defineProperty({}, dateRangeRuleName, function () {
        return A.Date.isGreaterOrEqual(instance.toInputDatePicker.getDate(), instance.fromInputDatePicker.getDate());
      }), true);
      var customRangeValidator = new A.FormValidator({
        boundingBox: instance.form,
        fieldContainer: 'div',
        on: {
          errorField: function errorField() {
            Util.toggleDisabled(instance.searchCustomRangeButton, true);
          },
          validField: function validField() {
            Util.toggleDisabled(instance.searchCustomRangeButton, false);
          }
        },
        rules: (_rules = {}, _defineProperty(_rules, instance.fromInputName, _defineProperty({}, dateRangeRuleName, true)), _defineProperty(_rules, instance.toInputName, _defineProperty({}, dateRangeRuleName, true)), _rules)
      });

      var onRangeSelectionChange = function onRangeSelectionChange() {
        customRangeValidator.validate();
      };

      if (instance.fromInputDatePicker) {
        instance.fromInputDatePicker.on('selectionChange', onRangeSelectionChange);
      }

      if (instance.toInputDatePicker) {
        instance.toInputDatePicker.on('selectionChange', onRangeSelectionChange);
      }
    },
    filter: function filter() {
      var instance = this;
      var fromDate = instance.fromInputDatePicker.getDate();
      var toDate = instance.toInputDatePicker.getDate();
      var modifiedFromParameter = ModifiedFacetFilterUtil.toLocaleDateStringFormatted(fromDate);
      var modifiedToParameter = ModifiedFacetFilterUtil.toLocaleDateStringFormatted(toDate);
      var param = ModifiedFacetFilterUtil.getParameterName();
      var paramFrom = param + 'From';
      var paramTo = param + 'To';
      var parameterArray = document.location.search.substr(1).split('&');
      parameterArray = FacetUtil.removeURLParameters(param, parameterArray);
      parameterArray = FacetUtil.removeURLParameters(paramFrom, parameterArray);
      parameterArray = FacetUtil.removeURLParameters(paramTo, parameterArray);
      var startParameterNameElement = document.getElementById(instance.namespace + 'start-parameter-name');

      if (startParameterNameElement) {
        parameterArray = FacetUtil.removeURLParameters(startParameterNameElement.value, parameterArray);
      }

      parameterArray = FacetUtil.addURLParameter(paramFrom, modifiedFromParameter, parameterArray);
      parameterArray = FacetUtil.addURLParameter(paramTo, modifiedToParameter, parameterArray);
      ModifiedFacetFilterUtil.submitSearch(parameterArray.join('&'));
    }
  });
  Liferay.namespace('Search').ModifiedFacetFilter = ModifiedFacetFilter;
  Liferay.namespace('Search').ModifiedFacetFilterUtil = ModifiedFacetFilterUtil;
}, '', {
  requires: ['aui-form-validator', 'liferay-search-facet-util']
});
//# sourceMappingURL=modified_facet.js.map