import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import { translate } from '../../../utils';
import { DayRangePicker } from './DateTimePicker/DayRangePicker.js';
export var DateRangePickerWithPresets =
/*#__PURE__*/
function (_React$PureComponent) {
  _inheritsLoose(DateRangePickerWithPresets, _React$PureComponent);

  function DateRangePickerWithPresets() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;

    _defineProperty(_assertThisInitialized(_this), "getRanges", function () {
      var _ref;

      var _this$props = _this.props,
          todayText = _this$props.todayText,
          yesterdayText = _this$props.yesterdayText,
          days7Text = _this$props.days7Text,
          days30Text = _this$props.days30Text,
          days60Text = _this$props.days60Text;

      var _this$getLocalTime = _this.getLocalTime(),
          NOW = _this$getLocalTime.NOW,
          START_OF_YESTERDAY = _this$getLocalTime.START_OF_YESTERDAY,
          START_OF_7_DAYS_AGO = _this$getLocalTime.START_OF_7_DAYS_AGO,
          START_OF_30_DAYS_AGO = _this$getLocalTime.START_OF_30_DAYS_AGO,
          START_OF_60_DAYS_AGO = _this$getLocalTime.START_OF_60_DAYS_AGO;

      return _ref = {}, _ref[todayText] = {
        startDate: NOW,
        endDate: NOW
      }, _ref[yesterdayText] = {
        startDate: START_OF_YESTERDAY,
        endDate: START_OF_YESTERDAY
      }, _ref[days7Text] = {
        startDate: START_OF_7_DAYS_AGO,
        endDate: NOW
      }, _ref[days30Text] = {
        startDate: START_OF_30_DAYS_AGO,
        endDate: NOW
      }, _ref[days60Text] = {
        startDate: START_OF_60_DAYS_AGO,
        endDate: NOW
      }, _ref;
    });

    _defineProperty(_assertThisInitialized(_this), "defaultValueFormatter", function (startDate, endDate) {
      var _this$props2 = _this.props,
          days7Text = _this$props2.days7Text,
          days30Text = _this$props2.days30Text,
          days60Text = _this$props2.days60Text,
          todayDateText = _this$props2.todayDateText,
          yesterdayDateText = _this$props2.yesterdayDateText;

      var currentDate = _this.getCurrentDate();

      var isSelectOneDay = startDate.isSame(endDate, 'date');
      var isStartDateToday = startDate.isSame(currentDate, 'day');
      var isStartDateYesterday = startDate.clone().add(1, 'day').isSame(currentDate, 'day');

      var checkHowManyLastDaysSelected = function checkHowManyLastDaysSelected(daysAgo) {
        return _this.checkRangeDate({
          startDate: startDate,
          endDate: endDate,
          daysAgo: daysAgo,
          endRange: currentDate
        });
      };

      var longDateFormat = moment.localeData().longDateFormat('L');
      var formattedStartDate = startDate.format(longDateFormat);
      var formattedEndDate = endDate.format(longDateFormat);

      if (isSelectOneDay) {
        if (isStartDateToday) {
          return translate(todayDateText, {
            date: formattedStartDate
          });
        }

        if (isStartDateYesterday) {
          return translate(yesterdayDateText, {
            date: formattedStartDate
          });
        }

        return startDate.format('L');
      }

      if (checkHowManyLastDaysSelected(6)) {
        return days7Text;
      }

      if (checkHowManyLastDaysSelected(29)) {
        return days30Text;
      }

      if (checkHowManyLastDaysSelected(59)) {
        return days60Text;
      }

      return formattedStartDate + " - " + formattedEndDate;
    });

    return _this;
  }

  var _proto = DateRangePickerWithPresets.prototype;

  _proto.getCurrentDate = function getCurrentDate() {
    return moment.utc().zone(this.props.timeZoneOffset);
  };

  _proto.getLocalTime = function getLocalTime() {
    return {
      NOW: this.getCurrentDate(),
      START_OF_TODAY: this.getCurrentDate(),
      START_OF_YESTERDAY: this.getCurrentDate().add(-1, 'days'),
      START_OF_7_DAYS_AGO: this.getCurrentDate().add(-6, 'days'),
      START_OF_30_DAYS_AGO: this.getCurrentDate().add(-29, 'days'),
      START_OF_60_DAYS_AGO: this.getCurrentDate().add(-59, 'days')
    };
  };

  _proto.checkRangeDate = function checkRangeDate(_ref2) {
    var startDate = _ref2.startDate,
        endDate = _ref2.endDate,
        daysAgo = _ref2.daysAgo,
        endRange = _ref2.endRange;
    return startDate.clone().add(daysAgo, 'day').isSame(endDate, 'day') && endDate.isSame(endRange, 'day');
  };

  _proto.getValueFormatter = function getValueFormatter() {
    if (this.props.useDefaultValueFormatter) {
      return this.defaultValueFormatter;
    }

    return this.props.valueFormatter;
  };

  _proto.render = function render() {
    var _this$props3 = this.props,
        timeZoneOffset = _this$props3.timeZoneOffset,
        useDefaultValueFormatter = _this$props3.useDefaultValueFormatter,
        valueFormatter = _this$props3.valueFormatter,
        props = _objectWithoutPropertiesLoose(_this$props3, ["timeZoneOffset", "useDefaultValueFormatter", "valueFormatter"]);

    return React.createElement(DayRangePicker, _extends({}, props, {
      ranges: this.getRanges(),
      valueFormatter: this.getValueFormatter()
    }));
  };

  return DateRangePickerWithPresets;
}(React.PureComponent);

_defineProperty(DateRangePickerWithPresets, "propTypes", Object.assign({}, DayRangePicker.propTypes, {
  timeZoneOffset: PropTypes.number,
  useDefaultValueFormatter: PropTypes.bool,
  todayText: PropTypes.string,
  todayDateText: PropTypes.string,
  yesterdayText: PropTypes.string,
  yesterdayDateText: PropTypes.string,
  days7Text: PropTypes.string,
  days30Text: PropTypes.string,
  days60Text: PropTypes.string,
  days90Text: PropTypes.string,
  rangeText: PropTypes.string
}));

_defineProperty(DateRangePickerWithPresets, "defaultProps", {
  useDefaultValueFormatter: true,
  todayText: 'TRANSLATE:TODAY',
  todayDateText: 'TRANSLATE:TODAY_DATE',
  yesterdayText: 'TRANSLATE:YESTERDAY',
  yesterdayDateText: 'TRANSLATE:YESTERDAY_DATE',
  days7Text: 'TRANSLATE:DAYS7',
  days30Text: 'TRANSLATE:DAYS30',
  days60Text: 'TRANSLATE:DAYS60',
  days90Text: 'TRANSLATE:DAYS90',
  rangeText: 'TRANSLATE:RANGE',
  timeZoneOffset: 0
});