"use strict";

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

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

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

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

var _weekday = _interopRequireDefault(require("dayjs/plugin/weekday"));

var _localeData = _interopRequireDefault(require("dayjs/plugin/localeData"));

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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

_dayjs.default.extend(_weekday.default);

_dayjs.default.extend(_localeData.default);

var _default = function _default(_ref) {
  var children = _ref.children,
      _ref$onChange = _ref.onChange,
      onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange,
      value = _ref.value,
      mode = _ref.mode,
      _ref$dateFormat = _ref.dateFormat,
      dateFormat = _ref$dateFormat === void 0 ? 'YYYY-MM-DD HH:mm:ss' : _ref$dateFormat;

  if (value !== undefined) {
    if (mode === 'array') {
      value = [value];
    }

    if (mode === 'split') {
      value = value ? value.split(',') : undefined;
    } else if (mode === 'date') {
      value = value && (0, _dayjs.default)(value, dateFormat);
    } else if (mode === 'time') {
      value = value && (0, _dayjs.default)(value, 'HH:mm');
    } else if (mode === 'JSON') {
      value = JSON.parse(value || '{}');
    }
  }

  var handleChange = (0, _react.useCallback)(function () {
    var _children$props, _children$props$onCha;

    for (var _len = arguments.length, arg = new Array(_len), _key = 0; _key < _len; _key++) {
      arg[_key] = arguments[_key];
    }

    var value = arg[0];

    if (value === undefined) {
      return onChange(value);
    }

    if (mode === 'array') {
      value = value[0];
    } else if (mode === 'split') {
      value = value.join(',');
    } else if (mode === 'date') {
      value = value && (0, _dayjs.default)(value).format(dateFormat);
    } else if (mode === 'time') {
      value = value && (0, _dayjs.default)(value).format('HH:mm');
    } else if (mode === 'JSON') {
      value = Object.keys(value).length ? JSON.stringify(value) : undefined;
    }

    onChange(value);
    children === null || children === void 0 ? void 0 : (_children$props = children.props) === null || _children$props === void 0 ? void 0 : (_children$props$onCha = _children$props.onChange) === null || _children$props$onCha === void 0 ? void 0 : _children$props$onCha.call.apply(_children$props$onCha, [_children$props].concat(arg));
  }, [children, dateFormat, mode, onChange]);
  return mode && /*#__PURE__*/_react.default.isValidElement(children) ? /*#__PURE__*/_react.default.cloneElement(children, _objectSpread(_objectSpread({}, children.props), {}, {
    onChange: handleChange,
    value: value
  })) : children instanceof Function && children(handleChange, value);
};

exports.default = _default;