"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

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

var _recompose = require("recompose");

var _styledComponents = _interopRequireWildcard(require("styled-components"));

var _lodash = require("lodash");

var _branding = require("../../../mixins/branding");

var _fieldLabel = require("../../typography/field-label");

var _selectorContext = require("./selector-context.proptypes");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

/**
 * TODO: Add support from RadioOptions as `large` configuration
 * TODO: Add support for free-form, like RadioOptions has (required for gender select)
 * TODO: Delete the RadioOptions in the Graveyard
 * TODO: Rename component to RadioOptions after ☝️
 * TODO: Remove label that's inside of this component OR accessibly-hide teh label
 */
var SelectorContainer = _styledComponents["default"].div.withConfig({
  displayName: "SelectorContainer",
  componentId: "ju7olk-0"
})(["&&{border:1px solid ", ";border-radius:6px;}& > *:first-child{border-radius:6px 6px 0 0;}& > *:last-child{border-bottom:0;border-radius:0 0 6px 6px;}& > *:only-child{border-radius:6px;}&& > *{border-bottom:1px solid ", ";}"], (0, _branding.getPaletteColor)('greyscale-light'), (0, _branding.getPaletteColor)('greyscale-light'));

var withManagedValueState = (0, _recompose.withState)('managedValue', 'setManagedValue', function (_ref) {
  var value = _ref.value;
  return value || undefined;
});
var withChangeHandler = (0, _recompose.withHandlers)(function () {
  return {
    onChangeHandler: function onChangeHandler(_ref2) {
      var setManagedValue = _ref2.setManagedValue,
          onChange = _ref2.onChange,
          managedValue = _ref2.managedValue,
          type = _ref2.type,
          stateful = _ref2.stateful;
      return function (event) {
        if (!stateful) {
          onChange(event.target.value);
          return;
        }

        var newValue = event.target.value;

        if (type === 'checkbox') {
          var shouldRemove = Array.isArray(managedValue) && managedValue.includes(newValue);

          if (shouldRemove) {
            // Removing
            newValue = (0, _lodash.remove)(managedValue, function (v) {
              return v !== newValue;
            });
          } else {
            // Adding
            newValue = (0, _lodash.concat)(managedValue || [], newValue);
          }
        }

        setManagedValue(newValue);
        onChange(newValue);
      };
    }
  };
});

var SizeContext = _react["default"].createContext('md');

exports.SizeContext = SizeContext;
var withSelectorContext = (0, _recompose.withContext)(_selectorContext.selectorContextTypes, function (props) {
  return {
    stateful: props.stateful,
    name: props.name,
    onChange: props.onChangeHandler,
    managedValue: props.managedValue,
    type: props.type !== undefined ? props.type : 'radio'
  };
});

var _Selector = function _Selector(_ref3) {
  var label = _ref3.label,
      children = _ref3.children,
      size = _ref3.size,
      _ref3$labelSpacing = _ref3.labelSpacing,
      labelSpacing = _ref3$labelSpacing === void 0 ? ['pt3', 'pb1'] : _ref3$labelSpacing;
  return _react["default"].createElement(SizeContext.Provider, {
    value: size
  }, label != null && _react["default"].createElement(_fieldLabel.FieldLabel, {
    spacing: labelSpacing
  }, label), _react["default"].createElement(SelectorContainer, null, children));
};

var Selector = (0, _recompose.compose)(withManagedValueState, withChangeHandler, withSelectorContext, _styledComponents.withTheme)(_Selector);
exports.Selector = Selector;
Selector.displayName = 'Selector';