/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

'use strict';

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

var helper = require('../../chunks/helper.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var isString = require('lodash/isString');
var isNumber = require('lodash/isNumber');
var components_radio_props = require('./props.js');
var components_radio_radio = require('./radio.js');
var utils_event = require('../../utils/event.js');
var components_configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../../utils/mixins.js');
require('vue');
require('../../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('../../utils/render-node.js');
require('@babel/runtime/helpers/typeof');
require('lodash/kebabCase');
require('lodash/mergeWith');
require('lodash');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
var isNumber__default = /*#__PURE__*/_interopDefaultLegacy(isNumber);

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 classPrefixMixins = components_configProvider_configReceiver.getClassPrefixMixins("radio-group");
var _RadioGroup = utils_mixins["default"](classPrefixMixins).extend({
  name: "LRadioGroup",
  props: _objectSpread({}, components_radio_props.RadioGroupProps),
  components: {
    LRadio: components_radio_radio["default"]
  },
  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__default["default"](option) || isString__default["default"](option)) {
          opt = {
            value: option,
            label: option.toString()
          };
        }

        return h("l-radio", helper.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__default["default"](_ref, "".concat(this.componentName, "__outline"), this.variant === "outline"), _defineProperty__default["default"](_ref, "".concat(this.componentName, "--filled"), this.variant.includes("filled")), _defineProperty__default["default"](_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();
      });
    }
  },
  created: function created() {
    this.$on("checked-change", this.handleRadioChange);
  },
  mounted: function mounted() {
    this.calcBarStyle();
    var observer = new MutationObserver(this.calcBarStyle);
    observer.observe(this.$el, {
      childList: true,
      attributes: true,
      subtree: true
    });
    this.observer = observer;
  },
  beforeDestroy: function beforeDestroy() {
    this.observer.disconnect();
  },
  methods: {
    handleRadioChange: function handleRadioChange(value, context) {
      utils_event.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")
        };
      }
    }
  }
});

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