"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DatePicker = void 0;
var _react = _interopRequireWildcard(require("react"));
var _ahooks = require("ahooks");
var _picker = _interopRequireDefault(require("../picker"));
var _nativeProps = require("../../utils/native-props");
var _withDefaultProps = require("../../utils/with-default-props");
var _usePropsValue = require("../../utils/use-props-value");
var _datePickerUtils = require("./date-picker-utils");
var _bound = require("../../utils/bound");
var _useRenderLabel = _interopRequireDefault(require("../date-picker-view/useRenderLabel"));
var _util = require("./util");
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',
  defaultValue: null
};
const DatePicker = exports.DatePicker = (0, _react.forwardRef)((p, ref) => {
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const {
    renderLabel
  } = props;
  const [value, setValue] = (0, _usePropsValue.usePropsValue)({
    value: props.value,
    defaultValue: props.defaultValue,
    onChange: v => {
      var _a;
      if (v === null) return;
      (_a = props.onConfirm) === null || _a === void 0 ? void 0 : _a.call(props, v);
    }
  });
  const now = (0, _react.useMemo)(() => new Date(), []);
  const mergedRenderLabel = (0, _useRenderLabel.default)(renderLabel);
  const pickerValue = (0, _react.useMemo)(() => {
    let date = value !== null && value !== void 0 ? value : now;
    if (date.tillNow) {
      return [_util.TILL_NOW];
    }
    date = new Date((0, _bound.bound)(date.getTime(), props.min.getTime(), props.max.getTime()));
    return (0, _datePickerUtils.convertDateToStringArray)(date, props.precision);
  }, [value, props.precision, props.min, props.max]);
  const onConfirm = (0, _react.useCallback)(val => {
    const date = (0, _datePickerUtils.convertStringArrayToDate)(val, props.precision);
    setValue(date, true);
  }, [setValue, props.precision]);
  const onSelect = (0, _ahooks.useMemoizedFn)(val => {
    var _a;
    const date = (0, _datePickerUtils.convertStringArrayToDate)(val, props.precision);
    (_a = props.onSelect) === null || _a === void 0 ? void 0 : _a.call(props, date);
  });
  const columns = (0, _react.useCallback)(selected => (0, _datePickerUtils.generateDatePickerColumns)(selected, props.min, props.max, props.precision, mergedRenderLabel, props.filter, props.tillNow), [props.min, props.max, props.precision, mergedRenderLabel, props.tillNow]);
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement(_picker.default, {
    ref: ref,
    columns: columns,
    value: pickerValue,
    onCancel: props.onCancel,
    onClose: props.onClose,
    closeOnMaskClick: props.closeOnMaskClick,
    visible: props.visible,
    confirmText: props.confirmText,
    cancelText: props.cancelText,
    onConfirm: onConfirm,
    onSelect: onSelect,
    getContainer: props.getContainer,
    loading: props.loading,
    loadingContent: props.loadingContent,
    afterShow: props.afterShow,
    afterClose: props.afterClose,
    onClick: props.onClick,
    title: props.title,
    stopPropagation: props.stopPropagation,
    mouseWheel: props.mouseWheel,
    destroyOnClose: props.destroyOnClose,
    forceRender: props.forceRender
  }, (_, actions) => {
    var _a;
    return (_a = props.children) === null || _a === void 0 ? void 0 : _a.call(props, value, actions);
  }));
});