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

import { h as helper } from '../_chunks/dep-b6ac6e96.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import isString from 'lodash/isString';
import isNumber from 'lodash/isNumber';
import { emitEvent } from '../utils/event.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../utils/mixins.js';
import { on, off } from '../utils/dom.js';
import _Radio from './radio.js';
import props from './radio-group-props.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import 'vue';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-2cb61c79.js';
import 'dayjs';
import 'raf';
import '../utils/easing.js';
import '../utils/render-tnode.js';
import '@vue/composition-api';
import 'lodash/kebabCase';
import './props.js';

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 classPrefixMixins = getClassPrefixMixins("radio-group");
var _RadioGroup = mixins(classPrefixMixins).extend({
  name: "TRadioGroup",
  props: _objectSpread({}, props),
  components: {
    Radio: _Radio
  },
  provide: function provide() {
    return {
      radioGroup: this
    };
  },
  data: function data() {
    return {
      barStyle: {
        width: "0px",
        left: "0px"
      },
      observer: null
    };
  },
  computed: {
    checkedClassName: function checkedClassName() {
      return ".".concat(this.classPrefix, "-radio-button.").concat(this.commonStatusClassName.checked);
    }
  },
  render: function render(h) {
    var _this = this,
      _ref;
    var $scopedSlots = this.$scopedSlots;
    var children = $scopedSlots["default"] && $scopedSlots["default"](null);
    if (this.options && this.options.length) {
      children = this.options.map(function (option, index) {
        var opt = option;
        if (isNumber(option) || isString(option)) opt = {
          value: option,
          label: option.toString()
        };
        return h(_Radio, helper([{}, {
          "props": option
        }, {
          "key": "radio-group-options-".concat(opt.value, "-").concat(index),
          "attrs": {
            "name": _this.name,
            "checked": _this.value === opt.value,
            "disabled": "disabled" in opt ? opt.disabled : _this.disabled,
            "value": opt.value
          }
        }]), [typeof opt.label === "function" ? opt.label(h) : opt.label]);
      });
    }
    var groupClass = ["".concat(this.componentName), this.commonSizeClassName[this.size], (_ref = {}, _defineProperty(_ref, "".concat(this.componentName, "__outline"), this.variant === "outline"), _defineProperty(_ref, "".concat(this.componentName, "--filled"), this.variant.includes("filled")), _defineProperty(_ref, "".concat(this.componentName, "--primary-filled"), this.variant === "primary-filled"), _ref)];
    if (this.variant.includes("filled")) children && children.push(h("div", {
      "style": this.barStyle,
      "class": "".concat(this.componentName, "__bg-block")
    }));
    return h("div", {
      "class": groupClass
    }, [children]);
  },
  watch: {
    value: function value() {
      var _this2 = this;
      this.$nextTick(function () {
        return _this2.calcBarStyle();
      });
    }
  },
  mounted: function mounted() {
    this.calcBarStyle();
    var observer = new MutationObserver(this.calcBarStyle);
    observer.observe(this.$el, {
      childList: true,
      attributes: true,
      subtree: true
    });
    this.observer = observer;
    this.addKeyboardListeners();
  },
  beforeDestroy: function beforeDestroy() {
    this.observer.disconnect();
    this.removeKeyboardListeners();
  },
  methods: {
    addKeyboardListeners: function addKeyboardListeners() {
      on(this.$el, "keydown", this.checkRadioInGroup);
    },
    removeKeyboardListeners: function removeKeyboardListeners() {
      off(this.$el, "keydown", this.checkRadioInGroup);
    },
    checkRadioInGroup: function checkRadioInGroup(e) {
      if (/enter/i.test(e.key) || /enter/i.test(e.code)) {
        var inputNode = e.target.querySelector("input");
        var data = inputNode.dataset;
        if (inputNode.checked && data.allowUncheck) {
          this.handleRadioChange(void 0, {
            e: e
          });
        } else {
          var value = !isNaN(Number(data.value)) ? Number(data.value) : data.value;
          value = typeof value === "string" && {
            "true": true,
            "false": false
          }[value] || value;
          value = typeof value === "string" && value[0] === "'" ? value.replace(/'/g, "") : value;
          this.handleRadioChange(value, {
            e: e
          });
        }
      }
    },
    handleRadioChange: function handleRadioChange(value, context) {
      emitEvent(this, "change", value, context);
    },
    calcDefaultBarStyle: function calcDefaultBarStyle() {
      var defaultNode = this.$el.cloneNode(true);
      var div = document.createElement("div");
      div.setAttribute("style", "position: absolute; visibility: hidden;");
      div.appendChild(defaultNode);
      document.body.appendChild(div);
      var defaultCheckedRadio = div.querySelector(this.checkedClassName);
      var offsetWidth = defaultCheckedRadio.offsetWidth,
        offsetLeft = defaultCheckedRadio.offsetLeft;
      this.barStyle = {
        width: "".concat(offsetWidth, "px"),
        left: "".concat(offsetLeft, "px")
      };
      document.body.removeChild(div);
    },
    calcBarStyle: function calcBarStyle() {
      if (this.variant === "outline") return;
      var checkedRadio = this.$el.querySelector(this.checkedClassName);
      if (!checkedRadio) return;
      var offsetWidth = checkedRadio.offsetWidth,
        offsetLeft = checkedRadio.offsetLeft;
      if (!offsetWidth) this.calcDefaultBarStyle();else this.barStyle = {
        width: "".concat(offsetWidth, "px"),
        left: "".concat(offsetLeft, "px")
      };
    }
  }
});

export { _RadioGroup as default };
//# sourceMappingURL=group.js.map
