"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _balloon = _interopRequireDefault(require("@alifd/next/lib/balloon"));
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireWildcard(require("react"));
require("./index.less");
var _locale = require("./locale");
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; }
// import PropTypes from 'prop-types';

var icons = ['smile', 'cry', 'success', 'warning', 'prompt', 'error', 'help', 'clock', 'success-filling', 'delete-filling', 'favorites-filling', 'add', 'minus', 'arrow-up', 'arrow-down', 'arrow-left', 'arrow-right', 'arrow-double-left', 'arrow-double-right', 'switch', 'sorting', 'descending', 'ascending', 'select', 'semi-select', 'loading', 'search', 'close', 'ellipsis', 'picture', 'calendar', 'ashbin', 'upload', 'download', 'set', 'edit', 'refresh', 'filter', 'attachment', 'account', 'email', 'atm', 'copy', 'exit', 'eye', 'eye-close', 'toggle-left', 'toggle-right', 'lock', 'unlock', 'chart-pie', 'chart-bar', 'form', 'detail', 'list', 'dashboard'];
var IconSetter = exports["default"] = /*#__PURE__*/function (_PureComponent) {
  function IconSetter() {
    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 = {
      setterValue: null
    };
    _this._onChange = function (icon) {
      var _this$props = _this.props,
        onChange = _this$props.onChange,
        type = _this$props.type;
      if (type === 'string') {
        onChange(icon);
      } else if (type === 'node') {
        onChange({
          componentName: 'Icon',
          props: {
            type: icon
          }
        });
      }
    };
    _this.onInputChange = function (icon) {
      _this._onChange(icon);
    };
    _this.onSelectIcon = function (icon) {
      _this._onChange(icon);
    };
    return _this;
  }
  (0, _inheritsLoose2["default"])(IconSetter, _PureComponent);
  IconSetter.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) {
    var value = nextProps.value,
      defaultValue = nextProps.defaultValue;
    if (prevState.setterValue == null) {
      if (value === undefined && defaultValue) {
        return {
          setterValue: defaultValue
        };
      }
    }
    return {
      setterValue: value
    };
  };
  var _proto = IconSetter.prototype;
  _proto.render = function render() {
    var _setterValue$props,
      _this2 = this;
    var _this$props2 = this.props,
      placeholder = _this$props2.placeholder,
      hasClear = _this$props2.hasClear;
    var setterValue = this.state.setterValue;
    var _value = typeof setterValue === 'object' ? setterValue === null || setterValue === void 0 ? void 0 : (_setterValue$props = setterValue.props) === null || _setterValue$props === void 0 ? void 0 : _setterValue$props.type : setterValue;
    var currentIcon = /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      size: "xs",
      type: _value
    });
    var clearIcon = hasClear && /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      size: "xs",
      id: "icon-clear",
      type: "delete-filling",
      onClick: function onClick(e) {
        e.preventDefault();
        e.stopPropagation();
        _this2.onSelectIcon('');
      }
    });
    var triggerNode = /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_input["default"], {
      size: "small",
      placeholder: placeholder,
      addonTextBefore: currentIcon,
      onChange: this.onInputChange,
      value: _value,
      readOnly: true,
      addonTextAfter: clearIcon
    }));
    var InnerBeforeNode = /*#__PURE__*/_react["default"].createElement(_balloon["default"], {
      className: 'lowcode-icon-content',
      trigger: triggerNode,
      needAdjust: true,
      triggerType: "click",
      closable: false,
      alignEdge: true,
      align: "l",
      popupClassName: "lowcode-icon-setter-popup"
    }, /*#__PURE__*/_react["default"].createElement("ul", {
      className: "lowcode-icon-list"
    }, icons.map(function (icon) {
      return /*#__PURE__*/_react["default"].createElement("li", {
        key: icon,
        onClick: function onClick() {
          return _this2.onSelectIcon(icon);
        }
      }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
        type: icon,
        size: "medium"
      }));
    })));
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "lc-icon-setter"
    }, InnerBeforeNode);
  };
  return IconSetter;
}(_react.PureComponent);
IconSetter.defaultProps = {
  value: undefined,
  type: 'string',
  defaultValue: '',
  hasClear: true,
  icons: icons,
  placeholder: "" + (0, _locale.intl)('PleaseClickToSelectIcon'),
  onChange: function onChange() {
    return undefined;
  }
};
IconSetter.displayName = 'IconSetter';