'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var core = require('@linaria/core');
var index = require('../Typo/index.js');
var antd = require('antd');
var _ = require('lodash');
var React = require('react');
var constants = require('../../utils/constants.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var ___default = /*#__PURE__*/_interopDefault(_);
var React__default = /*#__PURE__*/_interopDefault(React);

var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {
  enumerable: true,
  configurable: true,
  writable: true,
  value
}) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) {
    if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]);
  }
  return a;
};
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source) if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols) for (var prop of __getOwnPropSymbols(source)) {
    if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) target[prop] = source[prop];
  }
  return target;
};
const RadioStyle = "E_rapgekc";
const RadioGroupStyle = "E_rp906go";
const RadioButtonStyle = "E_rcc63c8";
const KitRadioGroupContext = React__default.default.createContext({});
const Radio = _a => {
  var _b = _a,
    {
      children,
      className,
      checked,
      compact = false
    } = _b,
    props = __objRest(_b, ["children", "className", "checked", "compact"]);
  const {
    description
  } = props;
  const context = React__default.default.useContext(KitRadioGroupContext);
  if (description) {
    children = /* @__PURE__ */React__default.default.createElement(React__default.default.Fragment, null, children, /* @__PURE__ */React__default.default.createElement("div", {
      className: core.cx("radio-description", index.Typo.Label.l4_regular)
    }, description));
  }
  return /* @__PURE__ */React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */React__default.default.createElement(antd.Radio, __spreadValues({
    className: core.cx(className, RadioStyle, compact && "compact"),
    checked: checked || false,
    "data-test": context.name ? `${context.name}-${String(props.value)}` : String(props.value)
  }, props), children ? children : null));
};
const RadioGroup = _c => {
  var _d = _c,
    {
      children,
      className
    } = _d,
    props = __objRest(_d, ["children", "className"]);
  return /* @__PURE__ */React__default.default.createElement(KitRadioGroupContext.Provider, {
    value: {
      disabled: props.disabled,
      name: props.name
    }
  }, /* @__PURE__ */React__default.default.createElement(antd.Radio.Group, __spreadValues({
    className: core.cx(className, RadioGroupStyle)
  }, props), children ? children : null));
};
const RadioButton = _e => {
  var _f = _e,
    {
      children,
      className
    } = _f,
    props = __objRest(_f, ["children", "className"]);
  const {
    type,
    placeholder = "Label",
    onInputChange = constants.EMPTY_FUNCTION,
    min,
    max,
    precision,
    initialValue
  } = props;
  const [inputValue, setInputValue] = React.useState(initialValue);
  const radioButtonValue = type === "input" || type === "input-number" ? inputValue : props.value;
  const radioGroupContext = React__default.default.useContext(KitRadioGroupContext);
  const renderInput = () => {
    const inputDisabled = props.disabled || (radioGroupContext == null ? void 0 : radioGroupContext.disabled);
    if (type === "input-number") return /* @__PURE__ */React__default.default.createElement(antd.InputNumber, {
      className: "ant-radio-button-input",
      placeholder,
      disabled: inputDisabled,
      value: !___default.default.isNil(inputValue) ? Number(inputValue) : void 0,
      min,
      max,
      precision,
      onChange: value => {
        const val = value || 0;
        setInputValue(val);
        onInputChange(val);
      }
    });
    return /* @__PURE__ */React__default.default.createElement(antd.Input, {
      className: "ant-radio-button-input",
      placeholder,
      disabled: inputDisabled,
      value: inputValue,
      onChange: e => {
        setInputValue(e.target.value);
        onInputChange(e.target.value);
      }
    });
  };
  const renderChildren = () => {
    if (type !== "input" && type !== "input-number") return children ? children : null;
    return /* @__PURE__ */React__default.default.createElement(React__default.default.Fragment, null, renderInput(), /* @__PURE__ */React__default.default.createElement("span", {
      className: "ant-radio-button-input-label"
    }, typeof children === "string" ? children : ""));
  };
  return /* @__PURE__ */React__default.default.createElement(antd.Radio.Button, __spreadValues({
    className: core.cx(className, RadioButtonStyle),
    value: radioButtonValue
  }, props), renderChildren());
};
var Radio$1 = Radio;

exports.RadioButton = RadioButton;
exports.RadioGroup = RadioGroup;
exports.default = Radio$1;
