import _Radio from "@alifd/next/es/radio";
import _Checkbox from "@alifd/next/es/checkbox";
import _Select from "@alifd/next/es/select";
import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { useCallback, useEffect, useState } from 'react';
import { selectList } from "../form-item/data";
import { getChildList } from "../../utils/api";
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 = useState(isMultiple ? [] : ''),
    inputValue = _useState[0],
    setValue = _useState[1];
  var _useState2 = useState([]),
    optionData = _useState2[0],
    setOptionData = _useState2[1];
  var onInputChange = function onInputChange(value) {
    onChange && onChange(value);
    setValue(value);
  };
  var initData = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var data;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (!(enumList !== undefined)) {
            _context.next = 5;
            break;
          }
          _context.next = 3;
          return getChildList(enumList.id);
        case 3:
          data = _context.sent;
          setOptionData(data);
        case 5:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })), [enumList]);
  useEffect(function () {
    initData();
  }, [initData]);
  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 _extends({}, item, {
        label: item.dictValue,
        value: item.dictKey
      });
    }) : 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.createElement(_Select, {
        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.createElement(_Checkbox.Group, {
          isPreview: isPreview,
          value: inputValue,
          onChange: function onChange(e) {
            return onInputChange(e);
          },
          defaultValue: defaultVal,
          disabled: isDisabled,
          dataSource: childList
        });
      } else {
        return /*#__PURE__*/React.createElement(_Radio.Group, {
          value: inputValue,
          isPreview: isPreview,
          onChange: function onChange(e) {
            return onInputChange(e);
          },
          defaultValue: defaultVal,
          disabled: isDisabled,
          dataSource: childList
        });
      }
    }
  };
  return renderColumn();
};
CommonOption.displayName = 'CommonOption';
export default CommonOption;