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

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactIntl = require('react-intl');

var _cspaceInput = require('cspace-input');

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

const labelable = _cspaceInput.enhancers.labelable;


const BaseCheckboxInput = labelable(_cspaceInput.baseComponents.CheckboxInput);

const messages = (0, _reactIntl.defineMessages)({
  true: {
    'id': 'checkboxInput.true',
    'defaultMessage': 'yes'
  },
  false: {
    'id': 'checkboxInput.false',
    'defaultMessage': 'no'
  },
  indeterminate: {
    'id': 'checkboxInput.indeterminate',
    'defaultMessage': 'indeterminate'
  }
});

const propTypes = Object.assign({}, BaseCheckboxInput.propTypes, {
  intl: _reactIntl.intlShape,
  viewType: _propTypes2.default.string,
  trueLabel: _propTypes2.default.string,
  falseLabel: _propTypes2.default.string,
  indeterminateLabel: _propTypes2.default.string
});

/*
 * A wrapper around a cspace-input CheckboxInput that supplies i18n and alternate transitions
 * when displayed in a search form.
 */
function CheckboxInput(props) {
  const intl = props.intl,
        viewType = props.viewType;
  var _props$trueLabel = props.trueLabel;
  const trueLabel = _props$trueLabel === undefined ? intl.formatMessage(messages.true) : _props$trueLabel;
  var _props$falseLabel = props.falseLabel;
  const falseLabel = _props$falseLabel === undefined ? intl.formatMessage(messages.false) : _props$falseLabel;
  var _props$indeterminateL = props.indeterminateLabel;

  const indeterminateLabel = _props$indeterminateL === undefined ? intl.formatMessage(messages.indeterminate) : _props$indeterminateL,
        remainingProps = _objectWithoutProperties(props, ['intl', 'viewType', 'trueLabel', 'falseLabel', 'indeterminateLabel']);

  let transition;

  if (viewType === 'search') {
    // When searching, the indeterminate value should be togglable.

    transition = {
      null: true,
      true: false,
      false: null
    };
  }

  return _react2.default.createElement(BaseCheckboxInput, _extends({
    trueLabel: trueLabel,
    falseLabel: falseLabel,
    indeterminateLabel: indeterminateLabel,
    transition: transition
  }, remainingProps));
}

CheckboxInput.propTypes = propTypes;

const IntlAwareCheckboxInput = (0, _reactIntl.injectIntl)(CheckboxInput);

IntlAwareCheckboxInput.propTypes = CheckboxInput.propTypes;

exports.default = IntlAwareCheckboxInput;