import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import React, { useCallback, useEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import Calendar, { CalendarState } from './Calendar';
import Button from '../Button';
import { FormattedDate } from '../CustomProvider';
import { composeFunctions, useClassNames, useCustom, TimeZone, DateUtils } from '../utils';
var defaultProps = {
  defaultValue: new Date(),
  classPrefix: 'calendar',
  as: Calendar
};
var CalendarPanel = /*#__PURE__*/React.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 = _objectWithoutPropertiesLoose(props, ["as", "bordered", "className", "classPrefix", "compact", "defaultValue", "isoWeek", "locale", "onChange", "onSelect", "renderCell", "timeZone", "value"]);

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

  var _useState2 = useState(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 = useState(false),
      showMonth = _useState3[0],
      setShowMonth = _useState3[1];

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

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

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

  var renderToolbar = useCallback(function () {
    return /*#__PURE__*/React.createElement(Button, {
      className: prefix('btn-today'),
      size: "sm",
      onClick: handleClickToday
    }, locale.today || 'Today');
  }, [handleClickToday, locale.today, prefix]);
  var customRenderCell = useCallback(function (date) {
    return renderCell === null || renderCell === void 0 ? void 0 : renderCell(TimeZone.toLocalTimeZone(date, timeZone));
  }, [renderCell, timeZone]);
  var classes = merge(className, withClassPrefix('panel', {
    bordered: bordered,
    compact: compact
  }));
  return /*#__PURE__*/React.createElement(Component, _extends({}, rest, {
    inline: true,
    className: classes,
    ref: ref,
    isoWeek: isoWeek,
    format: "yyyy-MM-dd",
    calendarState: showMonth ? CalendarState.DROP_MONTH : null,
    pageDate: pageDate,
    timeZone: timeZone,
    limitEndYear: 1000,
    locale: locale,
    renderTitle: function renderTitle(date) {
      return /*#__PURE__*/React.createElement(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.instanceOf(Date),
  defaultValue: PropTypes.instanceOf(Date),
  isoWeek: PropTypes.bool,
  timeZone: PropTypes.string,
  compact: PropTypes.bool,
  bordered: PropTypes.bool,
  locale: PropTypes.object,
  className: PropTypes.string,
  classPrefix: PropTypes.string,
  onChange: PropTypes.func,
  onSelect: PropTypes.func,
  renderCell: PropTypes.func
};
CalendarPanel.defaultProps = defaultProps;
export default CalendarPanel;