"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DatePickerView = void 0;
var _react = _interopRequireWildcard(require("react"));
var _nativeProps = require("../../utils/native-props");
var _usePropsValue = require("../../utils/use-props-value");
var _withDefaultProps = require("../../utils/with-default-props");
var _datePickerUtils = require("../date-picker/date-picker-utils");
var _util = require("../date-picker/util");
var _pickerView = _interopRequireDefault(require("../picker-view"));
var _useRenderLabel = _interopRequireDefault(require("./useRenderLabel"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const thisYear = new Date().getFullYear();
const defaultProps = {
  min: new Date(new Date().setFullYear(thisYear - 10)),
  max: new Date(new Date().setFullYear(thisYear + 10)),
  precision: 'day'
};
const DatePickerView = p => {
  var _a;
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const {
    renderLabel
  } = props;
  const [value, setValue] = (0, _usePropsValue.usePropsValue)({
    value: props.value,
    defaultValue: (_a = props.defaultValue) !== null && _a !== void 0 ? _a : null
  });
  const mergedRenderLabel = (0, _useRenderLabel.default)(renderLabel);
  const pickerValue = (0, _react.useMemo)(() => {
    if (value === null || value === void 0 ? void 0 : value.tillNow) {
      return [_util.TILL_NOW, null, null];
    }
    return (0, _datePickerUtils.convertDateToStringArray)(value, props.precision);
  }, [value, props.precision]);
  const onChange = (0, _react.useCallback)(val => {
    var _a;
    const date = (0, _datePickerUtils.convertStringArrayToDate)(val, props.precision);
    if (date) {
      setValue(date);
      (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, date);
    }
  }, [props.onChange, props.precision]);
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement(_pickerView.default, {
    columns: selected => (0, _datePickerUtils.generateDatePickerColumns)(selected, props.min, props.max, props.precision, mergedRenderLabel, props.filter, props.tillNow),
    loading: props.loading,
    loadingContent: props.loadingContent,
    value: pickerValue,
    mouseWheel: props.mouseWheel,
    onChange: onChange
  }));
};
exports.DatePickerView = DatePickerView;