"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireWildcard(require("react"));
var _moment = _interopRequireDefault(require("moment"));
var _datePicker = _interopRequireDefault(require("../date-picker"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _utils = require("../utils");
var _wauProvider = require("../wau-provider");
var _wauFieldLabel = _interopRequireDefault(require("../wau-field-label"));
require("./style");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
const ACTIVE_PICKER_INDEX_LEFT = 0;
const ACTIVE_PICKER_INDEX_RIGHT = 1;

/**
 * 日期范围选择组件
 *
 * @param
 */
const FieldRangePicker = ({
  text,
  mode,
  format,
  label,
  render,
  renderFormItem,
  plain,
  showTime,
  fieldProps,
  light
}, ref) => {
  const intl = (0, _wauProvider.useIntl)();
  const size = (0, _react.useContext)(_configProvider.default.SizeContext);
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const prefixCls = getPrefixCls('pro-field-date-picker');
  const [startText, endText] = Array.isArray(text) ? text : [];
  const [open, setOpen] = (0, _react.useState)(false);
  // activePickerIndex for https://github.com/ant-design/ant-design/issues/22158
  const [activePickerIndex, setActivePickerIndex] = (0, _react.useState)();
  const parsedStartText = startText ? (0, _moment.default)(startText).format(format || 'YYYY-MM-DD') : '';
  const parsedEndText = endText ? (0, _moment.default)(endText).format(format || 'YYYY-MM-DD') : '';
  if (mode === 'read') {
    const dom = /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      ref: ref,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        children: parsedStartText || '-'
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        children: parsedEndText || '-'
      })]
    });
    if (render) {
      return render(text, {
        mode,
        ...fieldProps
      }, /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        children: dom
      }));
    }
    return dom;
  }
  if (mode === 'edit' || mode === 'update') {
    const {
      disabled,
      onChange,
      allowClear,
      placeholder = intl.getMessage('tableForm.selectPlaceholder', '请选择')
    } = fieldProps;
    let dom;
    const momentValue = (0, _utils.parseValueToMoment)(fieldProps.value);
    if (light) {
      const valueStr = parsedStartText && parsedEndText && `${parsedStartText} ~ ${parsedEndText}`;
      dom = /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: `${prefixCls}-light`,
        onClick: () => {
          setOpen(true);
        },
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_datePicker.default.RangePicker, {
          ...fieldProps,
          ref: ref,
          format: format,
          showTime: showTime,
          placeholder: [intl.getMessage('tableForm.selectPlaceholder', '请选择'), intl.getMessage('tableForm.selectPlaceholder', '请选择')],
          bordered: plain === undefined ? true : !plain,
          onChange: v => {
            onChange(v);
            setTimeout(() => {
              setOpen(false);
            }, 0);
          },
          activePickerIndex: activePickerIndex,
          onOpenChange: setOpen,
          open: open,
          onCalendarChange: dates => {
            if (dates && !dates[0]) {
              setActivePickerIndex(ACTIVE_PICKER_INDEX_LEFT);
            } else if (dates && !dates[1]) {
              setActivePickerIndex(ACTIVE_PICKER_INDEX_RIGHT);
            }
          }
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauFieldLabel.default, {
          label: label,
          disabled: disabled,
          placeholder: placeholder,
          size: size,
          value: valueStr,
          allowClear: allowClear,
          onClear: () => {
            onChange(null);
            setActivePickerIndex(0);
          },
          expanded: open
        })]
      });
    } else {
      dom = /*#__PURE__*/(0, _jsxRuntime.jsx)(_datePicker.default.RangePicker, {
        ref: ref,
        format: format,
        showTime: showTime,
        placeholder: [intl.getMessage('tableForm.selectPlaceholder', '请选择'), intl.getMessage('tableForm.selectPlaceholder', '请选择')],
        bordered: plain === undefined ? true : !plain,
        ...fieldProps,
        value: momentValue
      });
    }
    if (renderFormItem) {
      return renderFormItem(text, {
        mode,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  return null;
};
var _default = /*#__PURE__*/_react.default.forwardRef(FieldRangePicker);
exports.default = _default;