"use strict";

exports.__esModule = true;
exports.default = CalendarTimePicker;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _dayjs = _interopRequireDefault(require("dayjs"));
var _meetReactComponentOne = require("@alifd/meet-react-component-one");
var _view = _interopRequireDefault(require("../view"));
var _icon = _interopRequireDefault(require("../icon"));
var _timePicker = require("../time-picker");
var _utils = require("../utils");
var _date = require("../utils/date");
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 CalendarTimePicker(props) {
  var _classNames, _classNames2;
  var _props$prefix = props.prefix,
    prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
    value = props.value,
    _props$format = props.format,
    format = _props$format === void 0 ? 'HH:mm:ss' : _props$format,
    label = props.label,
    onPickerToggle = props.onPickerToggle,
    onChange = props.onChange;
  var _useState = (0, _react.useState)(false),
    showPicker = _useState[0],
    setShowPicker = _useState[1];
  var _useState2 = (0, _react.useState)((0, _date.isValidDate)(value) ? value : new Date()),
    curTime = _useState2[0],
    setTime = _useState2[1];
  var clsPrefix = prefix + "calendar-tp";
  (0, _react.useEffect)(function () {
    if ((0, _date.isValidDate)(value)) {
      setTime(value);
    }
  }, [value]);
  return /*#__PURE__*/(0, _react.createElement)(_react.Fragment, null, showPicker ? /*#__PURE__*/(0, _react.createElement)(_timePicker.EmbedPicker, {
    className: clsPrefix + "-picker",
    format: format,
    value: curTime,
    onChange: function handlePickerChange(date) {
      var d = (0, _dayjs.default)(date);
      var h = d.hour();
      var m = d.minute();
      var s = d.second();
      var newDate = (0, _dayjs.default)(curTime).hour(h).minute(m).second(s);
      if ((0, _utils.isFunction)(onChange)) {
        onChange(newDate.toDate());
      }
    }
  }) : null, /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: "" + clsPrefix
  }, /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "-row"
  }, /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
    className: clsPrefix + "-label"
  }, label), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(clsPrefix + "-wrapper", (_classNames = {}, _classNames[clsPrefix + "-wrapper--active"] = showPicker, _classNames)),
    onClick: function handleTimeClick() {
      var nextState = !showPicker;
      if ((0, _utils.isFunction)(onPickerToggle)) {
        onPickerToggle(nextState);
      }
      setShowPicker(nextState);
    }
  }, /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
    className: clsPrefix + "-time"
  }, (0, _dayjs.default)(curTime).format(format)), /*#__PURE__*/(0, _react.createElement)(_icon.default, {
    className: (0, _classnames.default)(clsPrefix + "-arrow", (_classNames2 = {}, _classNames2[clsPrefix + "-arrow--active"] = showPicker, _classNames2)),
    name: "arrow-down"
  })))));
}