import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
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) { _defineProperty(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; }
import "core-js/modules/es.array.map.js";
import "core-js/modules/es.symbol.js";
import "core-js/modules/es.array.filter.js";
import "core-js/modules/es.array.for-each.js";
import "core-js/modules/es.array.push.js";
import "core-js/modules/es.function.name.js";
import "core-js/modules/es.object.define-properties.js";
import "core-js/modules/es.object.define-property.js";
import "core-js/modules/es.object.get-own-property-descriptor.js";
import "core-js/modules/es.object.get-own-property-descriptors.js";
import "core-js/modules/es.object.keys.js";
import "core-js/modules/esnext.iterator.filter.js";
import "core-js/modules/esnext.iterator.for-each.js";
import "core-js/modules/web.dom-collections.for-each.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/esnext.iterator.constructor.js";
import "core-js/modules/esnext.iterator.map.js";
import { createContext, useContext, useEffect, useState } from 'react';
import './index.css';
import classnames from 'classnames';
import { v4 as uuid } from 'uuid';
import { FcrIcon } from '../icon';
import { FcrIconType } from '../icon/type';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export var FcrRadio = function FcrRadio(props) {
  var _groupContext$value;
  var groupContext = 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 = _objectWithoutProperties(props, _excluded);
  return /*#__PURE__*/_jsxs("label", {
    className: classnames('fcr-radio', "fcr-radio-".concat(styleType)),
    children: [/*#__PURE__*/_jsxs("span", {
      className: "fcr-radio-input-wrapper",
      onClick: onClick,
      children: [/*#__PURE__*/_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__*/_jsx("span", {
        className: classnames('fcr-radio-inner', {
          'fcr-btn-click-effect': styleType !== 'transparent'
        }),
        children: /*#__PURE__*/_jsx(FcrIcon, {
          type: FcrIconType.FCR_CHECKBOX_CHECK,
          colors: {
            iconPrimary: iconColor
          },
          size: 8
        })
      })]
    }), /*#__PURE__*/_jsx("span", {
      className: "fcr-radio-label",
      onClick: onClick,
      children: label
    })]
  });
};
var RadioGroupContext = /*#__PURE__*/createContext(null);
export var FcrRadioGroup = function FcrRadioGroup(props) {
  var children = props.children,
    options = props.options,
    defaultValue = props.defaultValue;
  var _useState = useState(defaultValue || props.value),
    _useState2 = _slicedToArray(_useState, 2),
    value = _useState2[0],
    setValue = _useState2[1];
  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__*/_jsx(RadioGroupContext.Provider, {
    value: _objectSpread(_objectSpread({}, props), {}, {
      value: value,
      onChange: onRadioChange
    }),
    children: options ? options.map(function (props) {
      return /*#__PURE__*/_jsx(FcrRadio, _objectSpread({}, props), props.value || uuid());
    }) : children
  });
};