"use strict";

require("core-js/modules/es.symbol.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.array.for-each.js");
require("core-js/modules/es.array.push.js");
require("core-js/modules/es.object.define-properties.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.object.get-own-property-descriptors.js");
require("core-js/modules/es.object.keys.js");
require("core-js/modules/esnext.iterator.filter.js");
require("core-js/modules/esnext.iterator.for-each.js");
require("core-js/modules/web.dom-collections.for-each.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FcrRadioGroup = exports.FcrRadio = void 0;
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.function.name.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.map.js");
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = require("react");
require("./index.css");
var _classnames = _interopRequireDefault(require("classnames"));
var _uuid = require("uuid");
var _icon = require("../icon");
var _type = require("../icon/type");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["label", "onChange", "styleType", "name", "value", "checked", "onClick", "iconColor"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var FcrRadio = exports.FcrRadio = function FcrRadio(props) {
  var _groupContext$value;
  var groupContext = (0, _react.useContext)(RadioGroupContext);
  var label = props.label,
    _onChange = props.onChange,
    _props$styleType = props.styleType,
    styleType = _props$styleType === void 0 ? 'brand' : _props$styleType,
    name = props.name,
    value = props.value,
    checked = props.checked,
    onClick = props.onClick,
    _props$iconColor = props.iconColor,
    iconColor = _props$iconColor === void 0 ? styleType !== 'white' ? 'var(--fcr_ui_scene_white10)' : 'var(--fcr_ui_scene_ramp_brand6)' : _props$iconColor,
    inputProps = (0, _objectWithoutProperties2["default"])(props, _excluded);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("label", {
    className: (0, _classnames["default"])('fcr-radio', "fcr-radio-".concat(styleType)),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
      className: "fcr-radio-input-wrapper",
      onClick: onClick,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("input", _objectSpread(_objectSpread({
        name: name
      }, inputProps), {}, {
        onClick: function onClick(e) {
          e.stopPropagation();
        },
        checked: ((_groupContext$value = groupContext === null || groupContext === void 0 ? void 0 : groupContext.value) !== null && _groupContext$value !== void 0 ? _groupContext$value : null) === value || checked || false,
        onChange: function onChange(e) {
          var _groupContext$onChang;
          groupContext === null || groupContext === void 0 || (_groupContext$onChang = groupContext.onChange) === null || _groupContext$onChang === void 0 || _groupContext$onChang.call(groupContext, value);
          _onChange === null || _onChange === void 0 || _onChange(e.target.checked);
        },
        type: "radio"
      })), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: (0, _classnames["default"])('fcr-radio-inner', {
          'fcr-btn-click-effect': styleType !== 'transparent'
        }),
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
          type: _type.FcrIconType.FCR_CHECKBOX_CHECK,
          colors: {
            iconPrimary: iconColor
          },
          size: 8
        })
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "fcr-radio-label",
      onClick: onClick,
      children: label
    })]
  });
};
var RadioGroupContext = /*#__PURE__*/(0, _react.createContext)(null);
var FcrRadioGroup = exports.FcrRadioGroup = function FcrRadioGroup(props) {
  var children = props.children,
    options = props.options,
    defaultValue = props.defaultValue;
  var _useState = (0, _react.useState)(defaultValue || props.value),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    value = _useState2[0],
    setValue = _useState2[1];
  (0, _react.useEffect)(function () {
    props.value && setValue(props.value);
  }, [props.value]);
  var onRadioChange = function onRadioChange(value) {
    var _props$onChange;
    if (!props.value) {
      value && setValue(value);
    }
    (_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, value);
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(RadioGroupContext.Provider, {
    value: _objectSpread(_objectSpread({}, props), {}, {
      value: value,
      onChange: onRadioChange
    }),
    children: options ? options.map(function (props) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(FcrRadio, _objectSpread({}, props), props.value || (0, _uuid.v4)());
    }) : children
  });
};