"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 _configProvider = _interopRequireDefault(require("../config-provider"));
var _datePicker = _interopRequireDefault(require("../date-picker"));
var _utils = require("../utils");
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; }
/**
 * 日期选择组件
 *
 * @param
 */const FieldTimePicker = ({
  text,
  mode,
  light,
  label,
  format,
  render,
  renderFormItem,
  plain,
  fieldProps
}, ref) => {
  const [open, setOpen] = (0, _react.useState)(false);
  const size = (0, _react.useContext)(_configProvider.default.SizeContext);
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const prefixCls = getPrefixCls('pro-field-date-picker');
  if (mode === 'read') {
    const dom = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      ref: ref,
      children: text ? (0, _moment.default)(text).format(format || 'HH:mm:ss') : '-'
    });
    if (render) {
      return render(text, {
        mode,
        ...fieldProps
      }, /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        children: dom
      }));
    }
    return dom;
  }
  if (mode === 'edit' || mode === 'update') {
    let dom;
    const {
      disabled,
      onChange,
      placeholder,
      allowClear,
      value
    } = fieldProps;
    const momentValue = (0, _utils.parseValueToMoment)(value);
    if (light) {
      const valueStr = momentValue && momentValue.format(format || 'HH:mm:ss') || '';
      dom = /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: `${prefixCls}-light`,
        onClick: () => {
          setOpen(true);
        },
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_datePicker.default.TimePicker, {
          value: momentValue,
          format: format,
          ref: ref,
          ...fieldProps,
          onChange: v => {
            if (onChange) {
              onChange(v);
            }
            setTimeout(() => {
              setOpen(false);
            }, 0);
          },
          onOpenChange: setOpen,
          open: open
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauFieldLabel.default, {
          label: label,
          disabled: disabled,
          placeholder: placeholder,
          size: size,
          value: valueStr,
          allowClear: allowClear,
          onClear: () => {
            if (onChange) {
              onChange(null);
            }
          },
          expanded: open
        })]
      });
    } else {
      dom = /*#__PURE__*/(0, _jsxRuntime.jsx)(_datePicker.default.TimePicker, {
        ref: ref,
        format: format,
        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(FieldTimePicker);
exports.default = _default;