"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const comp = require("../../ui/src/comp.js");
const XEUtils = require("xe-utils");
require("../../ui/index.js");
const radio = require("./radio.js");
const button = require("./button.js");
const config = require("../../../../@vxe-ui/core/es/src/config.js");
const useFns = require("../../../../@vxe-ui/core/es/src/useFns.js");
const event = require("../../../../@vxe-ui/core/es/src/event.js");
function createInternalData() {
  return {
    // isLoaded: false
  };
}
const VxeRadioGroupComponent = comp.defineVxeComponent({
  name: "VxeRadioGroup",
  props: {
    modelValue: [String, Number, Boolean],
    disabled: {
      type: Boolean,
      default: null
    },
    type: String,
    options: Array,
    optionProps: Object,
    strict: {
      type: Boolean,
      default: () => config.getConfig().radioGroup.strict
    },
    size: {
      type: String,
      default: () => config.getConfig().radioGroup.size || config.getConfig().size
    },
    defaultConfig: Object
  },
  emits: [
    "update:modelValue",
    "change",
    "default-change"
  ],
  setup(props, context) {
    const { slots, emit } = context;
    const $xeForm = vue.inject("$xeForm", null);
    const formItemInfo = vue.inject("xeFormItemInfo", null);
    const xID = XEUtils.uniqueId();
    const { computeSize } = useFns.useSize(props);
    const reactData = vue.reactive({});
    const internalData = createInternalData();
    const computeIsDisabled = vue.computed(() => {
      const { disabled } = props;
      if (disabled === null) {
        if ($xeForm) {
          return $xeForm.props.readonly || $xeForm.props.disabled;
        }
        return false;
      }
      return disabled;
    });
    const computeDefaultOpts = vue.computed(() => {
      return Object.assign({}, props.defaultConfig);
    });
    const computeMaps = {
      computeIsDisabled
    };
    const $xeRadioGroup = {
      xID,
      props,
      context,
      reactData,
      name: XEUtils.uniqueId("xe_group_"),
      getComputeMaps: () => computeMaps
    };
    const computePropsOpts = vue.computed(() => {
      return Object.assign({}, props.optionProps);
    });
    const computeLabelField = vue.computed(() => {
      const propsOpts = computePropsOpts.value;
      return propsOpts.label || "label";
    });
    const computeValueField = vue.computed(() => {
      const propsOpts = computePropsOpts.value;
      return propsOpts.value || "value";
    });
    const computeDisabledField = vue.computed(() => {
      const propsOpts = computePropsOpts.value;
      return propsOpts.disabled || "disabled";
    });
    const emitModel = (value) => {
      emit("update:modelValue", value);
    };
    const emitDefaultValue = (value) => {
      emitModel(value);
      dispatchEvent("default-change", { value }, null);
    };
    const dispatchEvent = (type, params, evnt) => {
      emit(type, event.createEvent(evnt, { $radioGroup: $xeRadioGroup }, params));
    };
    const radioGroupMethods = {
      dispatchEvent
    };
    const radioGroupPrivateMethods = {
      handleChecked(params, evnt) {
        const { checkedValue, checkedLabel } = params;
        const value = checkedValue;
        emitModel(value);
        dispatchEvent("change", { value, label: value, checkedValue, checkedLabel }, evnt);
        if ($xeForm && formItemInfo) {
          $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, value);
        }
      }
    };
    const loadData = (datas) => {
      const { isLoaded } = internalData;
      const defaultOpts = computeDefaultOpts.value;
      const valueField = computeValueField.value;
      if (!isLoaded) {
        const { selectMode } = defaultOpts;
        if (datas.length > 0 && XEUtils.eqNull(props.modelValue)) {
          if (selectMode === "first" || selectMode === "last") {
            const selectItem = XEUtils[selectMode](datas);
            if (selectItem) {
              vue.nextTick(() => {
                if (XEUtils.eqNull(props.modelValue)) {
                  emitDefaultValue(selectItem[valueField]);
                }
              });
            }
          }
          internalData.isLoaded = true;
        }
      }
      return vue.nextTick();
    };
    Object.assign($xeRadioGroup, radioGroupMethods, radioGroupPrivateMethods);
    const renderVN = () => {
      const { options, type } = props;
      const vSize = computeSize.value;
      const defaultSlot = slots.default;
      const valueField = computeValueField.value;
      const labelField = computeLabelField.value;
      const disabledField = computeDisabledField.value;
      const btnComp = type === "button" ? button.default : radio.default;
      return vue.h("div", {
        class: ["vxe-radio-group", {
          [`size--${vSize}`]: vSize
        }]
      }, defaultSlot ? defaultSlot({}) : options ? options.map((item) => {
        return vue.h(btnComp, {
          key: item[valueField],
          checkedValue: item[valueField],
          content: item[labelField],
          disabled: item[disabledField]
        });
      }) : []);
    };
    vue.watch(() => props.options, (val) => {
      loadData(val || []);
    });
    vue.onMounted(() => {
      vue.nextTick(() => {
        const { options } = props;
        if (options) {
          loadData(options);
        }
      });
    });
    vue.onUnmounted(() => {
      XEUtils.assign(internalData, createInternalData());
    });
    vue.provide("$xeRadioGroup", $xeRadioGroup);
    $xeRadioGroup.renderVN = renderVN;
    return $xeRadioGroup;
  },
  render() {
    return this.renderVN();
  }
});
exports.default = VxeRadioGroupComponent;
