'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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _Input = require('../Input/Input');

var _Input2 = _interopRequireDefault(_Input);

var _ColorLuminance = require('../internal/ColorLuminance');

var _ColorLuminance2 = _interopRequireDefault(_ColorLuminance);

var _reactColor = require('react-color');

var _reactClickOutside = require('react-click-outside');

var _reactClickOutside2 = _interopRequireDefault(_reactClickOutside);

var _throttle = require('lodash/throttle');

var _throttle2 = _interopRequireDefault(_throttle);

var _style = require('./style.scss');

var _style2 = _interopRequireDefault(_style);

var _bind = require('classnames/bind');

var _bind2 = _interopRequireDefault(_bind);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; }

/**
 * The ColorPicker component.
 */

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

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

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(ColorPicker).call(this, props));

    _this.state = {
      displayColorPicker: false,
      color: ''
    };

    _this.componentWillMount = function () {
      if (typeof _this.props.value !== 'undefined') {
        _this.setState({ color: _this.props.value });
      }
    };

    _this.componentWillReceiveProps = function (nextProps) {
      if (nextProps.value !== _this.props.value) {
        _this.setState({ color: nextProps.value });
      }
    };

    _this.handleClick = function (event) {
      _this.setState({ displayColorPicker: !_this.state.displayColorPicker });
    };

    _this.handlePickerChange = function (color) {
      var newColor = color.hex;

      _this.setState({ color: newColor }, function () {
        if (typeof this.props.changeCallback === 'function') {
          this.throttle(this.props.changeCallback, {
            target: {
              name: this.props.name,
              value: newColor
            }
          });
        }
      });
    };

    _this.handleInputChange = function (event) {
      var newColor = '';

      if (event.target.value && !event.target.value.startsWith('#')) {
        newColor = '#';
      }

      newColor += event.target.value;

      _this.setState({ color: newColor }, function () {
        if (typeof this.props.changeCallback === 'function') {
          this.props.changeCallback({
            target: {
              name: this.props.name,
              value: newColor
            }
          });
        }
      });
    };

    _this.throttle = (0, _throttle2.default)(function (fn, data) {
      fn(data);
    }, 200);
    return _this;
  }

  _createClass(ColorPicker, [{
    key: 'handleClickOutside',
    value: function handleClickOutside() {
      this.setState({ displayColorPicker: false });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var cx = _bind2.default.bind(_style2.default);
      var componentClass = cx(_style2.default['colorpicker-component'], this.props.optClass);
      var colorPreviewClass = cx(_style2.default['color-preview'], this.state.color ? '' : 'empty');

      return _react2.default.createElement(
        'div',
        { className: componentClass },
        _react2.default.createElement(_Input2.default, {
          value: this.state.color.toUpperCase(),
          placeholder: 'Click to choose a color',
          onClick: this.handleClick,
          changeCallback: this.handleInputChange
        }),
        _react2.default.createElement('div', {
          className: colorPreviewClass,
          style: { backgroundColor: this.state.color, border: '1px solid ' + (0, _ColorLuminance2.default)(this.state.color || '#ffffff', -.20) },
          onClick: this.handleClick }),
        this.state.displayColorPicker ? _react2.default.createElement(
          'div',
          { className: _style2.default['sketch-container'], ref: function ref(c) {
              return _this2._sketchContainer = c;
            } },
          _react2.default.createElement(_reactColor.SketchPicker, { color: this.state.color, onChange: this.handlePickerChange })
        ) : null
      );
    }
  }]);

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

ColorPicker.defaultProps = {
  value: ''
};
ColorPicker.propTypes = {
  /**
   * Hex color value.
   */
  value: _react2.default.PropTypes.string,
  /**
   * A callback function to be called when the color changes.
   */
  changeCallback: _react2.default.PropTypes.func,
  /**
   * An optional CSS class to be used for local styles
   */
  optClass: _react2.default.PropTypes.string
};
exports.default = (0, _reactClickOutside2.default)(ColorPicker);