"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const strings = require("../../../utils/strings.js");
const error = require("../../../utils/error.js");
const token = require("./token.js");
const option = require("./option.js");
const vue = require("vue");
const castArray = require("../../../../../lodash-es/castArray.js");
const get = require("../../../../../lodash-es/get.js");
const isEqual = require("../../../../../lodash-es/isEqual.js");
function useOption(props, states) {
  const select = vue.inject(token.selectKey);
  if (!select) error.throwError(option.COMPONENT_NAME, "usage: <el-select><el-option /></el-select/>");
  const selectGroup = vue.inject(token.selectGroupKey, { disabled: false });
  const itemSelected = vue.computed(() => {
    return contains(castArray.default(select.props.modelValue), props.value);
  });
  const limitReached = vue.computed(() => {
    if (select.props.multiple) {
      const modelValue = castArray.default(select.props.modelValue ?? []);
      return !itemSelected.value && modelValue.length >= select.props.multipleLimit && select.props.multipleLimit > 0;
    } else return false;
  });
  const currentLabel = vue.computed(() => {
    return props.label ?? (shared_esmBundler.isObject(props.value) ? "" : props.value);
  });
  const currentValue = vue.computed(() => {
    return props.value || props.label || "";
  });
  const isDisabled = vue.computed(() => {
    return props.disabled || states.groupDisabled || limitReached.value;
  });
  const instance = vue.getCurrentInstance();
  const contains = (arr = [], target) => {
    if (!shared_esmBundler.isObject(props.value)) return arr && arr.includes(target);
    else {
      const valueKey = select.props.valueKey;
      return arr && arr.some((item) => {
        return vue.toRaw(get.default(item, valueKey)) === get.default(target, valueKey);
      });
    }
  };
  const hoverItem = () => {
    if (!isDisabled.value) select.states.hoveringIndex = select.optionsArray.indexOf(instance.proxy);
  };
  const updateOption = (query) => {
    states.visible = new RegExp(strings.escapeStringRegexp(query), "i").test(String(currentLabel.value)) || props.created;
  };
  vue.watch(() => currentLabel.value, () => {
    if (!props.created && !select.props.remote) select.setSelected();
  });
  vue.watch(() => props.value, (val, oldVal) => {
    const { remote, valueKey } = select.props;
    if (remote ? val !== oldVal : !isEqual.default(val, oldVal)) {
      select.onOptionDestroy(oldVal, instance.proxy);
      select.onOptionCreate(instance.proxy);
    }
    if (!props.created && !remote) {
      if (valueKey && shared_esmBundler.isObject(val) && shared_esmBundler.isObject(oldVal) && val[valueKey] === oldVal[valueKey]) return;
      select.setSelected();
    }
  });
  vue.watch(() => selectGroup.disabled, () => {
    states.groupDisabled = selectGroup.disabled;
  }, { immediate: true });
  return {
    select,
    currentLabel,
    currentValue,
    itemSelected,
    isDisabled,
    hoverItem,
    updateOption
  };
}
exports.useOption = useOption;
