import _useEffectState3 from "parsec-hooks/lib/effectStateHooks";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import React from 'react';
import { View } from 'remax/one';
import classNames from 'classnames';
import styles from './index.module.less';
import { useConfig } from '../config-provider';

var Radio = function Radio(props) {
  var _classNames;

  var _useConfig = useConfig(),
      brandPrimary = _useConfig.brandPrimary;

  var disabled = props.disabled,
      children = props.children,
      checked = props.checked,
      value = props.value,
      _props$activeBackgrou = props.activeBackgroundColor,
      activeBackgroundColor = _props$activeBackgrou === void 0 ? brandPrimary : _props$activeBackgrou,
      _props$backgroundColo = props.backgroundColor,
      backgroundColor = _props$backgroundColo === void 0 ? '#eeeeee' : _props$backgroundColo,
      _props$color = props.color,
      color = _props$color === void 0 ? '#000' : _props$color,
      _props$activeColor = props.activeColor,
      activeColor = _props$activeColor === void 0 ? '#fff' : _props$activeColor,
      style = props.style,
      onChange = props.onChange,
      className = props.className,
      activeCls = props.activeCls,
      _props$type = props.type,
      type = _props$type === void 0 ? 'normal' : _props$type;

  var _useEffectState = _useEffectState3(checked),
      _useEffectState2 = _slicedToArray(_useEffectState, 2),
      myChecked = _useEffectState2[0],
      setMyChecked = _useEffectState2[1];

  return /*#__PURE__*/React.createElement(View, {
    className: classNames(className, styles.container, (_classNames = {}, _defineProperty(_classNames, styles.radio, type === 'normal'), _defineProperty(_classNames, styles.btn, type === 'button'), _classNames), myChecked && classNames(activeCls, type === 'button' && styles.btnActive), disabled && styles.disabled),
    style: _objectSpread({
      background: type !== 'button' ? 'none' : myChecked ? activeBackgroundColor : backgroundColor,
      color: type !== 'button' ? 'none' : myChecked ? activeColor : color
    }, style),
    onTap: function onTap(e) {
      if (!disabled) {
        setMyChecked(!myChecked);
        onChange === null || onChange === void 0 ? void 0 : onChange(!myChecked, !myChecked ? value : undefined);
      }
    }
  }, type === 'normal' && /*#__PURE__*/React.createElement(View, {
    className: classNames(styles.dot, _defineProperty({}, styles.dotCheck, myChecked)),
    style: {
      borderColor: myChecked ? activeBackgroundColor : '#eee',
      borderWidth: myChecked ? undefined : 1,
      background: activeColor
    }
  }), children);
};

var getRadios = function getRadios(children, value, disabled, onChange) {
  var onGroupChange = function onGroupChange(checked, v) {
    if (disabled) {
      return;
    }

    onChange === null || onChange === void 0 ? void 0 : onChange(v);
  };

  return React.Children.map(children, function (radio) {
    var newRadio = radio;
    var checked = false;

    if (newRadio && /*#__PURE__*/React.isValidElement(newRadio) && newRadio.props) {
      checked = (newRadio.props.value || newRadio.props.value === 0 || newRadio.props.value === false) && newRadio.props.value === value;
      return /*#__PURE__*/React.createElement(Radio, _objectSpread(_objectSpread({}, newRadio.props), {}, {
        checked: checked,
        disabled: disabled,
        onChange: onGroupChange
      }));
    }

    return newRadio;
  });
};

Radio.Group = function (props) {
  var value = props.value,
      children = props.children,
      direction = props.direction,
      onChange = props.onChange,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      style = props.style,
      className = props.className;

  var _useEffectState4 = _useEffectState3(value),
      _useEffectState5 = _slicedToArray(_useEffectState4, 2),
      v = _useEffectState5[0],
      setV = _useEffectState5[1];

  var radios = getRadios(children, v, disabled, function () {
    for (var _len = arguments.length, arg = new Array(_len), _key = 0; _key < _len; _key++) {
      arg[_key] = arguments[_key];
    }

    setV(arg[0]);
    onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, arg);
  });
  return /*#__PURE__*/React.createElement(View, {
    style: _objectSpread({
      flexDirection: direction
    }, style),
    className: classNames(className, styles.group, disabled && styles.disabled)
  }, radios);
};

export default Radio;