'use strict';

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

var _createClass = 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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _desc, _value, _class, _VIEW$EDIT; /**
                                        * Created by baoyinghai on 11/17/16.
                                        */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _coreDecorators = require('core-decorators');

var _mxa = require('../../../../../components/mxa');

var _constant = require('../../constant');

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

function _defineProperty(obj, key, value) { 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 _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
  var desc = {};
  Object['ke' + 'ys'](descriptor).forEach(function (key) {
    desc[key] = descriptor[key];
  });
  desc.enumerable = !!desc.enumerable;
  desc.configurable = !!desc.configurable;

  if ('value' in desc || desc.initializer) {
    desc.writable = true;
  }

  desc = decorators.slice().reverse().reduce(function (desc, decorator) {
    return decorator(target, property, desc) || desc;
  }, desc);

  if (context && desc.initializer !== void 0) {
    desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
    desc.initializer = undefined;
  }

  if (desc.initializer === void 0) {
    Object['define' + 'Property'](target, property, desc);
    desc = null;
  }

  return desc;
}

var CheckboxGroup = _mxa.Checkbox.Group;

var MyCheckboxGroup = (_class = function (_React$Component) {
  _inherits(MyCheckboxGroup, _React$Component);

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

    var _this = _possibleConstructorReturn(this, (MyCheckboxGroup.__proto__ || Object.getPrototypeOf(MyCheckboxGroup)).call(this, props));

    _this._changeString2Array = function (str) {
      if (str instanceof Array) {
        return str;
      }
      return str && str.split && str.split(',');
    };

    _this._changeArray2String = function (ary) {
      return ary && ary.join && ary.join(',');
    };

    _this.state = {
      value: _this.props.value
    };
    return _this;
  }

  _createClass(MyCheckboxGroup, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(next) {
      if (next.value !== this.state.value) {
        this.state.value = next.value;
      }
    }
  }, {
    key: '_onChange',
    value: function _onChange(value) {
      var onChange = this.props.onChange;

      var v = this._changeArray2String(value);
      onChange && onChange(v);
      this.setState({
        value: v
      });
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(CheckboxGroup, { options: this.props.options, value: this._changeString2Array(this.state.value), onChange: this._onChange, disabled: this.props.disabled });
    }
  }]);

  return MyCheckboxGroup;
}(_react2.default.Component), (_applyDecoratedDescriptor(_class.prototype, '_onChange', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_onChange'), _class.prototype)), _class);


var compRender = function compRender(config) {
  var record = config.record,
      props = config.props;

  var options = [];
  record.displayComponent && record.displayComponent.dictionaryItems && record.displayComponent.dictionaryItems.forEach(function (i, index) {
    options.push({ label: i.value, value: i.code });
  });
  // console.log('#### checkbox group', options);

  return _react2.default.createElement(MyCheckboxGroup, { options: options, disabled: record.isReadonly || props.isModalInputMappingItem && props.isLeadInto && props.modalInputIsReadonly });
};

/* eslint-disable */

var StaticDisplay = function (_React$Component2) {
  _inherits(StaticDisplay, _React$Component2);

  function StaticDisplay() {
    _classCallCheck(this, StaticDisplay);

    return _possibleConstructorReturn(this, (StaticDisplay.__proto__ || Object.getPrototypeOf(StaticDisplay)).apply(this, arguments));
  }

  _createClass(StaticDisplay, [{
    key: 'render',
    value: function render() {
      var _this3 = this;

      var value = [];
      this.props.opts && this.props.opts.forEach(function (item) {
        if (_this3.props.value && _this3.props.value.indexOf(item.code) >= 0) {
          value.push(item.value);
        }
      });
      return _react2.default.createElement(
        'span',
        null,
        value.join(', ')
      );
    }
  }]);

  return StaticDisplay;
}(_react2.default.Component);

exports.default = (_VIEW$EDIT = {}, _defineProperty(_VIEW$EDIT, _constant.VIEW, function (config) {
  var record = config.record;

  var opts = record.displayComponent && record.displayComponent.dictionaryItems && record.displayComponent.dictionaryItems;
  return _react2.default.createElement(StaticDisplay, { opts: opts });
}), _defineProperty(_VIEW$EDIT, _constant.EDIT, compRender), _VIEW$EDIT);