"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

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

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

var _Calendar = _interopRequireWildcard(require("./Calendar"));

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

var _CustomProvider = require("../CustomProvider");

var _utils = require("../utils");

var defaultProps = {
  defaultValue: new Date(),
  classPrefix: 'calendar',
  as: _Calendar.default
};

var CalendarPanel = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var _ref;

  var Component = props.as,
      bordered = props.bordered,
      className = props.className,
      classPrefix = props.classPrefix,
      compact = props.compact,
      defaultValue = props.defaultValue,
      isoWeek = props.isoWeek,
      overrideLocale = props.locale,
      onChange = props.onChange,
      onSelect = props.onSelect,
      renderCell = props.renderCell,
      timeZone = props.timeZone,
      valueProp = props.value,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "bordered", "className", "classPrefix", "compact", "defaultValue", "isoWeek", "locale", "onChange", "onSelect", "renderCell", "timeZone", "value"]);

  var _useState = (0, _react.useState)(_utils.TimeZone.toTimeZone(valueProp !== null && valueProp !== void 0 ? valueProp : defaultValue, timeZone)),
      value = _useState[0],
      updateValue = _useState[1];

  var _useState2 = (0, _react.useState)(_utils.TimeZone.toTimeZone((_ref = valueProp !== null && valueProp !== void 0 ? valueProp : defaultValue) !== null && _ref !== void 0 ? _ref : new Date(), timeZone)),
      pageDate = _useState2[0],
      setPageDate = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      showMonth = _useState3[0],
      setShowMonth = _useState3[1];

  var _useCustom = (0, _utils.useCustom)('Calendar', overrideLocale),
      locale = _useCustom.locale;

  var prevValueRef = (0, _react.useRef)(value);
  var prevTimeZoneRef = (0, _react.useRef)(timeZone);
  var setValue = (0, _react.useCallback)(function (nextValue) {
    updateValue(function (prevValue) {
      prevValueRef.current = prevValue;
      return nextValue;
    });
  }, []);
  (0, _react.useEffect)(function () {
    var nextValue = _utils.TimeZone.toTimeZone(valueProp !== null && valueProp !== void 0 ? valueProp : _utils.TimeZone.toLocalTimeZone(prevValueRef.current, prevTimeZoneRef.current), timeZone);

    prevTimeZoneRef.current = timeZone;
    setValue(nextValue);
    setPageDate(nextValue !== null && nextValue !== void 0 ? nextValue : _utils.TimeZone.zonedDate(timeZone));
  }, [setValue, timeZone, valueProp]);
  var handleToggleMonthDropdown = (0, _react.useCallback)(function () {
    setShowMonth(function (prevShowMonth) {
      return !prevShowMonth;
    });
  }, []);
  var handleChange = (0, _react.useCallback)(function (nextValue) {
    setValue(nextValue);
    setPageDate((0, _utils.composeFunctions)(function (d) {
      return _utils.DateUtils.setHours(d, _utils.DateUtils.getHours(pageDate));
    }, function (d) {
      return _utils.DateUtils.setMinutes(d, _utils.DateUtils.getMinutes(pageDate));
    }, function (d) {
      return _utils.DateUtils.setSeconds(d, _utils.DateUtils.getSeconds(pageDate));
    })(nextValue));
    onChange === null || onChange === void 0 ? void 0 : onChange(_utils.TimeZone.toLocalTimeZone(nextValue, timeZone));
  }, [onChange, pageDate, setValue, timeZone]);
  var handleChangePageDate = (0, _react.useCallback)(function (nextValue) {
    setShowMonth(false);
    handleChange(nextValue);
  }, [handleChange]);
  var handleClickToday = (0, _react.useCallback)(function () {
    var nextValue = _utils.TimeZone.zonedDate(timeZone);

    setShowMonth(false);
    handleChange(nextValue);
  }, [handleChange, timeZone]);
  var handleSelect = (0, _react.useCallback)(function (nextValue) {
    onSelect === null || onSelect === void 0 ? void 0 : onSelect(_utils.TimeZone.toLocalTimeZone(nextValue, timeZone));
    handleChange(nextValue);
  }, [handleChange, onSelect, timeZone]);

  var _useClassNames = (0, _utils.useClassNames)(classPrefix),
      prefix = _useClassNames.prefix,
      merge = _useClassNames.merge,
      withClassPrefix = _useClassNames.withClassPrefix;

  var renderToolbar = (0, _react.useCallback)(function () {
    return /*#__PURE__*/_react.default.createElement(_Button.default, {
      className: prefix('btn-today'),
      size: "sm",
      onClick: handleClickToday
    }, locale.today || 'Today');
  }, [handleClickToday, locale.today, prefix]);
  var customRenderCell = (0, _react.useCallback)(function (date) {
    return renderCell === null || renderCell === void 0 ? void 0 : renderCell(_utils.TimeZone.toLocalTimeZone(date, timeZone));
  }, [renderCell, timeZone]);
  var classes = merge(className, withClassPrefix('panel', {
    bordered: bordered,
    compact: compact
  }));
  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, rest, {
    inline: true,
    className: classes,
    ref: ref,
    isoWeek: isoWeek,
    format: "yyyy-MM-dd",
    calendarState: showMonth ? _Calendar.CalendarState.DROP_MONTH : null,
    pageDate: pageDate,
    timeZone: timeZone,
    limitEndYear: 1000,
    locale: locale,
    renderTitle: function renderTitle(date) {
      return /*#__PURE__*/_react.default.createElement(_CustomProvider.FormattedDate, {
        date: date,
        formatStr: locale.formattedMonthPattern || 'MMMM  yyyy'
      });
    },
    renderToolbar: renderToolbar,
    renderCell: customRenderCell,
    onMoveForward: handleChange,
    onMoveBackward: handleChange,
    onToggleMonthDropdown: handleToggleMonthDropdown,
    onChangePageDate: handleChangePageDate,
    onSelect: handleSelect
  }));
});

CalendarPanel.displayName = 'CalendarPanel';
CalendarPanel.propTypes = {
  value: _propTypes.default.instanceOf(Date),
  defaultValue: _propTypes.default.instanceOf(Date),
  isoWeek: _propTypes.default.bool,
  timeZone: _propTypes.default.string,
  compact: _propTypes.default.bool,
  bordered: _propTypes.default.bool,
  locale: _propTypes.default.object,
  className: _propTypes.default.string,
  classPrefix: _propTypes.default.string,
  onChange: _propTypes.default.func,
  onSelect: _propTypes.default.func,
  renderCell: _propTypes.default.func
};
CalendarPanel.defaultProps = defaultProps;
var _default = CalendarPanel;
exports.default = _default;