"use strict";

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.Datepicker = void 0;

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

var _reactIntl = require("react-intl");

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _DayPickerInput = _interopRequireDefault(require("react-day-picker/DayPickerInput"));

var _datepicker = require("./datepicker.css-rules");

var _box = require("../../primitives/box");

var _fieldLabel = require("../../core/typography/field-label");

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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

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

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

// we put the date picker styles here so it's specific to this instance rather than globally
var StyledBox = (0, _styledComponents["default"])(_box.Box).withConfig({
  displayName: "StyledBox",
  componentId: "sc-1m8yi3g-0"
})(["&&{", ";}"], _datepicker.datePickerStyles);
var Datepicker = (0, _reactIntl.injectIntl)(function (_ref) {
  var intl = _ref.intl,
      label = _ref.label,
      onChange = _ref.onChange,
      value = _ref.value,
      _ref$disabledDates = _ref.disabledDates,
      disabledDates = _ref$disabledDates === void 0 ? [] : _ref$disabledDates,
      _ref$disableWeekends = _ref.disableWeekends,
      disableWeekends = _ref$disableWeekends === void 0 ? false : _ref$disableWeekends,
      _ref$disablePastDates = _ref.disablePastDates,
      disablePastDates = _ref$disablePastDates === void 0 ? false : _ref$disablePastDates,
      otherProps = _objectWithoutProperties(_ref, ["intl", "label", "onChange", "value", "disabledDates", "disableWeekends", "disablePastDates"]);

  var formatDate = (0, _react.useCallback)(function () {
    return intl.formatDate(value, {
      month: 'short',
      day: 'numeric',
      year: 'numeric'
    });
  }, // Format based on locale
  [value, intl]);
  var parseDate = (0, _react.useCallback)(function () {
    return value ? new Date(value) : undefined;
  }, [value]);
  var handleDayChange = (0, _react.useCallback)(function (day) {
    onChange(day);
  }, [onChange]);
  return _react["default"].createElement(StyledBox, _extends({
    stacked: "column"
  }, otherProps), _react["default"].createElement(_fieldLabel.FieldLabel, null, label), _react["default"].createElement(_DayPickerInput["default"], {
    value: value,
    onDayChange: handleDayChange,
    formatDate: formatDate,
    parseDate: parseDate,
    dayPickerProps: _objectSpread({
      disabledDays: [].concat(_toConsumableArray(disabledDates), [_objectSpread({}, disableWeekends ? {
        daysOfWeek: [0, 6]
      } : {}), _objectSpread({}, disablePastDates ? {
        before: new Date()
      } : {})])
    }, disablePastDates ? {
      fromMonth: new Date()
    } : {}, {
      showOutsideDays: true,
      enableOutsideDaysClick: true
    })
  }));
});
exports.Datepicker = Datepicker;
Datepicker.displayName = 'Datepicker';