"use strict";

exports.__esModule = true;
exports.default = void 0;
var _tslib = require("tslib");
var _react = _interopRequireWildcard(require("react"));
var _dayjs = _interopRequireDefault(require("dayjs"));
var _picker = _interopRequireDefault(require("../picker"));
var _locale = require("../locale");
var _zhCn = _interopRequireDefault(require("../locale/lang/zh-cn"));
var _date = require("../utils/date");
var _hooks = require("../utils/hooks");
var _utils = require("./utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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; }
var EmbedPicker = function (props, ref) {
  var _props$prefix = props.prefix,
    prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
    valueProp = props.value,
    defaultValue = props.defaultValue,
    onChange = props.onChange,
    className = props.className,
    hourStep = props.hourStep,
    minuteStep = props.minuteStep,
    secondStep = props.secondStep,
    _props$format = props.format,
    format = _props$format === void 0 ? _utils.DEFAULT_FORMAT : _props$format,
    disabledHours = props.disabledHours,
    disabledMinutes = props.disabledMinutes,
    disabledSeconds = props.disabledSeconds,
    validRangeProp = props.validRange,
    others = (0, _tslib.__rest)(props, ["prefix", "value", "defaultValue", "onChange", "className", "hourStep", "minuteStep", "secondStep", "format", "disabledHours", "disabledMinutes", "disabledSeconds", "validRange"]);
  var options = (0, _utils.getTimeOption)(props);
  var validRange = (0, _date.getRealRange)(options, props, validRangeProp);
  var locale = (0, _locale.useLocale)('TimePicker', others, _zhCn.default.TimePicker);
  var _useValue = (0, _hooks.useValue)(props, new Date(), {
      fitValue: function fitValue(v) {
        var d = (0, _date.toDate)(v);
        var x = (0, _date.getRangeDate)((0, _date.isValidDate)(d) ? d : new Date(), {
          time: options
        }, validRange);
        if (validRange && (secondStep !== 1 || minuteStep !== 1 || hourStep !== 1)) {
          var xt = (0, _dayjs.default)(x).format(format);
          if (xt !== (0, _dayjs.default)(validRange[0]).format(format) && xt !== (0, _dayjs.default)(validRange[1]).format(format)) {
            return (0, _date.getNearestDate)(options, props, x, 'auto');
          }
        }
        return x;
      },
      compare: function compare(a, b) {
        return a.getTime() === b.getTime();
      }
    }),
    value = _useValue[0],
    setValue = _useValue[1],
    isControlled = _useValue[2];
  var dataSource = (0, _utils.getColumns)(value, options, Object.assign(Object.assign({}, props), {
    locale: locale
  }));
  var pickerValue = (0, _utils.dateToPicker)(value, options);
  return /*#__PURE__*/(0, _react.createElement)(_picker.default, {
    ref: ref,
    className: className,
    prefix: prefix,
    value: pickerValue,
    data: dataSource,
    onChange: function handleChange(values) {
      var d = (0, _date.getRangeDate)((0, _utils.pickerToDate)(values, options), {
        time: options
      }, validRange);
      if (onChange) {
        onChange(d);
      }
      if (!isControlled) {
        setValue(d);
      }
    }
  });
};
var _default = /*#__PURE__*/(0, _react.forwardRef)(EmbedPicker);
exports.default = _default;