'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _input = require('../input');

var _input2 = _interopRequireDefault(_input);

var _icon = require('../icon');

var _icon2 = _interopRequireDefault(_icon);

var _modal = require('../modal');

var _modal2 = _interopRequireDefault(_modal);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var defaultModalProps = {
    title: 'Title',
    visible: true
};

var PopEdit = function (_PureComponent) {
    (0, _inherits3['default'])(PopEdit, _PureComponent);

    function PopEdit(props) {
        (0, _classCallCheck3['default'])(this, PopEdit);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (PopEdit.__proto__ || Object.getPrototypeOf(PopEdit)).call(this, props));

        _this.setValue = function (values) {
            if (Array.isArray(values)) {
                return values.map(function (value) {
                    return _this.formatValue(value);
                });
            } else {
                return _this.formatValue(values);
            }
        };
        _this.formatValue = function (val) {
            var _this$props = _this.props,
                _this$props$dataTextF = _this$props.dataTextField,
                dataTextField = _this$props$dataTextF === undefined ? 'name' : _this$props$dataTextF,
                _this$props$dataValue = _this$props.dataValueField,
                dataValueField = _this$props$dataValue === undefined ? 'value' : _this$props$dataValue;

            if (typeof val === 'number') {
                var _ref;

                return _ref = {}, (0, _defineProperty3['default'])(_ref, dataTextField, val.toString()), (0, _defineProperty3['default'])(_ref, dataValueField, val), _ref;
            } else if (typeof val === 'string') {
                var _ref2;

                return _ref2 = {}, (0, _defineProperty3['default'])(_ref2, dataTextField, val), (0, _defineProperty3['default'])(_ref2, dataValueField, val), _ref2;
            } else {
                var _ref3;

                return val ? (_ref3 = {}, (0, _defineProperty3['default'])(_ref3, dataTextField, val[dataTextField]), (0, _defineProperty3['default'])(_ref3, dataValueField, val[dataValueField]), _ref3) : val;
            }
        };
        _this.formmatInputVal = function (valueObj) {
            var _this$props$dataTextF2 = _this.props.dataTextField,
                dataTextField = _this$props$dataTextF2 === undefined ? '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 === undefined ? false : _this$props2$disabled;

            var curInputObj = _this.setValue(value);
            if (!disabled) {
                _this.setState(function (prevState) {
                    return {
                        isVisible: true,
                        inputObj: curInputObj || prevState.inputObj,
                        inputVal: _this.formmatInputVal(curInputObj) || prevState.inputVal
                    };
                });
            }
        };
        _this.handleCancel = function () {
            _this.setState({
                isVisible: false
            });
        };
        _this.handleOk = function () {
            var _this$props3 = _this.props,
                onOk = _this$props3.modalProps.onOk,
                onChange = _this$props3.onChange;

            onOk && onOk(_this.state.inputObj);
            onChange && JSON.stringify(_this.state.inputObj) !== JSON.stringify(_this.state.currentValue) && onChange(_this.state.inputObj);
            var curInputObj = _this.setValue(_this.state.inputObj);
            _this.setState({
                isVisible: false,
                inputVal: _this.formmatInputVal(curInputObj)
            });
        };
        _this.handleValue = function (val) {
            _this.setState({
                inputObj: _this.setValue(val)
            });
        };
        _this.renderInput = function () {
            var _this$props4 = _this.props,
                inputProps = _this$props4.inputProps,
                _this$props4$disabled = _this$props4.disabled,
                disabled = _this$props4$disabled === undefined ? false : _this$props4$disabled,
                _this$props4$classNam = _this$props4.className,
                className = _this$props4$classNam === undefined ? '' : _this$props4$classNam;

            var value = _this.setValue(_this.props.value);
            var defaultValue = _this.setValue(_this.props.defaultValue);
            var defaultInputProps = disabled ? (0, _extends3['default'])((0, _extends3['default'])({}, inputProps), { value: defaultValue && _this.formmatInputVal(defaultValue) ? _this.formmatInputVal(defaultValue) : _this.formmatInputVal(value), disabled: disabled }) : (0, _extends3['default'])((0, _extends3['default'])({}, inputProps), { value: value ? _this.formmatInputVal(value) : _this.state.inputVal });
            var inputRender = _react2['default'].createElement(_input2['default'], (0, _extends3['default'])({}, defaultInputProps, { className: className, suffix: _react2['default'].createElement(_icon2['default'], { type: 'select', 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;
        var defaultVal = _this.formmatInputVal(defalutObj);
        _this.state = {
            isVisible: false,
            inputObj: defalutObj,
            inputVal: defaultVal || '',
            currentValue: defalutObj
        };
        return _this;
    }

    (0, _createClass3['default'])(PopEdit, [{
        key: 'componentDidUpdate',
        value: function componentDidUpdate(prevProps) {
            var _props = this.props,
                value = _props.value,
                onChange = _props.onChange;

            var newVal = JSON.stringify(this.setValue(value));
            var oldVal = JSON.stringify(this.setValue(prevProps.value));
            if (newVal !== oldVal) {
                onChange && onChange(value);
            }
            this.setState({
                currentValue: value
            });
        }
    }, {
        key: 'render',
        value: function render() {
            var _this2 = this;

            var modalProps = this.props.modalProps;
            var isVisible = this.state.isVisible;

            var modalNewProps = (0, _extends3['default'])((0, _extends3['default'])({}, defaultModalProps), { onCancel: this.handleCancel });
            Object.keys(modalProps).forEach(function (key) {
                if (key !== 'render' && key !== 'onOk') modalNewProps[key] = modalProps[key];
            });
            return _react2['default'].createElement(
                _react.Fragment,
                null,
                this.renderInput(),
                isVisible && _react2['default'].createElement(
                    _modal2['default'],
                    (0, _extends3['default'])({ visible: true }, modalNewProps, { onOk: function onOk() {
                            return _this2.handleOk();
                        } }),
                    modalProps.render(this.handleValue)
                )
            );
        }
    }]);
    return PopEdit;
}(_react.PureComponent);

PopEdit.propTypes = {
    inputProps: _propTypes2['default'].object,
    formProps: _propTypes2['default'].object,
    modalProps: _propTypes2['default'].object
};
exports['default'] = PopEdit;
module.exports = exports['default'];