'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var initializers = require('@bigbinary/neeto-commons-frontend/initializers');
var utils = require('@bigbinary/neeto-commons-frontend/utils');
var Select = require('@bigbinary/neetoui/Select');
var DatePicker = require('@bigbinary/neetoui/DatePicker');
var ramda = require('ramda');
var i18next = require('i18next');
var jsxRuntime = require('react/jsx-runtime');

var defaultTimePeriodOptions = function defaultTimePeriodOptions() {
  var today = utils.dayjs().startOf("day");
  var startOfLastWeek = today.subtract(7, "day").startOf("week");
  var endOfLastWeek = today.subtract(7, "day").endOf("week");
  var lastMonthStart = today.subtract(1, "month").startOf("month");
  var lastMonthEnd = today.subtract(1, "month").endOf("month");
  var thisQuarterStart = utils.dayjs().startOf("quarter");
  var thisQuarterEnd = utils.dayjs().endOf("quarter");
  var lastQuarterStart = utils.dayjs().subtract(1, "quarter").startOf("quarter");
  var lastQuarterEnd = utils.dayjs().subtract(1, "quarter").endOf("quarter");
  var startOfLastYear = utils.dayjs().subtract(1, "year").startOf("year");
  var endOfLastYear = utils.dayjs().subtract(1, "year").endOf("year");
  var startOfYear = utils.dayjs().startOf("year");
  var endOfYear = utils.dayjs().endOf("year");
  var startOf24Hours = utils.dayjs().subtract(24, "hour");
  var now = utils.dayjs();
  var startOfLast7Days = utils.dayjs().subtract(7, "day");
  var startOfWeek = utils.dayjs().startOf("week");
  var startOfLast30Days = utils.dayjs().subtract(30, "day");
  var startOfMonth = today.startOf("month");
  return [{
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.allTime"),
    value: {
      startDate: null,
      endDate: null,
      rangeType: "all"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.last24Hours"),
    value: {
      startDate: startOf24Hours,
      endDate: now,
      rangeType: "hours"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.last7Days"),
    value: {
      startDate: startOfLast7Days,
      endDate: now,
      rangeType: "days"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.thisWeek"),
    value: {
      startDate: startOfWeek,
      endDate: now,
      rangeType: "week"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.lastWeek"),
    value: {
      startDate: startOfLastWeek,
      endDate: endOfLastWeek,
      rangeType: "week"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.last30Days"),
    value: {
      startDate: startOfLast30Days,
      endDate: now,
      rangeType: "days"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.thisMonth"),
    value: {
      startDate: startOfMonth,
      endDate: now,
      rangeType: "month"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.lastMonth"),
    value: {
      startDate: lastMonthStart,
      endDate: lastMonthEnd,
      rangeType: "month"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.thisQuarter"),
    value: {
      startDate: thisQuarterStart,
      endDate: thisQuarterEnd,
      rangeType: "quarter"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.lastQuarter"),
    value: {
      startDate: lastQuarterStart,
      endDate: lastQuarterEnd,
      rangeType: "quarter"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.thisYear"),
    value: {
      startDate: startOfYear,
      endDate: endOfYear,
      rangeType: "year"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.lastYear"),
    value: {
      startDate: startOfLastYear,
      endDate: endOfLastYear,
      rangeType: "year"
    }
  }, {
    label: i18next.t("neetoMolecules.dateRangeFilter.timePeriodOptions.customDuration"),
    value: "custom"
  }];
};

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var DateRangeFilter = function DateRangeFilter(_ref) {
  var _timePeriod$value, _timePeriod$value2, _timePeriod$value3, _globalProps$user;
  var timePeriodOptions = _ref.timePeriodOptions,
    timePeriod = _ref.timePeriod,
    setTimePeriod = _ref.setTimePeriod,
    datePickerProps = _ref.datePickerProps;
  var isCustom = (timePeriod === null || timePeriod === void 0 ? void 0 : timePeriod.value) === "custom" || (timePeriod === null || timePeriod === void 0 || (_timePeriod$value = timePeriod.value) === null || _timePeriod$value === void 0 ? void 0 : _timePeriod$value.rangeType) === "custom";
  var startDate = timePeriod === null || timePeriod === void 0 || (_timePeriod$value2 = timePeriod.value) === null || _timePeriod$value2 === void 0 ? void 0 : _timePeriod$value2.startDate;
  var endDate = timePeriod === null || timePeriod === void 0 || (_timePeriod$value3 = timePeriod.value) === null || _timePeriod$value3 === void 0 ? void 0 : _timePeriod$value3.endDate;
  var requiredDateFormat = initializers.globalProps === null || initializers.globalProps === void 0 || (_globalProps$user = initializers.globalProps.user) === null || _globalProps$user === void 0 ? void 0 : _globalProps$user.dateFormat;
  var options = timePeriodOptions || defaultTimePeriodOptions();
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: "neeto-molecules-date-range-filter flex flex-wrap gap-3",
    "data-testid": "date-range-filter",
    children: [/*#__PURE__*/jsxRuntime.jsx("div", {
      className: "neeto-molecules-date-range-filter__select w-40",
      children: /*#__PURE__*/jsxRuntime.jsx(Select, {
        options: options,
        required: true,
        getOptionValue: ramda.prop("label"),
        strategy: "fixed",
        value: timePeriod || options[0],
        onChange: function onChange(option) {
          return setTimePeriod(option);
        }
      })
    }), isCustom && /*#__PURE__*/jsxRuntime.jsx("div", {
      className: "neeto-molecules-date-range-filter__custom-duration-picker max-w-sm flex-grow",
      "data-testid": "date-range-filter-custom-duration-picker",
      children: /*#__PURE__*/jsxRuntime.jsx(DatePicker, _objectSpread({
        autoFocus: true,
        className: "w-full",
        dateFormat: requiredDateFormat,
        picker: "date",
        type: "range",
        value: [startDate ? utils.dayjs(startDate) : null, endDate ? utils.dayjs(endDate) : null],
        onChange: function onChange(_ref2) {
          var _ref3 = _slicedToArray(_ref2, 2),
            startDate = _ref3[0],
            endDate = _ref3[1];
          setTimePeriod({
            label: timePeriod === null || timePeriod === void 0 ? void 0 : timePeriod.label,
            value: {
              startDate: startDate,
              endDate: endDate,
              rangeType: "custom"
            }
          });
        }
      }, datePickerProps))
    })]
  });
};

module.exports = DateRangeFilter;
//# sourceMappingURL=DateRangeFilter.js.map
