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

var _react2 = _interopRequireDefault(_react);

var _cspaceLayout = require('cspace-layout');

var _DropdownInput = require('./DropdownInput');

var _DropdownInput2 = _interopRequireDefault(_DropdownInput);

var _Menu = require('./Menu');

var _Menu2 = _interopRequireDefault(_Menu);

var _changeable = require('../enhancers/changeable');

var _changeable2 = _interopRequireDefault(_changeable);

var _committable = require('../enhancers/committable');

var _committable2 = _interopRequireDefault(_committable);

var _pathHelpers = require('../helpers/pathHelpers');

var _IDGeneratorInput = require('../../styles/cspace-input/IDGeneratorInput.css');

var _IDGeneratorInput2 = _interopRequireDefault(_IDGeneratorInput);

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 DropdownInput = (0, _committable2.default)((0, _changeable2.default)(_DropdownInput2.default));

var propTypes = _extends({}, DropdownInput.propTypes, {
  patterns: _react.PropTypes.array,
  generateID: _react.PropTypes.func,
  sampleColumnLabel: _react.PropTypes.string,
  typeColumnLabel: _react.PropTypes.string,
  onMount: _react.PropTypes.func,
  onOpen: _react.PropTypes.func
});

var defaultProps = {
  patterns: [],
  sampleColumnLabel: 'Sample',
  typeColumnLabel: 'Type'
};

var renderItemLabel = function renderItemLabel(label) {
  return _react2.default.createElement(
    _cspaceLayout.Row,
    null,
    _react2.default.createElement(
      'div',
      null,
      label[0]
    ),
    _react2.default.createElement(
      'div',
      null,
      label[1]
    )
  );
};

var IDGeneratorInput = function (_Component) {
  _inherits(IDGeneratorInput, _Component);

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

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

    _this.focusMenu = _this.focusMenu.bind(_this);
    _this.handleDropdownInputClose = _this.handleDropdownInputClose.bind(_this);
    _this.handleDropdownInputMount = _this.handleDropdownInputMount.bind(_this);
    _this.handleDropdownInputOpen = _this.handleDropdownInputOpen.bind(_this);
    _this.handleMenuRef = _this.handleMenuRef.bind(_this);
    _this.handleMenuSelect = _this.handleMenuSelect.bind(_this);

    _this.state = {
      open: false
    };
    return _this;
  }

  _createClass(IDGeneratorInput, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var onMount = this.props.onMount;


      if (onMount) {
        onMount();
      }
    }
  }, {
    key: 'focusMenu',
    value: function focusMenu() {
      if (this.menu) {
        this.menu.focus();
      }
    }
  }, {
    key: 'handleDropdownInputMount',
    value: function handleDropdownInputMount(_ref) {
      var focusInput = _ref.focusInput;

      this.focusInput = focusInput;
    }
  }, {
    key: 'handleDropdownInputClose',
    value: function handleDropdownInputClose() {
      this.setState({
        open: false
      });
    }
  }, {
    key: 'handleDropdownInputOpen',
    value: function handleDropdownInputOpen() {
      var _props = this.props,
          onOpen = _props.onOpen,
          patterns = _props.patterns;


      if (onOpen) {
        onOpen(patterns);
      }

      this.setState({
        open: true
      });
    }
  }, {
    key: 'handleMenuRef',
    value: function handleMenuRef(ref) {
      this.menu = ref;
    }
  }, {
    key: 'handleMenuSelect',
    value: function handleMenuSelect(option) {
      this.setState({
        open: false
      });

      this.focusInput();

      var generateID = this.props.generateID;


      if (generateID) {
        generateID(option.value, (0, _pathHelpers.getPath)(this.props));
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var open = this.state.open;

      var _props2 = this.props,
          patterns = _props2.patterns,
          sampleColumnLabel = _props2.sampleColumnLabel,
          typeColumnLabel = _props2.typeColumnLabel,
          generateID = _props2.generateID,
          onMount = _props2.onMount,
          onOpen = _props2.onOpen,
          remainingProps = _objectWithoutProperties(_props2, ['patterns', 'sampleColumnLabel', 'typeColumnLabel', 'generateID', 'onMount', 'onOpen']);

      var options = patterns.map(function (pattern) {
        return {
          value: pattern.name,
          label: [pattern.type, pattern.sample]
        };
      });

      return _react2.default.createElement(
        DropdownInput,
        _extends({}, remainingProps, {
          className: _IDGeneratorInput2.default.normal,
          focusPopup: this.focusMenu,
          open: open,
          onClose: this.handleDropdownInputClose,
          onMount: this.handleDropdownInputMount,
          onOpen: this.handleDropdownInputOpen
        }),
        _react2.default.createElement(
          'header',
          null,
          _react2.default.createElement(
            _cspaceLayout.Row,
            null,
            _react2.default.createElement(
              'div',
              null,
              typeColumnLabel
            ),
            _react2.default.createElement(
              'div',
              null,
              sampleColumnLabel
            )
          )
        ),
        _react2.default.createElement(_Menu2.default, {
          options: options,
          ref: this.handleMenuRef,
          renderItemLabel: renderItemLabel,
          tabIndex: '-1',
          onSelect: this.handleMenuSelect
        })
      );
    }
  }]);

  return IDGeneratorInput;
}(_react.Component);

exports.default = IDGeneratorInput;


IDGeneratorInput.propTypes = propTypes;
IDGeneratorInput.defaultProps = defaultProps;