'use strict';

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

var vue = require('vue');
var legacyUtil = require('../utils/legacyUtil.cjs');

function useOptions(options, children, fieldNames) {
  const mergedOptions = vue.shallowRef();
  const valueOptions = vue.shallowRef();
  const labelOptions = vue.shallowRef();
  const tempMergedOptions = vue.shallowRef([]);
  vue.watch(
    [options, children],
    () => {
      if (options.value)
        tempMergedOptions.value = vue.toRaw(options.value).slice();
      else
        tempMergedOptions.value = legacyUtil.convertChildrenToData(children.value);
    },
    { immediate: true, deep: true }
  );
  vue.watchEffect(() => {
    const newOptions = tempMergedOptions.value;
    const newValueOptions = /* @__PURE__ */ new Map();
    const newLabelOptions = /* @__PURE__ */ new Map();
    const fieldNamesValue = fieldNames.value;
    function dig(optionList, isChildren = false) {
      for (let i = 0; i < optionList.length; i += 1) {
        const option = optionList[i];
        if (!option[fieldNamesValue.options] || isChildren) {
          newValueOptions.set(option[fieldNamesValue.value], option);
          newLabelOptions.set(option[fieldNamesValue.label], option);
        } else {
          dig(option[fieldNamesValue.options], true);
        }
      }
    }
    dig(newOptions);
    mergedOptions.value = newOptions;
    valueOptions.value = newValueOptions;
    labelOptions.value = newLabelOptions;
  });
  return {
    options: mergedOptions,
    valueOptions,
    labelOptions
  };
}

exports.default = useOptions;
//# sourceMappingURL=useOptions.cjs.map
