import _Box from "@alifd/next/es/box";
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 _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React from 'react';
import { bizCssPrefix } from "../../variables";
import "./index.scss";
import { selectList } from "../form-item/data";
import { getChildList } from "../../utils/api";
var CommonOption = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(CommonOption, _React$Component);
  function CommonOption(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    // 渲染数据
    _this.initData = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var enumList, data;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            enumList = _this.props.enumList;
            if (!(enumList !== undefined)) {
              _context.next = 6;
              break;
            }
            _context.next = 4;
            return getChildList(enumList.id);
          case 4:
            data = _context.sent;
            _this.setState({
              optionData: data
            });
          case 6:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    _this.onChange = function (value) {
      _this.props.onChange && _this.props.onChange(value);
      _this.setState({
        selectData: value
      });
    };
    _this.renderColumn = function () {
      var _this$props = _this.props,
        isMultiple = _this$props.isMultiple,
        isDeployment = _this$props.isDeployment,
        enumList = _this$props.enumList,
        defaultValue = _this$props.defaultValue,
        placeholder = _this$props.placeholder,
        isEdit = _this$props.isEdit;
      var childList = enumList ? enumList === null || enumList === void 0 ? void 0 : enumList.childList.map(function (item) {
        return _extends({}, item, {
          dictValue: item.dictValue,
          dictKey: item.dictKey,
          label: item.dictValue,
          value: item.dictKey
        });
      }) : selectList;
      isDeployment = isDeployment === undefined ? 'primary' : isDeployment;
      var mode = isMultiple ? 'multiple' : 'single';
      if (isDeployment === 'primary') {
        defaultValue = defaultValue === '' ? [] : defaultValue;
        return /*#__PURE__*/React.createElement(_Select, {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultValue: defaultValue,
          hasClear: true,
          disabled: !isEdit,
          placeholder: placeholder,
          mode: mode,
          dataSource: childList
        });
      } else {
        if (mode === 'multiple') {
          return /*#__PURE__*/React.createElement(_Checkbox.Group, {
            onChange: function onChange(e) {
              return _this.onChange(e);
            },
            defaultValue: defaultValue,
            disabled: !isEdit,
            dataSource: childList
          });
        } else {
          return /*#__PURE__*/React.createElement(_Radio.Group, {
            onChange: function onChange(e) {
              return _this.onChange(e);
            },
            defaultValue: defaultValue,
            disabled: !isEdit,
            dataSource: childList
          });
        }
      }
    };
    _this.state = {
      childList: [],
      selectData: [],
      optionData: []
    };
    return _this;
  }
  var _proto = CommonOption.prototype;
  _proto.componentDidMount = /*#__PURE__*/function () {
    var _componentDidMount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return this.initData();
          case 2:
          case "end":
            return _context2.stop();
        }
      }, _callee2, this);
    }));
    function componentDidMount() {
      return _componentDidMount.apply(this, arguments);
    }
    return componentDidMount;
  }();
  _proto.render = function render() {
    return /*#__PURE__*/React.createElement(_Box, {
      direction: "row",
      align: "center",
      className: bizCssPrefix + '-option'
    }, this.renderColumn());
  };
  return CommonOption;
}(React.Component);
CommonOption.displayName = 'CommonOption';
CommonOption.displayName = 'CommonOption';
export default CommonOption;