"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.RadioButton = void 0;

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

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

var _uuid = require("uuid");

var _typography = require("../../../mixins/typography");

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

var _primitives = require("../../../primitives");

var _radioOption = require("../../icon/radio-option");

var _focus = require("../../../mixins/focus");

var _spacing = require("../../../mixins/spacing");

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

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

var RadioButtonContainer = (0, _styledComponents["default"])(_primitives.Box).withConfig({
  displayName: "RadioButtonContainer",
  componentId: "sc-1grcdji-0"
})(["cursor:pointer;position:relative;", " &:focus-within{box-shadow:", ",", " ", ";}border-radius:", ";transform:scale(1);"], (0, _focus.getBaseFocusState)({
  hoverTransition: false,
  activeTransition: false
}), _focus.focusedBoxShadow, _focus.focusedBorderShadow, _focus.focusedBorderColor, _focus.focusedBorderRadius);
var StyledRadioOption = (0, _styledComponents["default"])(_radioOption.RadioOption).withConfig({
  displayName: "StyledRadioOption",
  componentId: "sc-1grcdji-1"
})(["margin-left:-", ";"], function (_ref) {
  var theme = _ref.theme;
  return theme.spacing.s1;
});

var Label = _styledComponents["default"].label.withConfig({
  displayName: "Label",
  componentId: "sc-1grcdji-2"
})(["color:", ";cursor:pointer;font-size:", ";font-family:", ";display:flex;align-items:center;-webkit-touch-callout:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;margin-top:9px;margin-bottom:11px;z-index:-1;", ""], (0, _branding.getPaletteColor)('greyscale-black'), _typography.getFontSize, _typography.getFontFamily, _spacing.getSpacingRules);

var InvisibleInput = _styledComponents["default"].input.withConfig({
  displayName: "InvisibleInput",
  componentId: "sc-1grcdji-3"
})(["height:0;width:0;margin:0;padding:0;opacity:0;position:absolute;"]);

var RadioButton = function RadioButton(_ref2) {
  var id = _ref2.id,
      text = _ref2.text,
      selected = _ref2.selected,
      onClick = _ref2.onClick,
      name = _ref2.name,
      spacing = _ref2.spacing,
      grow = _ref2.grow,
      shrink = _ref2.shrink,
      basis = _ref2.basis,
      onFocus = _ref2.onFocus,
      onBlur = _ref2.onBlur,
      className = _ref2.className,
      dataQa = _ref2['data-qa'],
      dataCy = _ref2['data-cy'],
      _ref2$stacked = _ref2.stacked,
      stacked = _ref2$stacked === void 0 ? 'row' : _ref2$stacked,
      _ref2$align = _ref2.align,
      align = _ref2$align === void 0 ? 'center' : _ref2$align,
      justify = _ref2.justify,
      wrap = _ref2.wrap,
      children = _ref2.children,
      _ref2$direction = _ref2.direction,
      direction = _ref2$direction === void 0 ? 'left' : _ref2$direction,
      props = _objectWithoutProperties(_ref2, ["id", "text", "selected", "onClick", "name", "spacing", "grow", "shrink", "basis", "onFocus", "onBlur", "className", "data-qa", "data-cy", "stacked", "align", "justify", "wrap", "children", "direction"]);

  var onClickWithEvent = function onClickWithEvent(_e) {
    return onClick(id);
  };

  var randomIdentifier = (0, _react.useMemo)(function () {
    return (0, _uuid.v4)();
  }, []);

  var label = /*#__PURE__*/_react["default"].createElement(Label, {
    face: "sans",
    fontSize: "f1",
    htmlFor: randomIdentifier,
    spacing: direction === 'left' ? 'ml1' : 'mr1'
  }, text != null ? text : children);

  return /*#__PURE__*/_react["default"].createElement(RadioButtonContainer, _extends({
    onClick: onClickWithEvent,
    onFocus: onFocus,
    onBlur: onBlur,
    spacing: spacing
  }, {
    grow: grow,
    shrink: shrink,
    basis: basis
  }, {
    stacked: stacked,
    align: align,
    justify: justify,
    wrap: wrap
  }, {
    className: className,
    "data-cy": dataCy,
    "data-qa": dataQa,
    id: id
  }), /*#__PURE__*/_react["default"].createElement(InvisibleInput, _extends({
    readOnly: true,
    type: "radio",
    checked: selected,
    name: name,
    id: randomIdentifier
  }, props)), direction === 'left' ? /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(StyledRadioOption, {
    checked: selected
  }), label) : /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, label, /*#__PURE__*/_react["default"].createElement(StyledRadioOption, {
    checked: selected
  })));
};

exports.RadioButton = RadioButton;
RadioButton.displayName = 'RadioButton';