import _extends from 'babel-runtime/helpers/extends';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import React, { PureComponent, Fragment } from "react";
import Input from '../input';
import Icon from '../icon';
import Modal from '../modal';
import PropTypes from 'prop-types';
var defaultModalProps = {
    title: 'Title',
    visible: true
};

var PopEdit = function (_PureComponent) {
    _inherits(PopEdit, _PureComponent);

    function PopEdit(props) {
        _classCallCheck(this, PopEdit);

        var _this = _possibleConstructorReturn(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 = {}, _defineProperty(_ref, dataTextField, val.toString()), _defineProperty(_ref, dataValueField, val), _ref;
            } else if (typeof val === 'string') {
                var _ref2;

                return _ref2 = {}, _defineProperty(_ref2, dataTextField, val), _defineProperty(_ref2, dataValueField, val), _ref2;
            } else {
                var _ref3;

                return val ? (_ref3 = {}, _defineProperty(_ref3, dataTextField, val[dataTextField]), _defineProperty(_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 && 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 ? _extends(_extends({}, inputProps), { value: defaultValue && _this.formmatInputVal(defaultValue) ? _this.formmatInputVal(defaultValue) : _this.formmatInputVal(value), disabled: disabled }) : _extends(_extends({}, inputProps), { value: value ? _this.formmatInputVal(value) : _this.state.inputVal });
            var inputRender = React.createElement(Input, _extends({}, defaultInputProps, { className: className, suffix: React.createElement(Icon, { 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 || ''
        };
        return _this;
    }

    _createClass(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);
            }
        }
    }, {
        key: 'render',
        value: function render() {
            var _this2 = this;

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

            var modalNewProps = _extends(_extends({}, defaultModalProps), { onCancel: this.handleCancel });
            Object.keys(modalProps).forEach(function (key) {
                if (key !== 'render' && key !== 'onOk') modalNewProps[key] = modalProps[key];
            });
            return React.createElement(
                Fragment,
                null,
                this.renderInput(),
                isVisible && React.createElement(
                    Modal,
                    _extends({ visible: true }, modalNewProps, { onOk: function onOk() {
                            return _this2.handleOk();
                        } }),
                    modalProps.render(this.handleValue)
                )
            );
        }
    }]);

    return PopEdit;
}(PureComponent);

PopEdit.propTypes = {
    inputProps: PropTypes.object,
    formProps: PropTypes.object,
    modalProps: PropTypes.object
};
export default PopEdit;