"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _input = _interopRequireDefault(require("antd/lib/input"));
var _modal = _interopRequireDefault(require("antd/lib/modal"));
var _icons = require("@fishx/icons");
var _propTypes = _interopRequireDefault(require("prop-types"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var defaultModalProps = {
  title: 'Title',
  visible: true
};
var PopEdit = /*#__PURE__*/function (_PureComponent) {
  _inherits(PopEdit, _PureComponent);
  var _super = _createSuper(PopEdit);
  function PopEdit(props) {
    var _this;
    _classCallCheck(this, PopEdit);
    _this = _super.call(this, props);
    _this.setValue = function (values, isClear) {
      if (Array.isArray(values)) {
        return values.map(function (value) {
          return _this.formatValue(value, isClear);
        });
      } else {
        return _this.formatValue(values, isClear);
      }
    };
    _this.formatValue = function (val, isClear) {
      var _this$props = _this.props,
        _this$props$dataTextF = _this$props.dataTextField,
        dataTextField = _this$props$dataTextF === void 0 ? 'name' : _this$props$dataTextF,
        _this$props$dataValue = _this$props.dataValueField,
        dataValueField = _this$props$dataValue === void 0 ? 'value' : _this$props$dataValue;
      if (typeof val === 'number') {
        var _ref, _ref2;
        // @ts-ignore
        return isClear ? (_ref = {}, _defineProperty(_ref, dataTextField, ''), _defineProperty(_ref, dataValueField, null), _ref) : (_ref2 = {}, _defineProperty(_ref2, dataTextField, val.toString()), _defineProperty(_ref2, dataValueField, val), _ref2);
      } else if (typeof val === 'string') {
        var _ref3, _ref4;
        return isClear ? (_ref3 = {}, _defineProperty(_ref3, dataTextField, ''), _defineProperty(_ref3, dataValueField, ''), _ref3) : (_ref4 = {}, _defineProperty(_ref4, dataTextField, val), _defineProperty(_ref4, dataValueField, val), _ref4);
      } else {
        var _ref5, _ref6;
        return val ? isClear ? (_ref5 = {}, _defineProperty(_ref5, dataTextField, ''), _defineProperty(_ref5, dataValueField, undefined), _ref5) : (_ref6 = {}, _defineProperty(_ref6, dataTextField, val[dataTextField]), _defineProperty(_ref6, dataValueField, val[dataValueField]), _ref6) : val;
      }
    };
    _this.formmatInputVal = function (valueObj) {
      var _this$props$dataTextF2 = _this.props.dataTextField,
        dataTextField = _this$props$dataTextF2 === void 0 ? 'name' : _this$props$dataTextF2;
      if (valueObj && Array.isArray(valueObj)) {
        var valArr = valueObj.map(function (val) {
          return val[dataTextField];
        });
        return valArr.join('/');
      } else {
        return valueObj === null || valueObj === void 0 ? void 0 : valueObj[dataTextField];
      }
    };
    _this.onOpen = function () {
      var _this$props2 = _this.props,
        value = _this$props2.value,
        _this$props2$disabled = _this$props2.disabled,
        disabled = _this$props2$disabled === void 0 ? false : _this$props2$disabled;
      var curInputObj = _this.setValue(value);
      if (!disabled) {
        _this.setState(function (prevState) {
          return {
            isVisible: true,
            inputObj: curInputObj || prevState.inputObj,
            // @ts-ignore
            inputVal: _this.formmatInputVal(curInputObj) || prevState.inputVal
          };
        });
      }
    };
    _this.handleCancel = function () {
      _this.setState({
        isVisible: false
      });
    };
    _this.handleOk = function () {
      // @ts-ignore
      var _this$props3 = _this.props,
        _this$props3$modalPro = _this$props3.modalProps,
        _this$props3$modalPro2 = _this$props3$modalPro === void 0 ? {} : _this$props3$modalPro,
        onOk = _this$props3$modalPro2.onOk,
        onChange = _this$props3.onChange;
      onOk && onOk(_this.state.inputObj);
      onChange && onChange(_this.state.inputObj);
      var curInputObj = _this.setValue(_this.state.inputObj);
      _this.setState({
        isVisible: false,
        // @ts-ignore
        inputVal: _this.formmatInputVal(curInputObj)
      });
    };
    _this.handleValue = function (val) {
      _this.setState({
        inputObj: _this.setValue(val)
      });
    };
    _this.handleClear = function () {
      var _this$props4 = _this.props,
        _this$props4$modalPro = _this$props4.modalProps,
        _this$props4$modalPro2 = _this$props4$modalPro === void 0 ? {} : _this$props4$modalPro,
        onOk = _this$props4$modalPro2.onOk,
        onChange = _this$props4.onChange;
      var clearObj = _this.setValue(_this.state.inputObj, true);
      onOk && onOk(clearObj);
      onChange && onChange(clearObj);
      _this.setState({
        isVisible: false,
        // @ts-ignore
        inputVal: _this.formmatInputVal(clearObj)
      });
    };
    _this.renderInput = function () {
      var _this$props5 = _this.props,
        inputProps = _this$props5.inputProps,
        _this$props5$disabled = _this$props5.disabled,
        disabled = _this$props5$disabled === void 0 ? false : _this$props5$disabled,
        _this$props5$classNam = _this$props5.className,
        className = _this$props5$classNam === void 0 ? '' : _this$props5$classNam;
      var value = _this.setValue(_this.props.value);
      var defaultValue = _this.setValue(_this.props.defaultValue);
      var defaultInputProps = disabled ? _objectSpread(_objectSpread({}, inputProps), {}, {
        // @ts-ignore
        value: defaultValue && _this.formmatInputVal(defaultValue) ? _this.formmatInputVal(defaultValue) : _this.formmatInputVal(value),
        disabled: disabled
      }) : _objectSpread(_objectSpread({}, inputProps), {}, {
        // @ts-ignore
        value: value ? _this.formmatInputVal(value) : _this.state.inputVal
      });
      var inputRender = /*#__PURE__*/_react.default.createElement(_input.default, _extends({}, defaultInputProps, {
        className: className,
        suffix: defaultInputProps.allowClear ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_icons.CloseCircleFilled, {
          onClick: _this.handleClear
        }), /*#__PURE__*/_react.default.createElement(_icons.SelectOutlined, {
          onClick: _this.onOpen
        })) : /*#__PURE__*/_react.default.createElement(_icons.SelectOutlined, {
          onClick: _this.onOpen
        }),
        onClick: _this.onOpen,
        readOnly: true
      }));
      return inputRender;
    };
    var _value = props.value,
      _defaultValue = props.defaultValue;
    var defalutObj = _value ? _this.setValue(_value) : _defaultValue ? _this.setValue(_defaultValue) : null;
    // @ts-ignore
    var defaultVal = _this.formmatInputVal(defalutObj);
    _this.state = {
      isVisible: false,
      inputObj: defalutObj,
      inputVal: defaultVal || ''
    };
    return _this;
  }
  // @ts-ignore
  _createClass(PopEdit, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var _this$props6 = this.props,
        value = _this$props6.value,
        onChange = _this$props6.onChange;
      var newVal = JSON.stringify(this.setValue(value));
      var oldVal = JSON.stringify(this.setValue(prevProps.value));
      if (newVal !== oldVal) {
        onChange && onChange(value);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;
      var modalProps = this.props.modalProps;
      var isVisible = this.state.isVisible;
      var modalNewProps = _objectSpread(_objectSpread({}, defaultModalProps), {}, {
        onCancel: this.handleCancel
      });
      // @ts-ignore
      Object.keys(modalProps).forEach(function (key) {
        // @ts-ignore
        if (key !== 'render' && key !== 'onOk') modalNewProps[key] = modalProps[key];
      });
      return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, this.renderInput(), isVisible &&
      /*#__PURE__*/
      // @ts-ignore
      _react.default.createElement(_modal.default
      // @ts-ignore
      , _extends({
        visible: true
      }, modalNewProps, {
        onOk: function onOk() {
          return _this2.handleOk();
        }
      }),
      // @ts-ignore
      modalProps.render(this.handleValue)));
    }
  }]);
  return PopEdit;
}(_react.PureComponent);
PopEdit.propTypes = {
  inputProps: _propTypes.default.object,
  formProps: _propTypes.default.object,
  modalProps: _propTypes.default.object
};
var _default = PopEdit;
exports.default = _default;