'use strict';

exports.__esModule = true;
exports.default = undefined;

var _extends = Object.assign || 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; };

var _class, _temp, _initialiseProps;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactDom = require('react-dom');

var _Carrousel = require('./../Carrousel');

var _Carrousel2 = _interopRequireDefault(_Carrousel);

var _InputPre = require('./../InputPre');

var _InputPre2 = _interopRequireDefault(_InputPre);

var _Button = require('./../Button');

var _Button2 = _interopRequireDefault(_Button);

var _instance = require('./instance.js');

var _instance2 = _interopRequireDefault(_instance);

var _instanceData = require('./instance.data.js');

var _instanceData2 = _interopRequireDefault(_instanceData);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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; }

var Emoji = (_temp = _class = function (_Component) {
  _inherits(Emoji, _Component);

  // 全局配置
  function Emoji(props, context) {
    _classCallCheck(this, Emoji);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));

    _initialiseProps.call(_this);

    return _this;
  }
  // 表情

  // 过滤已经回调的属性


  Emoji.prototype.render = function render() {
    var _this2 = this;

    // 全局配置
    var _context$locale = this.context.locale,
        locale = _context$locale === undefined ? {} : _context$locale;

    var _props = this.props,
        portal = _props.portal,
        show = _props.show,
        data = _props.data,
        autoFocus = _props.autoFocus,
        value = _props.value,
        _props$placeholder = _props.placeholder,
        placeholder = _props$placeholder === undefined ? locale['say_something'] || '说点什么吧...' : _props$placeholder,
        _props$maskAttribute = _props.maskAttribute,
        maskAttribute = _props$maskAttribute === undefined ? {} : _props$maskAttribute,
        _props$submitProps = _props.submitProps,
        submitProps = _props$submitProps === undefined ? {} : _props$submitProps,
        _props$inputProps = _props.inputProps,
        inputProps = _props$inputProps === undefined ? {} : _props$inputProps,
        _props$carrouselProps = _props.carrouselProps,
        carrouselProps = _props$carrouselProps === undefined ? {} : _props$carrouselProps,
        liconAttribute = _props.liconAttribute,
        licon = _props.licon,
        onChange = _props.onChange,
        others = _objectWithoutProperties(_props, ['portal', 'show', 'data', 'autoFocus', 'value', 'placeholder', 'maskAttribute', 'submitProps', 'inputProps', 'carrouselProps', 'liconAttribute', 'licon', 'onChange']);

    // 剔除掉onClick事件, 因为在instance时已经回调了


    maskAttribute = this.filterProps(maskAttribute);
    submitProps = this.filterProps(submitProps);
    return (0, _reactDom.createPortal)(_react2.default.createElement(
      'div',
      _extends({ ref: function ref(el) {
          _this2.$el = el;
        } }, maskAttribute, { className: 'mask emoji-mask' + (show ? ' active' : '') + (maskAttribute.className ? ' ' + maskAttribute.className : '') }),
      _react2.default.createElement(
        'div',
        _extends({}, others, { className: 'emoji active' + (others.className ? ' ' + others.className : '') }),
        _react2.default.createElement(
          'div',
          { className: 'emoji-edit' },
          licon,
          liconAttribute && _react2.default.createElement('i', _extends({}, liconAttribute, { className: 'icon' + (liconAttribute.className ? ' ' + liconAttribute.className : '') })),
          _react2.default.createElement(_InputPre2.default, _extends({
            ref: '$ComponentInputPre',
            className: 'emoji-edit-input',
            inputAttribute: { style: { padding: '0' } },
            value: value,
            onChange: onChange,
            placeholder: placeholder
          }, inputProps)),
          _react2.default.createElement('i', { ref: function ref(el) {
              _this2.$icon = el;
            }, className: 'icon emoji-edit-icon' }),
          _react2.default.createElement(
            _Button2.default,
            _extends({ ref: '$ComponentSubmit' }, submitProps, { className: 'emoji-edit-submit' + (submitProps.className ? ' ' + submitProps.className : ''), disabled: !value }),
            submitProps.caption || locale['submit'] || '提交'
          )
        ),
        _react2.default.createElement(
          _Carrousel2.default,
          _extends({ ref: '$ComponentCarrousel' }, carrouselProps, { pagination: true, className: 'carrousel-container emoji-carrousel' + (carrouselProps.className ? ' ' + carrouselProps.className : ''), style: { display: 'none' } }),
          this.getFaceDOM()
        )
      )
    ), portal || this.context.portal || document.getElementById('root') || document.body);
  };

  return Emoji;
}(_react.Component), _class.contextTypes = {
  locale: _propTypes2.default.object,
  portal: _propTypes2.default.object
}, _class.defaultProps = {
  autoFocus: false,
  data: _instanceData2.default
}, _initialiseProps = function _initialiseProps() {
  var _this3 = this;

  this.componentDidUpdate = function (prevProps) {
    // 自动获取焦点
    if (_this3.props.autoFocus && prevProps.show !== _this3.props.show && _this3.props.show === true && _this3.$input) {
      _this3.$input.focus();
    }
  };

  this.componentDidMount = function () {
    // 输入框
    _this3.$inputEl = _this3.refs.$ComponentInputPre.$el;
    _this3.$input = _this3.refs.$ComponentInputPre.$input;
    // 提交按钮
    _this3.$submit = _this3.refs.$ComponentSubmit.$el;
    // 滑动表情框
    _this3.$carrousel = _this3.refs.$ComponentCarrousel.$el;
    if (_this3.props.autoFocus && _this3.$input) _this3.$input.focus();
    if (_this3.instance) return;
    var _props2 = _this3.props,
        _props2$maskAttribute = _props2.maskAttribute,
        maskAttribute = _props2$maskAttribute === undefined ? {} : _props2$maskAttribute,
        _props2$submitProps = _props2.submitProps,
        submitProps = _props2$submitProps === undefined ? {} : _props2$submitProps,
        _onChange = _props2.onChange;

    _this3.instance = new _instance2.default({
      data: _this3.props.data,

      mask: _this3.$el,
      isClickMaskHide: false,
      onClickMask: maskAttribute.onClick ? function (s) {
        return maskAttribute.onClick(s.event);
      } : null,

      onClickSubmit: submitProps.onClick ? function (s, value) {
        return submitProps.onClick(s.event, value);
      } : null,

      onChange: function onChange(s, value) {
        if (_onChange) {
          _onChange(s.event, value);
          // 自增高
          if (_this3.refs.$ComponentInputPre && _this3.refs.$ComponentInputPre.$ComponentInputText) {
            _this3.refs.$ComponentInputPre.$ComponentInputText.preAutoSize();
          }
        }
      }
    });
  };

  this.getFaceDOM = function () {
    var data = _this3.props.data;
    // icons分页变量

    var icons = [];
    var page = 0;
    var index = 0;
    var count = 23;
    // icons分页
    for (var name in data) {
      if (index !== 0 && index % count === 0) {
        page++;
      }
      if (!icons[page]) icons[page] = [];
      icons[page].push({ key: name, value: data[name] });
      index++;
    }
    // 生成DOM
    return icons.map(function (icon, i) {
      return _react2.default.createElement(
        'div',
        { key: 'page' + i, className: 'emoji-carrousel-slide' },
        icon.map(function (item, index) {
          return _react2.default.createElement(
            'a',
            { key: 'face' + index, className: 'emoji-face', 'data-emoji': item.value, title: item.key },
            '\xA0'
          );
        }),
        _react2.default.createElement(
          'a',
          { className: 'emoji-face-delete' },
          '\xA0'
        )
      );
    });
  };

  this.filterProps = function (props) {
    if (!props) return props;
    var propsed = {};
    for (var n in props) {
      if (n !== 'onClick') {
        propsed[n] = props[n];
      }
    }
    return propsed;
  };
}, _temp);
exports.default = Emoji;
Emoji.propTypes = process.env.NODE_ENV !== "production" ? {
  portal: _propTypes2.default.object,
  show: _propTypes2.default.bool, // ios内核必须隐藏, 不能移除dom, 弹出时才不会有bug, 所以必须用show
  data: _propTypes2.default.object,

  autoFocus: _propTypes2.default.bool,
  value: _propTypes2.default.string,
  placeholder: _propTypes2.default.string,

  maskAttribute: _propTypes2.default.object,
  submitProps: _propTypes2.default.object,
  inputProps: _propTypes2.default.object,
  carrouselProps: _propTypes2.default.object,
  liconAttribute: _propTypes2.default.object,
  licon: _propTypes2.default.node,

  onChange: _propTypes2.default.func
} : {};
module.exports = exports['default'];