import { defineComponent, inject, getCurrentInstance, ref, onBeforeMount, toRefs, computed, onBeforeUnmount, createVNode } from 'vue';
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import { SELECT_PROVIDE_KEY } from './const';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
const optionProps = {
  value: {
    type: [String, Number, Boolean, Object],
    default: undefined
  },
  label: String,
  disabled: Boolean
};
var option = defineComponent({
  name: 'FOption',
  props: optionProps,
  setup(props, ctx) {
    const parent = inject(SELECT_PROVIDE_KEY, null);
    if (!parent) {
      console.warn('[FOption]: FOption 必须搭配 FSelect 组件使用！');
    }
    const instance = getCurrentInstance();
    const {
      addOption,
      removeOption,
      parentGroupOption
    } = parent;
    const optionRef = ref();
    onBeforeMount(() => {
      const option = _objectSpread(_objectSpread({
        id: instance.uid
      }, toRefs(props)), {}, {
        slots: ctx.slots
      });
      option.label = computed(() => {
        if (props.label) {
          return props.label;
        }
        const el = optionRef.value;
        // TODO: 检测 text 变更
        return (el === null || el === void 0 ? void 0 : el.textContent) || '';
      });

      // 如果是选项组包裹的，则收集包裹下面的option
      addOption(option, parentGroupOption);
    });
    onBeforeUnmount(() => {
      removeOption(instance.uid, parentGroupOption);
    });
    return () => {
      var _ctx$slots$default, _ctx$slots;
      return createVNode("span", {
        "ref": optionRef
      }, [(_ctx$slots$default = (_ctx$slots = ctx.slots).default) === null || _ctx$slots$default === void 0 ? void 0 : _ctx$slots$default.call(_ctx$slots)]);
    };
  }
});

export { option as default, optionProps };
