"use strict";

exports.__esModule = true;
exports.default = void 0;

var _tslib = require("tslib");

var _react = _interopRequireWildcard(require("react"));

var _dayjs = _interopRequireDefault(require("dayjs"));

var _form = require("../form");

var _selectDrawer = _interopRequireDefault(require("../select-drawer"));

var _date = require("../utils/date");

var _view = _interopRequireDefault(require("../view"));

var _embedPicker = _interopRequireDefault(require("./embed-picker"));

require("./styles/index.css");

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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var TimePicker = function (props) {
  var _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      valueProp = props.value,
      defaultValue = props.defaultValue,
      sizeProp = props.size,
      alignProp = props.align,
      iconType = props.iconType,
      placeholder = props.placeholder,
      format = props.format,
      hasClear = props.hasClear,
      disabled = props.disabled,
      readOnly = props.readOnly,
      _props$onChange = props.onChange,
      onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
      _props$onClear = props.onClear,
      onClear = _props$onClear === void 0 ? function () {} : _props$onClear,
      _props$onCancel = props.onCancel,
      onCancel = _props$onCancel === void 0 ? function () {} : _props$onCancel,
      className = props.className,
      zIndex = props.zIndex,
      others = (0, _tslib.__rest)(props, ["prefix", "value", "defaultValue", "size", "align", "iconType", "placeholder", "format", "hasClear", "disabled", "readOnly", "onChange", "onClear", "onCancel", "className", "zIndex"]);

  var _useState = (0, _react.useState)((0, _date.toDate)(valueProp || defaultValue)),
      value = _useState[0],
      setValue = _useState[1];

  var _useState2 = (0, _react.useState)(value || new Date()),
      pickerValue = _useState2[0],
      setPickerValue = _useState2[1];

  var _useContext = (0, _react.useContext)(_form.ItemContext),
      isInsideForm = _useContext.isInsideForm,
      contentAlign = _useContext.contentAlign,
      contextSize = _useContext.size;

  var align = alignProp || (isInsideForm ? contentAlign : 'left');
  var size = sizeProp || (isInsideForm ? contextSize : 'medium');
  var isControlled = ('value' in props);
  (0, _react.useEffect)(function () {
    if ('value' in props) {
      var newValue = (0, _date.toDate)(valueProp || defaultValue);

      if (!(0, _date.isValidDate)(newValue) || newValue === value) {
        return;
      }

      setValue(newValue);
      setPickerValue(newValue);
    }
  }, [valueProp]);
  return /*#__PURE__*/(0, _react.createElement)(_selectDrawer.default, {
    disableScroll: true,
    hasClear: hasClear,
    content: value ? (0, _dayjs.default)(value).format(format || 'HH:mm') : '',
    align: align,
    size: size,
    iconType: iconType,
    placeholder: placeholder,
    disabled: disabled,
    readOnly: readOnly,
    className: className,
    zIndex: zIndex,
    onOk: function handleOk() {
      var newValue = pickerValue;

      if (onChange) {
        onChange(newValue);
      }

      if (!isControlled) {
        setValue(newValue);
      }
    },
    onCancel: function handleCancel(reason) {
      setPickerValue(value || new Date());

      if ((0, _utils.isFunction)(onCancel)) {
        onCancel(reason);
      }
    },
    onClear: function handleClear() {
      if (!isControlled) {
        setPickerValue(new Date());
        setValue(undefined);
      }

      if (onClear) {
        onClear();
      }

      if (onChange) {
        onChange(undefined);
      }
    }
  }, /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: prefix + "timepicker-container",
    "disable-scroll": true
  }, /*#__PURE__*/(0, _react.createElement)(_embedPicker.default, _extends({}, others, {
    prefix: prefix,
    value: pickerValue,
    format: format || 'HH:mm',
    onChange: function handleChange(d) {
      return setPickerValue(d);
    }
  }))));
};

var _default = /*#__PURE__*/(0, _react.forwardRef)(TimePicker);

exports.default = _default;