"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 _getLocale = require("../../../../../utils/getLocale");

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

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

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 RangePickerTrigger = function RangePickerTrigger(props) {
  var _classNames;

  var children = props.children,
      focused = props.focused,
      _props$triggerClassNa = props.triggerClassName,
      triggerClassName = _props$triggerClassNa === void 0 ? '' : _props$triggerClassNa,
      disabled = props.disabled,
      allowClear = props.allowClear,
      placeholder = props.placeholder,
      onClick = props.onClick;

  var _useContext = (0, _react.useContext)(_context.RangePickerContext),
      picker = _useContext.picker,
      format = _useContext.format,
      values = _useContext.values;

  var onChildrenClick = function onChildrenClick() {
    return !disabled && (onClick === null || onClick === void 0 ? void 0 : onClick());
  };

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

  var correctlyValue = values && values instanceof Array;
  var startDate = correctlyValue && values[0] ? (0, _moment["default"])(values[0]).format(format) : '';
  var endDate = correctlyValue && values[1] ? (0, _moment["default"])(values[1]).format(format) : '';
  var viewDateEnable = startDate && endDate;
  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: onChildrenClick
    }, children) : /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])(_utils.PickerRangeTriggerClass, (_classNames = {}, _defineProperty(_classNames, "".concat(_utils.PickerRangeTriggerClass, "--disabled"), disabled), _defineProperty(_classNames, "".concat(_utils.PickerRangeTriggerClass, "--focused"), focused), _classNames), triggerClassName),
      onClick: onChildrenClick
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(_utils.PickerRangeTriggerClass, "__input"), _defineProperty({}, "".concat(_utils.PickerRangeTriggerClass, "__input--placeholder"), !viewDateEnable && placeholder))
    }, startDate && /*#__PURE__*/_react["default"].createElement("p", {
      className: (0, _classnames["default"])("".concat(_utils.PickerRangeTriggerClass, "__selected-date"))
    }, startDate), endDate && /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(_utils.PickerRangeTriggerClass, "__date-connector"))
    }, "~"), endDate && /*#__PURE__*/_react["default"].createElement("p", {
      className: (0, _classnames["default"])("".concat(_utils.PickerRangeTriggerClass, "__selected-date"))
    }, endDate), !viewDateEnable && placeholder), /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(_utils.PickerRangeTriggerClass, "__operation"))
    }, allowClear && !disabled && /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])('tu-icon-cross', "".concat(_utils.PickerRangeTriggerClass, "__selected-date__cancel"), _defineProperty({}, "".concat(_utils.PickerRangeTriggerClass, "__selected-date__cancel--visible"), viewDateEnable))
    }), !disabled && /*#__PURE__*/_react["default"].createElement("span", {
      className: "tu-icon-caret-down"
    }))))
  );
};

RangePickerTrigger.defaultProps = {
  children: null,
  disabled: false,
  allowClear: false,
  placeholder: (0, _getLocale.getLocale)('Rep_NewS_All')
};
var _default = RangePickerTrigger;
exports["default"] = _default;