"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireWildcard(require("react"));
var _lowcodeEngine = require("@alilc/lowcode-engine");
require("./index.less");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var ClassNameView = exports["default"] = /*#__PURE__*/function (_PureComponent) {
  function ClassNameView() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _PureComponent.call.apply(_PureComponent, [this].concat(args)) || this;
    _this.state = {
      dataSource: [],
      selectValue: ''
    };
    _this.getClassNameList = function () {
      var _ClassNameView$defaul, _ClassNameView$defaul2;
      var schema = (ClassNameView === null || ClassNameView === void 0 ? void 0 : (_ClassNameView$defaul = ClassNameView.defaultProps) === null || _ClassNameView$defaul === void 0 ? void 0 : (_ClassNameView$defaul2 = _ClassNameView$defaul.getSchema) === null || _ClassNameView$defaul2 === void 0 ? void 0 : _ClassNameView$defaul2.call(_ClassNameView$defaul)) || _lowcodeEngine.project.exportSchema();
      var css = schema.componentsTree[0].css;
      return (css === null || css === void 0 ? void 0 : css.match(/(?<=\.)\w+(?=\s*[{,])/g)) || [];
    };
    _this.setClassNameSetterData = function () {
      var _value$split;
      var value = _this.props.value;
      var classNameList = _this.getClassNameList();
      var dataSource = [];
      classNameList.map(function (item) {
        dataSource.push({
          value: item,
          label: item
        });
        return item;
      });
      var selectValue = (value === null || value === void 0 ? void 0 : (_value$split = value.split) === null || _value$split === void 0 ? void 0 : _value$split.call(value, ' ')) || [];
      selectValue.forEach(function (current) {
        if (!classNameList.some(function (cls) {
          return cls === current;
        })) {
          dataSource.push({
            value: current,
            label: current
          });
        }
      });
      _this.setState({
        dataSource: dataSource,
        selectValue: selectValue
      });
    };
    _this.handleChange = function (value) {
      var onChange = _this.props.onChange;
      onChange(value.join(' '));
      _this.setState({
        selectValue: value
      });
    };
    return _this;
  }
  (0, _inheritsLoose2["default"])(ClassNameView, _PureComponent);
  var _proto = ClassNameView.prototype;
  _proto.componentWillMount = function componentWillMount() {
    this.setClassNameSetterData();
  };
  _proto.render = function render() {
    var _this$state = this.state,
      dataSource = _this$state.dataSource,
      selectValue = _this$state.selectValue;
    return /*#__PURE__*/_react["default"].createElement(_select["default"], {
      className: "ClassNameSetter_Select",
      style: {
        width: '100%'
      },
      mode: "multiple",
      dataSource: dataSource,
      onFocus: this.setClassNameSetterData,
      onChange: this.handleChange,
      value: selectValue
    });
  };
  return ClassNameView;
}(_react.PureComponent);
ClassNameView.display = 'ClassName';
ClassNameView.defaultProps = {};