"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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

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

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

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const {
  labelable
} = _cspaceInput.enhancers;
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 = _objectSpread({}, BaseCheckboxInput.propTypes, {
  intl: _reactIntl.intlShape,
  viewType: _propTypes.default.string,
  trueLabel: _propTypes.default.string,
  falseLabel: _propTypes.default.string,
  indeterminateLabel: _propTypes.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,
    viewType,
    trueLabel = intl.formatMessage(messages.true),
    falseLabel = intl.formatMessage(messages.false),
    indeterminateLabel = intl.formatMessage(messages.indeterminate)
  } = props,
        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 _react.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;
var _default = IntlAwareCheckboxInput;
exports.default = _default;