import { cx } from '@linaria/core';
import { Typo } from '../Typo/index.js';
import { Radio as Radio$2, InputNumber, Input } from 'antd';
import _ from 'lodash';
import React__default, { useCallback, useState } from 'react';
import { EMPTY_FUNCTION } from '../../utils/constants.js';

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_4100_0_7_rapgekc";
const RadioGroupStyle = "E_4100_0_7_rp906go";
const RadioButtonStyle = "E_4100_0_7_rcc63c8";
const KitRadioGroupContext = React__default.createContext({});
const Radio = _a => {
  var _b = _a,
    {
      children,
      className,
      checked,
      compact = false,
      "data-testid": dataTestId
    } = _b,
    props = __objRest(_b, ["children", "className", "checked", "compact", "data-testid"]);
  const {
    description
  } = props;
  const context = React__default.useContext(KitRadioGroupContext);
  const markerRef = useCallback(node => {
    if (node && dataTestId) {
      const label = node.closest("label.ant-radio-wrapper");
      if (label) {
        label.setAttribute("data-testid", dataTestId);
      }
    }
  }, [dataTestId]);
  if (description) {
    children = /* @__PURE__ */React__default.createElement(React__default.Fragment, null, children, /* @__PURE__ */React__default.createElement("div", {
      className: cx("radio-description", Typo.Label.l4_regular)
    }, description));
  }
  return /* @__PURE__ */React__default.createElement(Radio$2, __spreadValues({
    className: cx(className, RadioStyle, compact && "compact"),
    checked: checked || false,
    "data-test": context.name ? `${context.name}-${String(props.value)}` : String(props.value)
  }, props), dataTestId && /* @__PURE__ */React__default.createElement("span", {
    ref: markerRef,
    style: {
      display: "none"
    }
  }), children ? children : null);
};
const RadioGroup = _c => {
  var _d = _c,
    {
      children,
      className,
      "data-testid": dataTestId
    } = _d,
    props = __objRest(_d, ["children", "className", "data-testid"]);
  return /* @__PURE__ */React__default.createElement(KitRadioGroupContext.Provider, {
    value: {
      disabled: props.disabled,
      name: props.name
    }
  }, /* @__PURE__ */React__default.createElement(Radio$2.Group, __spreadValues({
    "data-testid": dataTestId,
    className: cx(className, RadioGroupStyle)
  }, props), children ? children : null));
};
const RadioButton = _e => {
  var _f = _e,
    {
      children,
      className,
      "data-testid": dataTestId
    } = _f,
    props = __objRest(_f, ["children", "className", "data-testid"]);
  const {
    type,
    placeholder = "Label",
    onInputChange = EMPTY_FUNCTION,
    min,
    max,
    precision,
    initialValue
  } = props;
  const [inputValue, setInputValue] = useState(initialValue);
  const radioButtonValue = type === "input" || type === "input-number" ? inputValue : props.value;
  const radioGroupContext = React__default.useContext(KitRadioGroupContext);
  const markerRef = useCallback(node => {
    if (node && dataTestId) {
      const label = node.closest("label.ant-radio-button-wrapper");
      if (label) {
        label.setAttribute("data-testid", dataTestId);
      }
    }
  }, [dataTestId]);
  const renderInput = () => {
    const inputDisabled = props.disabled || (radioGroupContext == null ? void 0 : radioGroupContext.disabled);
    if (type === "input-number") return /* @__PURE__ */React__default.createElement(InputNumber, {
      className: "ant-radio-button-input",
      placeholder,
      disabled: inputDisabled,
      "data-testid": dataTestId ? `${dataTestId}-input-number` : void 0,
      value: !_.isNil(inputValue) ? Number(inputValue) : void 0,
      min,
      max,
      precision,
      onChange: value => {
        const val = value || 0;
        setInputValue(val);
        onInputChange(val);
      }
    });
    return /* @__PURE__ */React__default.createElement(Input, {
      className: "ant-radio-button-input",
      placeholder,
      disabled: inputDisabled,
      "data-testid": dataTestId ? `${dataTestId}-input` : void 0,
      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.createElement(React__default.Fragment, null, renderInput(), /* @__PURE__ */React__default.createElement("span", {
      className: "ant-radio-button-input-label"
    }, typeof children === "string" ? children : ""));
  };
  return /* @__PURE__ */React__default.createElement(Radio$2.Button, __spreadValues({
    className: cx(className, RadioButtonStyle),
    value: radioButtonValue
  }, props), dataTestId && /* @__PURE__ */React__default.createElement("span", {
    ref: markerRef,
    style: {
      display: "none"
    }
  }), renderChildren());
};
var Radio$1 = Radio;

export { RadioButton, RadioGroup, Radio$1 as default };
