import { inject, ref, computed } from 'vue';
import { CHANGE_EVENT } from '../constants';
import { useNormalModel } from './useModel';
import useFormAdaptor from './useFormAdaptor';

function useSelect(_ref) {
  let {
    props,
    emit,
    parent
  } = _ref;
  const {
    validate,
    isFormDisabled
  } = useFormAdaptor({
    valueType: 'boolean'
  });
  const group = inject(parent.groupKey, null);
  const focus = ref(false);
  const hover = ref(false);
  const isGroup = group !== null;
  const [currentValue, updateCurrentValue] = useNormalModel(props, emit);
  const checked = computed(() => {
    if (!isGroup) {
      return currentValue.value;
    }
    return group.isSelect(props.value);
  });
  const innerDisabled = computed(() => {
    var _group$props;
    return props.disabled || isGroup && (group === null || group === void 0 || (_group$props = group.props) === null || _group$props === void 0 ? void 0 : _group$props.disabled) || isFormDisabled.value;
  });
  const handleClick = () => {
    if (innerDisabled.value) {
      return;
    }
    if (isGroup) {
      group.onSelect(props.value, () => {
        const newVal = group.isSelect(props.value);
        emit(CHANGE_EVENT, newVal);
      });
    } else {
      const newVal = !currentValue.value;
      updateCurrentValue(newVal);
      emit(CHANGE_EVENT, newVal);
      validate(CHANGE_EVENT);
    }
  };
  const handleMouseOver = () => {
    hover.value = true;
  };
  const handleMouseOut = () => {
    hover.value = false;
  };
  return {
    currentValue,
    updateCurrentValue,
    checked,
    innerDisabled,
    isGroup,
    group,
    focus,
    hover,
    handleClick,
    handleMouseOver,
    handleMouseOut
  };
}

export { useSelect as default };
