"use strict";

function _typeof(obj) { 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.default = void 0;

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

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

var _uniqueId = _interopRequireDefault(require("../../tools/uniqueId"));

var _classnames = _interopRequireDefault(require("classnames"));

var _carbonComponents = require("carbon-components");

var _iconsReact = require("@carbon/icons-react");

var _keyboard = require("../../internal/keyboard");

var _deprecate = _interopRequireDefault(require("../../prop-types/deprecate"));

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

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

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 prefix = _carbonComponents.settings.prefix;

function RadioTile(_ref) {
  var _classNames;

  var children = _ref.children,
      className = _ref.className,
      iconDescription = _ref.iconDescription,
      light = _ref.light,
      checked = _ref.checked,
      name = _ref.name,
      value = _ref.value,
      id = _ref.id,
      onChange = _ref.onChange,
      tabIndex = _ref.tabIndex,
      other = _objectWithoutProperties(_ref, ["children", "className", "iconDescription", "light", "checked", "name", "value", "id", "onChange", "tabIndex"]);

  var _useRef = (0, _react.useRef)(id || (0, _uniqueId.default)()),
      inputId = _useRef.current;

  var classes = (0, _classnames.default)(className, "".concat(prefix, "--tile"), "".concat(prefix, "--tile--selectable"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "--tile--is-selected"), checked), _defineProperty(_classNames, "".concat(prefix, "--tile--light"), light), _classNames));

  function handleOnChange(evt) {
    onChange(value, name, evt);
  }

  function handleOnKeyDown(evt) {
    if ((0, _keyboard.matches)(evt, [_keyboard.keys.Enter, _keyboard.keys.Space])) {
      evt.preventDefault();
      onChange(value, name, evt);
    }
  }

  return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement("input", _extends({}, other, {
    type: "radio",
    checked: checked,
    name: name,
    value: value,
    className: "".concat(prefix, "--tile-input"),
    onChange: handleOnChange,
    id: inputId
  })), _react.default.createElement("label", {
    htmlFor: inputId,
    className: classes,
    tabIndex: tabIndex,
    onKeyDown: handleOnKeyDown
  }, _react.default.createElement("span", {
    className: "".concat(prefix, "--tile__checkmark")
  }, _react.default.createElement(_iconsReact.CheckmarkFilled16, null)), _react.default.createElement("span", {
    className: "".concat(prefix, "--tile-content")
  }, children)));
}

RadioTile.propTypes = {
  /**
   * `true` if this tile should be selected.
   */
  checked: _propTypes.default.bool,

  /**
   * The CSS class names.
   */
  className: _propTypes.default.string,

  /**
   * `true` if the `<input>` should be checked at initialization.
   */
  defaultChecked: _propTypes.default.bool,

  /**
   * The ID of the `<input>`.
   */
  id: _propTypes.default.string,

  /**
   * The `name` of the `<input>`.
   */
  name: _propTypes.default.string,

  /**
   * The description of the tile checkmark icon.
   */
  iconDescription: (0, _deprecate.default)(_propTypes.default.string, 'The `iconDescription` prop for `RadioTile` is no longer needed and has ' + 'been deprecated. It will be moved in the next major release.'),

  /**
   * The handler of the massaged `change` event on the `<input>`.
   */
  onChange: _propTypes.default.func,

  /**
   * The `value` of the `<input>`.
   */
  value: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]).isRequired,

  /**
   * Specify the tab index of the wrapper element
   */
  tabIndex: _propTypes.default.number,

  /**
   * `true` to use the light version.
   */
  light: _propTypes.default.bool
};
RadioTile.defaultProps = {
  onChange: function onChange() {},
  tabIndex: 0,
  light: false
};
var _default = RadioTile;
exports.default = _default;