import "choerodon-ui/lib/button/style";
import _Button from "choerodon-ui/lib/button";
import "choerodon-ui/lib/select/style";
import _Select from "choerodon-ui/lib/select";
import "choerodon-ui/lib/form/style";
import _Form from "choerodon-ui/lib/form";

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

/* eslint-disable */
import React, { Fragment, useCallback, useMemo } from 'react';
import { injectIntl } from 'react-intl';
import map from 'lodash/map';
import findKey from 'lodash/findKey';
import omit from 'lodash/omit';
import "./index.less";
var FormItem = _Form.Item;
var formItemLayout = {
  wrapperCol: {
    xs: {
      span: 24
    },
    sm: {
      span: 24
    }
  }
}; // eslint-disable-next-line func-names

var selectKeyGen = /*#__PURE__*/regeneratorRuntime.mark(function _callee(id) {
  var num;
  return regeneratorRuntime.wrap(function _callee$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          num = 0;

        case 1:
          if (!true) {
            _context.next = 9;
            break;
          }

          // eslint-disable-next-line no-plusplus
          num += 1;

          if (!(num > 10000)) {
            _context.next = 5;
            break;
          }

          return _context.abrupt("break", 9);

        case 5:
          _context.next = 7;
          return "key".concat(id++);

        case 7:
          _context.next = 1;
          break;

        case 9:
        case "end":
          return _context.stop();
      }
    }
  }, _callee);
})(1);

function getSelectKey() {
  return selectKeyGen.next().value;
}

function getRepeatKey(data, value, field) {
  return findKey(omit(data, ['keys', field]), function (item) {
    return item === value;
  });
}

export var SimpleSelect = injectIntl(function (_ref) {
  var intl = _ref.intl,
      uid = _ref.uid,
      label = _ref.label,
      options = _ref.options,
      onDelete = _ref.onDelete,
      removeable = _ref.removeable,
      form = _ref.form,
      notFoundContent = _ref.notFoundContent,
      requireText = _ref.requireText;
  var handleDelete = useCallback(function () {
    onDelete(uid);
  }, [uid]);

  function uniqValid(rule, value, callback) {
    if (!value) callback(requireText);
    var field = rule.field;
    var data = form.getFieldsValue();
    var repeatKey = getRepeatKey(data, value, field);

    if (repeatKey) {
      var text = intl.formatMessage({
        id: 'noRepeat'
      });
      callback(text);
    }

    callback();
  }

  return /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formItemLayout, {
    className: "c7ncd-dynamic-select",
    key: uid
  }), form.getFieldDecorator(uid, {
    rules: [{
      required: true,
      validator: uniqValid
    }]
  })( /*#__PURE__*/React.createElement(_Select, {
    required: true,
    searchable: true,
    filter: true,
    optionFilterProp: "children",
    notFoundContent: notFoundContent,
    filterOption: function filterOption(input, option) {
      return option.props.children.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    },
    label: label,
    getPopupContainer: function getPopupContainer(triggerNode) {
      return triggerNode.parentNode;
    }
  }, options)), /*#__PURE__*/React.createElement(_Button, {
    className: "c7ncd-dynamic-select-remove",
    disabled: !removeable,
    shape: "circle",
    icon: "delete",
    onClick: handleDelete
  }));
});
SimpleSelect.defaultProps = {
  label: '',
  options: []
};
export default function DynamicSelect(_ref2) {
  var form = _ref2.form,
      label = _ref2.label,
      fieldKeys = _ref2.fieldKeys,
      options = _ref2.options,
      addText = _ref2.addText,
      notFoundContent = _ref2.notFoundContent,
      requireText = _ref2.requireText;

  function add() {
    var keys = form.getFieldValue('keys');
    if (!keys) return;
    var newKey = getSelectKey();
    var nextKeys = keys.concat(newKey);
    form.setFieldsValue({
      keys: nextKeys
    });
  }

  function remove(k) {
    var keys = form.getFieldValue('keys');

    if (keys.length === 1) {
      return;
    }

    form.setFieldsValue(_defineProperty({
      keys: keys.filter(function (key) {
        return key !== k;
      })
    }, k, undefined));
  }

  var keys = useMemo(function () {
    return fieldKeys.keys;
  }, [fieldKeys]); // const disabledAdd = options.length <= 1 || (keys && keys.length >= options.length);

  return /*#__PURE__*/React.createElement(React.Fragment, null, map(keys, function (key, index) {
    return /*#__PURE__*/React.createElement(SimpleSelect, {
      key: key,
      uid: key,
      form: form,
      notFoundContent: notFoundContent,
      requireText: requireText,
      options: options,
      removeable: index > 0 || keys && keys.length > 1,
      onDelete: remove,
      label: label
    });
  }), /*#__PURE__*/React.createElement(FormItem, null, /*#__PURE__*/React.createElement(_Button, {
    icon: "add",
    type: "primary",
    funcType: "flat",
    onClick: add
  }, addText)));
}
DynamicSelect.defaultProps = {
  label: '',
  addText: ''
};