"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

var _classnames = _interopRequireDefault(require("classnames"));

var _moment = _interopRequireDefault(require("moment"));

var _utils = require("../../utils");

var _context = _interopRequireDefault(require("../../context"));

require("./index.css");

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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var PickerTrigger = function PickerTrigger(props) {
  var _classNames;

  var children = props.children,
      focused = props.focused,
      viewDate = props.viewDate,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      _props$allowClear = props.allowClear,
      allowClear = _props$allowClear === void 0 ? false : _props$allowClear,
      placeholder = props.placeholder,
      _onClick = props.onClick;

  var _useContext = (0, _react.useContext)(_context["default"]),
      picker = _useContext.picker,
      format = _useContext.format,
      value = _useContext.value;

  var currFormat = format;

  switch (picker) {
    case 'date':
      currFormat = _utils.PickerFormat;
      break;

    case 'month':
      currFormat = 'YYYY/MM';
      break;

    case 'year':
      currFormat = 'YYYY';
      break;

    default:
      currFormat = format;
      break;
  }

  return (
    /*#__PURE__*/
    // eslint-disable-next-line react/jsx-no-useless-fragment
    _react["default"].createElement(_react["default"].Fragment, null, children ? /*#__PURE__*/_react["default"].createElement("div", {
      onClick: function onClick() {
        return !disabled && _onClick();
      }
    }, children) : /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])(_utils.PickerTriggerClass, (_classNames = {}, _defineProperty(_classNames, "".concat(_utils.PickerTriggerClass, "--disabled"), disabled), _defineProperty(_classNames, "".concat(_utils.PickerTriggerClass, "--focused"), focused), _classNames)),
      onClick: function onClick() {
        return !disabled && _onClick();
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(_utils.PickerTriggerClass, "__input"), _defineProperty({}, "".concat(_utils.PickerTriggerClass, "__input--placeholder"), !value && placeholder))
    }, value ? (0, _moment["default"])(viewDate).format(currFormat) : placeholder), /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(_utils.PickerTriggerClass, "__operation"))
    }, !disabled && /*#__PURE__*/_react["default"].createElement("span", {
      className: "tu-icon-caret-down"
    }), allowClear && !disabled && /*#__PURE__*/_react["default"].createElement("span", {
      className: "tu-icon-circle"
    }))))
  );
};

PickerTrigger.defaultProps = {
  children: null,
  disabled: false,
  allowClear: false,
  placeholder: '请选择'
};
var _default = PickerTrigger;
exports["default"] = _default;