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

/*
 * @Author: codingfly
 * @Description:
 * @Date: 2021-10-13 08:47:06
 * @LastEditTime: 2021-10-13 08:59:50
 * @FilePath: \ui_model\src\transfer-change\index.tsx
 */
import React, { useCallback } from 'react';
import dayjs from 'dayjs';
import weekday from 'dayjs/plugin/weekday';
import localeData from 'dayjs/plugin/localeData';
dayjs.extend(weekday);
dayjs.extend(localeData);
export default (function (_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 && dayjs(value, dateFormat);
    } else if (mode === 'time') {
      value = value && dayjs(value, 'HH:mm');
    } else if (mode === 'JSON') {
      value = JSON.parse(value || '{}');
    }
  }

  var handleChange = 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 && dayjs(value).format(dateFormat);
    } else if (mode === 'time') {
      value = value && dayjs(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.isValidElement(children) ? /*#__PURE__*/React.cloneElement(children, _objectSpread(_objectSpread({}, children.props), {}, {
    onChange: handleChange,
    value: value
  })) : children instanceof Function && children(handleChange, value);
});