"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.DatePickerButton = exports.DateRangeDisplay = exports.dateRangeDisplayFactory = exports.formatIntervalForDisplay = void 0;

var R = _interopRequireWildcard(require("ramda"));

var _ChevronRightIcon = _interopRequireDefault(require("mdi-react/ChevronRightIcon"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _Button = require("../Button");

var _Icons = require("../Icons");

var _index = require("./index");

require("./DatePicker.css");

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; 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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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

var fromInternalDateFormat = _index._internal.fromInternalDateFormat,
    internalDateFormat = _index._internal.internalDateFormat;

var isAbsent = function isAbsent(val) {
  return R.isNil(val) || R.isEmpty(val);
};

var dash = "\u2013"; // Unicode Character 'EN DASH' (U+2013)

var nonBreakSpace = "\xA0"; // Unicode Character 'NO-BREAK SPACE' (U+00A0)

var toLocalizedFormat = R.curry(function (locale, dateTime) {
  return dateTime.toFormat('DD', {
    locale: locale
  });
});
var shortLocalizedDateFormatter = R.curry(function (toLocalizedFormat, date) {
  return isAbsent(date) ? "".concat(dash).concat(dash) : toLocalizedFormat(fromInternalDateFormat(date));
});
var formatIntervalForDisplay = R.curry(function (toLocalizedFormat, locale, interval) {
  if (isAbsent(interval)) {
    return "".concat(dash).concat(dash);
  }

  var _interval = _slicedToArray(interval, 2),
      start = _interval[0],
      end = _interval[1];

  var format = shortLocalizedDateFormatter(toLocalizedFormat(locale));
  var isSameDay = fromInternalDateFormat(end).diff(fromInternalDateFormat(start), 'day').days === 0;
  return isSameDay ? format(start) : "".concat(format(start)).concat(nonBreakSpace).concat(dash).concat(nonBreakSpace).concat(format(fromInternalDateFormat(end).toFormat(internalDateFormat)));
});
exports.formatIntervalForDisplay = formatIntervalForDisplay;

var dateRangeDisplayFactory = function dateRangeDisplayFactory(toLocalizedFormat) {
  return function DateRangeDisplay(_ref) {
    var locale = _ref.locale,
        ranges = _ref.ranges;
    var formatter = formatIntervalForDisplay(toLocalizedFormat, locale);

    var _ranges = _slicedToArray(ranges, 2),
        base = _ranges[0],
        comparison = _ranges[1];

    if (comparison !== undefined) {
      return _react.default.createElement("span", null, formatter(base), _react.default.createElement(_Icons.CompareIcon, null), formatter(comparison));
    } else {
      return _react.default.createElement("span", null, formatter(base));
    }
  };
};

exports.dateRangeDisplayFactory = dateRangeDisplayFactory;
var DateRangeDisplay = dateRangeDisplayFactory(toLocalizedFormat);
exports.DateRangeDisplay = DateRangeDisplay;

var DatePickerButton = function DatePickerButton(_ref2) {
  var expanded = _ref2.expanded,
      locale = _ref2.locale,
      ranges = _ref2.ranges,
      className = _ref2.className,
      DateRangeDisplay = _ref2.DateRangeDisplay,
      _ref2$type = _ref2.type,
      type = _ref2$type === void 0 ? 'inline-outline-secondary' : _ref2$type,
      restProps = _objectWithoutProperties(_ref2, ["expanded", "locale", "ranges", "className", "DateRangeDisplay", "type"]);

  return _react.default.createElement(_Button.Button, _extends({}, restProps, {
    className: (0, _classnames.default)(className, {
      'vs-date-picker-button-expanded': expanded
    }, 'vs-date-picker-button'),
    type: type
  }), _react.default.createElement(DateRangeDisplay, {
    locale: locale,
    ranges: ranges
  }), _react.default.createElement(_ChevronRightIcon.default, {
    className: "vs-expanded-icon"
  }));
};

exports.DatePickerButton = DatePickerButton;
DatePickerButton.propTypes = {
  expanded: _propTypes.default.bool,
  children: _propTypes.default.node
};
DatePickerButton.defaultProps = {
  DateRangeDisplay: DateRangeDisplay
};
var _default = DatePickerButton;
exports.default = _default;