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

'use strict';

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

var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var isString = require('lodash/isString');
var isNumber = require('lodash/isNumber');
var isNil = require('lodash/isNil');
var isFunction = require('lodash/isFunction');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var hooks_tnode = require('../hooks/tnode.js');
var radio_radioGroupProps = require('./radio-group-props.js');
var radio_radio = require('./radio.js');
var radio_constants = require('./constants.js');
var radio_useKeyboard = require('./useKeyboard.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-f4db2590.js');
require('dayjs');
require('lodash/kebabCase');
require('lodash/camelCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/isUndefined');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isNull');
require('../form/hooks.js');
require('./props.js');
require('../utils/dom.js');
require('../utils/easing.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
var isNumber__default = /*#__PURE__*/_interopDefaultLegacy(isNumber);
var isNil__default = /*#__PURE__*/_interopDefaultLegacy(isNil);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

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__default["default"](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 = vue.defineComponent({
  name: "TRadioGroup",
  props: _objectSpread({}, radio_radioGroupProps["default"]),
  setup: function setup(props2) {
    var _toRefs = vue.toRefs(props2),
      value = _toRefs.value,
      modelValue = _toRefs.modelValue;
    var _useVModel = hooks_useVModel["default"](value, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      innerValue = _useVModel2[0],
      setInnerValue = _useVModel2[1];
    var radioGroupRef = vue.ref();
    var radioBtnName = hooks_useConfig.usePrefixClass("radio-button");
    var _useCommonClassName = hooks_useConfig.useCommonClassName(),
      STATUS = _useCommonClassName.STATUS,
      SIZE = _useCommonClassName.SIZE;
    radio_useKeyboard["default"](radioGroupRef, setInnerValue);
    var checkedClassName = vue.computed(function () {
      return ".".concat(radioBtnName.value, ".").concat(STATUS.value.checked);
    });
    var barStyle = vue.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")
      };
    };
    vue.watch(innerValue, /*#__PURE__*/_asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
      return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return vue.nextTick();
          case 2:
            calcBarStyle();
          case 3:
          case "end":
            return _context.stop();
        }
      }, _callee);
    })));
    vue.onMounted(function () {
      calcBarStyle();
    });
    var _toRefs2 = vue.toRefs(props2),
      name = _toRefs2.name,
      disabled = _toRefs2.disabled;
    vue.provide(radio_constants.RadioGroupInjectionKey, vue.reactive({
      name: name,
      disabled: disabled,
      value: innerValue,
      allowUncheck: props2.allowUncheck,
      setValue: setInnerValue
    }));
    var radioGroupName = hooks_useConfig.usePrefixClass("radio-group");
    var renderSlot = hooks_tnode.useTNodeDefault();
    var renderBlock = function renderBlock() {
      if (props2.variant.includes("filled") && !isNil__default["default"](innerValue.value)) return vue.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__default["default"](option) || isString__default["default"](option)) opt = {
          value: option,
          label: option.toString()
        };
        return vue.createVNode(radio_radio["default"], {
          "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__default["default"](opt.label) ? opt.label(vue.h) : opt.label];
          }
        });
      });
    };
    var groupClass = vue.computed(function () {
      var _ref2;
      return ["".concat(radioGroupName.value), SIZE.value[props2.size], (_ref2 = {}, _defineProperty__default["default"](_ref2, "".concat(radioGroupName.value, "__outline"), props2.variant === "outline"), _defineProperty__default["default"](_ref2, "".concat(radioGroupName.value, "--filled"), props2.variant.includes("filled")), _defineProperty__default["default"](_ref2, "".concat(radioGroupName.value, "--primary-filled"), props2.variant === "primary-filled"), _ref2)];
    });
    return function () {
      return vue.createVNode("div", {
        "ref": radioGroupRef,
        "class": groupClass.value
      }, [renderSlot("default") || renderOptions(), renderBlock()]);
    };
  }
});

exports["default"] = _Group;
//# sourceMappingURL=group.js.map
