/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _asyncToGenerator, r as regenerator } from '../_chunks/dep-beaa677e.mjs';
import { _ as _slicedToArray } from '../_chunks/dep-55846619.mjs';
import { _ as _defineProperty } from '../_chunks/dep-eb20e23f.mjs';
import { defineComponent, toRefs, ref, computed, watch, nextTick, onMounted, onUnmounted, provide, reactive, createVNode, h } from 'vue';
import { i as isString_1 } from '../_chunks/dep-1cf2b129.mjs';
import { i as isNumber_1 } from '../_chunks/dep-6eea369f.mjs';
import { i as isNil_1 } from '../_chunks/dep-51aacdc9.mjs';
import { i as isFunction_1 } from '../_chunks/dep-6b22dd27.mjs';
import { usePrefixClass, useCommonClassName } from '../hooks/useConfig.mjs';
import useVModel from '../hooks/useVModel.mjs';
import { useTNodeDefault } from '../hooks/tnode.mjs';
import { useMutationObserver } from '../watermark/hooks.mjs';
import props from './radio-group-props.mjs';
import _Radio from './radio.mjs';
import { RadioGroupInjectionKey } from './constants.mjs';
import useKeyboard from './useKeyboard.mjs';
import '../_chunks/dep-b4041200.mjs';
import '../_chunks/dep-a0b4a044.mjs';
import '../_chunks/dep-cfcedf47.mjs';
import '../_chunks/dep-c959a6fb.mjs';
import '../_chunks/dep-e3b14d70.mjs';
import '../_chunks/dep-66d6733b.mjs';
import '../_chunks/dep-c31e1d9e.mjs';
import '../_chunks/dep-0f0c89ff.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-44417261.mjs';
import '../_chunks/dep-bf74f1c2.mjs';
import '../_chunks/dep-79821ff2.mjs';
import '../_chunks/dep-0bfbf721.mjs';
import '../_chunks/dep-39cc276d.mjs';
import '../_chunks/dep-8f777748.mjs';
import '../_chunks/dep-ae357f26.mjs';
import '../_chunks/dep-352691c2.mjs';
import '../_chunks/dep-2cfd5b3a.mjs';
import '../_chunks/dep-05fa17c9.mjs';
import '../_chunks/dep-a9c8a5d5.mjs';
import '../_chunks/dep-10085c61.mjs';
import '../_chunks/dep-1b058de3.mjs';
import '../_chunks/dep-c852a61a.mjs';
import '../_chunks/dep-92e7085e.mjs';
import '../_chunks/dep-44f056cd.mjs';
import '../_chunks/dep-23a0f453.mjs';
import '../_chunks/dep-99920870.mjs';
import '../_chunks/dep-c7e9ce16.mjs';
import '../_chunks/dep-3afa94cf.mjs';
import '../_chunks/dep-ba3c0b2e.mjs';
import '../_chunks/dep-ee57d235.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-6b4aa943.mjs';
import '../_chunks/dep-d52291a9.mjs';
import '../config-provider/type.mjs';
import '../_chunks/dep-c4e1a112.mjs';
import '../_chunks/dep-74848b92.mjs';
import '../_chunks/dep-37ba476d.mjs';
import '../_chunks/dep-be27d184.mjs';
import '../_chunks/dep-f6649a46.mjs';
import '../_chunks/dep-1fe0ad42.mjs';
import '../_chunks/dep-d54f2690.mjs';
import '../_chunks/dep-59c8420d.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-bd03960e.mjs';
import '../_chunks/dep-b6a73f93.mjs';
import '../_chunks/dep-e091cfd9.mjs';
import '../_chunks/dep-30451c48.mjs';
import '../utils/helper.mjs';
import '../_chunks/dep-6f89b77e.mjs';
import '../form/hooks.mjs';
import './props.mjs';
import '../utils/dom.mjs';
import '../utils/easing.mjs';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _Group = defineComponent({
  name: "TRadioGroup",
  props: _objectSpread({}, props),
  setup: function setup(props2) {
    var _toRefs = toRefs(props2),
      value = _toRefs.value,
      modelValue = _toRefs.modelValue;
    var _useVModel = useVModel(value, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      innerValue = _useVModel2[0],
      setInnerValue = _useVModel2[1];
    var radioGroupRef = ref();
    var radioBtnName = usePrefixClass("radio-button");
    var _useCommonClassName = useCommonClassName(),
      STATUS = _useCommonClassName.STATUS,
      SIZE = _useCommonClassName.SIZE;
    useKeyboard(radioGroupRef, setInnerValue);
    var checkedClassName = computed(function () {
      return ".".concat(radioBtnName.value, ".").concat(STATUS.value.checked);
    });
    var barStyle = ref({
      width: "0px",
      left: "0px"
    });
    var calcDefaultBarStyle = function calcDefaultBarStyle() {
      var div = document.createElement("div");
      div.setAttribute("style", "position: absolute; visibility: hidden;");
      div.appendChild(radioGroupRef.value.cloneNode(true));
      document.body.appendChild(div);
      var defaultCheckedRadio = div.querySelector(checkedClassName.value);
      var offsetWidth = defaultCheckedRadio.offsetWidth,
        offsetLeft = defaultCheckedRadio.offsetLeft;
      barStyle.value = {
        width: "".concat(offsetWidth, "px"),
        left: "".concat(offsetLeft, "px")
      };
      document.body.removeChild(div);
    };
    var calcBarStyle = function calcBarStyle() {
      if (props2.variant === "outline") return;
      var checkedRadio = radioGroupRef.value.querySelector(checkedClassName.value);
      if (!checkedRadio) {
        barStyle.value = {
          width: "0px",
          left: "0px"
        };
        return;
      }
      var offsetWidth = checkedRadio.offsetWidth,
        offsetLeft = checkedRadio.offsetLeft;
      if (!offsetWidth) calcDefaultBarStyle();else barStyle.value = {
        width: "".concat(offsetWidth, "px"),
        left: "".concat(offsetLeft, "px")
      };
    };
    var observerReturn;
    watch(innerValue, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
      return regenerator.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return nextTick();
          case 2:
            calcBarStyle();
          case 3:
          case "end":
            return _context.stop();
        }
      }, _callee);
    })));
    onMounted(function () {
      calcBarStyle();
      var checkedRadioLabel = radioGroupRef.value.querySelector("".concat(checkedClassName.value, " .").concat(radioBtnName.value, "__label"));
      if (checkedRadioLabel) {
        observerReturn = useMutationObserver(checkedRadioLabel, function (mutations) {
          mutations.forEach(function (mutation) {
            if (mutation.type === "characterData") calcBarStyle();
          });
        }, {
          attributes: true,
          childList: true,
          characterData: true,
          subtree: true
        });
      }
    });
    onUnmounted(function () {
      var _observerReturn;
      (_observerReturn = observerReturn) === null || _observerReturn === void 0 ? void 0 : _observerReturn.stop();
    });
    var _toRefs2 = toRefs(props2),
      name = _toRefs2.name,
      disabled = _toRefs2.disabled;
    provide(RadioGroupInjectionKey, reactive({
      name: name,
      disabled: disabled,
      value: innerValue,
      allowUncheck: props2.allowUncheck,
      setValue: setInnerValue
    }));
    var radioGroupName = usePrefixClass("radio-group");
    var renderSlot = useTNodeDefault();
    var renderBlock = function renderBlock() {
      if (props2.variant.includes("filled") && !isNil_1(innerValue.value)) return createVNode("div", {
        "style": barStyle.value,
        "class": "".concat(radioGroupName.value, "__bg-block")
      }, null);
    };
    var renderOptions = function renderOptions() {
      var _props2$options;
      return (_props2$options = props2.options) === null || _props2$options === void 0 ? void 0 : _props2$options.map(function (option) {
        var opt = option;
        if (isNumber_1(option) || isString_1(option)) opt = {
          value: option,
          label: option.toString()
        };
        return createVNode(_Radio, {
          "key": "radio-group-options-".concat(opt.value, "-").concat(Math.random()),
          "name": props2.name,
          "checked": innerValue.value === opt.value,
          "disabled": "disabled" in opt ? opt.disabled : props2.disabled,
          "value": opt.value
        }, {
          "default": function _default() {
            return [isFunction_1(opt.label) ? opt.label(h) : opt.label];
          }
        });
      });
    };
    var groupClass = computed(function () {
      var _ref2;
      return ["".concat(radioGroupName.value), SIZE.value[props2.size], (_ref2 = {}, _defineProperty(_ref2, "".concat(radioGroupName.value, "__outline"), props2.variant === "outline"), _defineProperty(_ref2, "".concat(radioGroupName.value, "--filled"), props2.variant.includes("filled")), _defineProperty(_ref2, "".concat(radioGroupName.value, "--primary-filled"), props2.variant === "primary-filled"), _ref2)];
    });
    return function () {
      return createVNode("div", {
        "ref": radioGroupRef,
        "class": groupClass.value
      }, [renderSlot("default") || renderOptions(), renderBlock()]);
    };
  }
});

export { _Group as default };
//# sourceMappingURL=group.mjs.map
