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 _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 { __rest } from "tslib";
import classNames from 'classnames';
import React, { createElement, forwardRef, useEffect, useState } from "react";
import { Text } from "@alifd/meet-react-component-one";
import View from '../view';
import { isFunction, isNumber, isString } from '../utils';

var getInitCheckedStatus = function getInitCheckedStatus(props) {
  if ('checked' in props) {
    return props.checked;
  } else if (props.defaultChecked) {
    return props.defaultChecked;
  }
};

var Radio = function Radio(props, ref) {
  var _props$size = props.size,
      size = _props$size === void 0 ? 'medium' : _props$size,
      _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      children = props.children,
      checked = props.checked,
      onChange = props.onChange,
      disabled = props.disabled,
      className = props.className,
      defaultChecked = props.defaultChecked,
      readOnly = props.readOnly,
      onClick = props.onClick,
      others = __rest(props, ["size", "prefix", "children", "checked", "onChange", "disabled", "className", "defaultChecked", "readOnly", "onClick"]);

  var _useState = useState(getInitCheckedStatus(props)),
      _useState2 = _slicedToArray(_useState, 2),
      selected = _useState2[0],
      setSelected = _useState2[1];

  var selectedPrefix = selected ? '--selected' : '--unselected';
  var disabledPrefix = disabled ? '-disabled' : '';
  var circleCls = classNames(["".concat(prefix, "radio-circle"), "".concat(prefix, "radio-circle--").concat(size), "".concat(prefix, "radio-circle").concat(selectedPrefix).concat(disabledPrefix)]);
  var pointCls = classNames(["".concat(prefix, "radio-point"), "".concat(prefix, "radio-point--").concat(size), "".concat(prefix, "radio-point").concat(selectedPrefix).concat(disabledPrefix)]);
  var textCls = classNames(["".concat(prefix, "radio-text"), "".concat(prefix, "radio-text--").concat(size), "".concat(prefix, "radio-text").concat(selectedPrefix).concat(disabledPrefix)]);
  useEffect(function () {
    if ('checked' in props) {
      setSelected(props.checked);
    }
  });

  var handleClick = function handleClick(e) {
    if (!disabled && !readOnly) {
      if (isFunction(onChange)) {
        onChange(!selected, e);
      }

      if (isFunction(onClick)) {
        onClick(e);
      }

      if (!('checked' in props)) {
        setSelected(!selected);
      }
    }
  };

  return /*#__PURE__*/React.createElement(View, _extends({}, others, {
    ref: ref,
    className: classNames("".concat(prefix, "radio-container"), "".concat(prefix, "radio-container--").concat(size), className),
    onClick: handleClick
  }), /*#__PURE__*/React.createElement(View, {
    className: circleCls
  }, /*#__PURE__*/React.createElement(View, {
    className: pointCls
  })), isString(children) || isNumber(children) ? /*#__PURE__*/React.createElement(Text, {
    className: textCls
  }, children) : /*#__PURE__*/React.createElement(View, {
    className: textCls
  }, children));
};

export default /*#__PURE__*/forwardRef(Radio);