'use strict';

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

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 _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 _getClassName2 = require('babel-plugin-react-css-modules/dist/browser/getClassName');

var _getClassName3 = _interopRequireDefault(_getClassName2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _draftJs = require('draft-js');

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _index = require('../../CheckBox/index');

var _index2 = _interopRequireDefault(_index);

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

var _styleModuleImportMap = {
  './rich-text-link-button.scss': {
    'wrapper-controls': 'flive_rich-text-link-button_wrapper-controls',
    'button': 'flive_rich-text-link-button_button',
    'button--active': 'flive_rich-text-link-button_button--active',
    'link-input': 'flive_rich-text-link-button_link-input',
    'input-link': 'flive_rich-text-link-button_input-link',
    'input-target': 'flive_rich-text-link-button_input-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 LinkButton = function (_Component) {
  _inherits(LinkButton, _Component);

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

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

    _this.onToggle = function () {
      var _this$state = _this.state,
          editorState = _this$state.editorState,
          showLinkInput = _this$state.showLinkInput;

      var selection = editorState.getSelection();
      if (!selection.isCollapsed() && showLinkInput === false) {
        _this.setState({
          showLinkInput: true,
          linkValue: ''
        }, function () {
          setTimeout(function () {
            return _this.linkInput.focus();
          }, 0);
        });
      }
    };

    _this.onLinkChange = function (e) {
      _this.setState({ linkValue: e.target.value });
    };

    _this.onConfirmLink = function (e) {
      if (e) e.preventDefault();
      var _this$state2 = _this.state,
          editorState = _this$state2.editorState,
          linkValue = _this$state2.linkValue,
          isTargetBlank = _this$state2.isTargetBlank;

      var attributes = { url: linkValue };
      if (isTargetBlank) {
        attributes = _extends({}, attributes, { target: '_blank' });
      }
      var entityKey = _draftJs.Entity.create('LINK', 'MUTABLE', attributes);
      _this.props.onChange(_draftJs.RichUtils.toggleLink(editorState, editorState.getSelection(), entityKey));
      _this.setState({
        showLinkInput: false,
        linkValue: ''
      });
    };

    _this.onRemoveLink = function (e) {
      if (e) e.preventDefault();
      var editorState = _this.state.editorState;

      var selection = editorState.getSelection();
      if (!selection.isCollapsed()) {
        _this.props.onChange(_draftJs.RichUtils.toggleLink(editorState, selection, null));
        _this.setState({
          showLinkInput: false
        });
      }
    };

    _this.onLinkTargetChange = function (isTargetBlank) {
      _this.setState({ isTargetBlank: !!isTargetBlank });
    };

    _this.onLinkInputKeyDown = function (e) {
      if (e.which === 13) {
        _this.onConfirmLink();
      }
    };

    _this.state = {
      editorState: props.editorState,
      linkValue: '',
      showLinkInput: false,
      isTargetBlank: true
    };
    return _this;
  }

  _createClass(LinkButton, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      if (nextProps.editorState && nextProps.editorState !== this.props.editorState) {
        this.setState({ editorState: nextProps.editorState });
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          label = _props.label,
          className = _props.className,
          active = _props.active;
      var _state = this.state,
          isTargetBlank = _state.isTargetBlank,
          showLinkInput = _state.showLinkInput,
          linkValue = _state.linkValue;

      return _react2.default.createElement(
        'div',
        { className: (className ? className + ' ' : '') + 'flive_rich-text-link-button_wrapper-controls' },
        _react2.default.createElement(
          'button',
          {
            type: 'button',

            onMouseDown: this.onToggle,
            className: (0, _getClassName3.default)((0, _classnames2.default)('button', { 'button--active': active || showLinkInput }), _styleModuleImportMap)
          },
          label
        ),
        showLinkInput && _react2.default.createElement(
          'span',
          { className: 'flive_rich-text-link-button_link-input' },
          _react2.default.createElement('input', {
            className: 'flive_rich-text-link-button_input-link',
            type: 'url',
            ref: function ref(_ref) {
              _this2.linkInput = _ref;
            },
            value: linkValue,
            onChange: this.onLinkChange,
            onKeyDown: this.onLinkInputKeyDown,
            placeholder: 'http://example.com'
          }),
          _react2.default.createElement(
            _index2.default,
            {
              className: 'flive_rich-text-link-button_input-target',
              value: isTargetBlank,
              onChange: this.onLinkTargetChange
            },
            'Target blank'
          ),
          _react2.default.createElement(
            'button',
            { type: 'button', onClick: this.onConfirmLink },
            'OK'
          ),
          _react2.default.createElement(
            'button',
            { type: 'button', onClick: this.onRemoveLink },
            'Remove'
          )
        )
      );
    }
  }]);

  return LinkButton;
}(_react.Component);

LinkButton.propTypes = {
  active: _propTypes2.default.bool,
  label: _propTypes2.default.node,
  onChange: _propTypes2.default.func,
  className: _propTypes2.default.string,
  editorState: _propTypes2.default.object
};

LinkButton.defaultProps = {
  active: false,
  label: null,
  style: null,
  onToggle: null,
  onChange: null,
  className: null,
  editorState: null
};
exports.default = LinkButton;
module.exports = exports['default'];