"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _radio = _interopRequireDefault(require("@alifd/next/lib/radio"));
var _checkbox = _interopRequireDefault(require("@alifd/next/lib/checkbox"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireWildcard(require("react"));
var _data = require("../form-item/data");
var _api = require("../../utils/api");
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; }
var CommonOption = function CommonOption(_ref) {
  var value = _ref.value,
    enumList = _ref.enumList,
    _ref$placeholder = _ref.placeholder,
    placeholder = _ref$placeholder === void 0 ? '' : _ref$placeholder,
    defaultValue = _ref.defaultValue,
    _ref$isMultiple = _ref.isMultiple,
    isMultiple = _ref$isMultiple === void 0 ? false : _ref$isMultiple,
    _ref$isDisabled = _ref.isDisabled,
    isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled,
    _ref$isDeployment = _ref.isDeployment,
    isDeployment = _ref$isDeployment === void 0 ? "primary" : _ref$isDeployment,
    _ref$isPreview = _ref.isPreview,
    isPreview = _ref$isPreview === void 0 ? false : _ref$isPreview,
    onChange = _ref.onChange;
  var _useState = (0, _react.useState)(isMultiple ? [] : ''),
    inputValue = _useState[0],
    setValue = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    optionData = _useState2[0],
    setOptionData = _useState2[1];
  var onInputChange = function onInputChange(value) {
    onChange && onChange(value);
    setValue(value);
  };
  var initData = (0, _react.useCallback)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
    var data;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (!(enumList !== undefined)) {
            _context.next = 5;
            break;
          }
          _context.next = 3;
          return (0, _api.getChildList)(enumList.id);
        case 3:
          data = _context.sent;
          setOptionData(data);
        case 5:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })), [enumList]);
  (0, _react.useEffect)(function () {
    initData();
  }, [initData]);
  (0, _react.useEffect)(function () {
    var val = value;
    if (isMultiple) {
      if (val && !(value instanceof Array) && String(val).indexOf(',') > -1) {
        val = String(val).split(',');
      }
      if (!val) {
        val = [];
      }
    }
    setValue(val);
  }, [value]);
  var renderColumn = function renderColumn() {
    var childList = enumList ? optionData.map(function (item) {
      return (0, _extends2["default"])({}, item, {
        label: item.dictValue,
        value: item.dictKey
      });
    }) : _data.selectList;
    // isDeployment = isDeployment === undefined ? 'primary' : isDeployment;
    var mode = isMultiple ? 'multiple' : 'single';
    var defaultVal = isMultiple ? [defaultValue] : defaultValue;
    if (isDeployment === 'primary') {
      // console.log(inputValue)
      // defaultValue = defaultValue === '' ? [] : defaultValue;
      return /*#__PURE__*/_react["default"].createElement(_select["default"], {
        value: inputValue,
        isPreview: isPreview,
        onChange: function onChange(e) {
          return onInputChange(e);
        },
        defaultValue: defaultVal,
        hasClear: true,
        disabled: isDisabled,
        dataSource: childList,
        placeholder: placeholder,
        mode: mode
      });
    } else {
      if (mode === 'multiple') {
        return /*#__PURE__*/_react["default"].createElement(_checkbox["default"].Group, {
          isPreview: isPreview,
          value: inputValue,
          onChange: function onChange(e) {
            return onInputChange(e);
          },
          defaultValue: defaultVal,
          disabled: isDisabled,
          dataSource: childList
        });
      } else {
        return /*#__PURE__*/_react["default"].createElement(_radio["default"].Group, {
          value: inputValue,
          isPreview: isPreview,
          onChange: function onChange(e) {
            return onInputChange(e);
          },
          defaultValue: defaultVal,
          disabled: isDisabled,
          dataSource: childList
        });
      }
    }
  };
  return renderColumn();
};
CommonOption.displayName = 'CommonOption';
var _default = CommonOption;
exports["default"] = _default;