/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { ref, computed, watch, onBeforeUpdate } from '@vue/composition-api';
import get from 'lodash/get';
import isArray from 'lodash/isArray';

function useSelectOptions(props, instance, keys) {
  const options = ref([]);
  const getOptions = () => {
    var _props$options;
    let dynamicIndex = 0;
    const innerOptions = ((_props$options = props.options) === null || _props$options === void 0 ? void 0 : _props$options.map(option => {
      const getFormatOption = option2 => {
        const {
          value,
          label
        } = keys.value;
        const res = {
          ...option2,
          index: dynamicIndex,
          label: get(option2, label),
          value: get(option2, value)
        };
        dynamicIndex += 1;
        return res;
      };
      if (option.group && option.children) {
        return {
          ...option,
          children: option.children.map(child => getFormatOption(child))
        };
      }
      return getFormatOption(option);
    })) || [];
    const currentSlots = instance.proxy.$slots.default || [];
    const optionsSlots = currentSlots.filter(item => {
      var _item$componentOption;
      return ((_item$componentOption = item.componentOptions) === null || _item$componentOption === void 0 ? void 0 : _item$componentOption.tag) === "l-option";
    });
    const groupSlots = currentSlots.filter(item => {
      var _item$componentOption2;
      return ((_item$componentOption2 = item.componentOptions) === null || _item$componentOption2 === void 0 ? void 0 : _item$componentOption2.tag) === "l-option-group";
    });
    if (isArray(groupSlots)) {
      groupSlots.forEach(group => {
        var _group$componentOptio;
        const groupOption = {
          group: (_group$componentOptio = group.componentOptions.propsData) === null || _group$componentOptio === void 0 ? void 0 : _group$componentOptio.label,
          ...group.componentOptions.propsData,
          children: []
        };
        const res = group.componentOptions.children;
        if (isArray(res)) {
          res.forEach(child => {
            groupOption.children.push({
              class: child.data.staticClass,
              style: child.data.staticStyle,
              ...child.componentOptions.propsData,
              slots: () => child.componentOptions.children,
              index: dynamicIndex
            });
            dynamicIndex += 1;
          });
        }
        innerOptions.push(groupOption);
      });
    }
    if (isArray(optionsSlots)) {
      optionsSlots.forEach(child => {
        innerOptions.push({
          class: child.data.staticClass,
          style: child.data.staticStyle,
          ...child.componentOptions.propsData,
          slots: () => child.componentOptions.children,
          index: dynamicIndex
        });
        dynamicIndex += 1;
      });
    }
    options.value = innerOptions;
  };
  const optionsMap = computed(() => {
    const res = /* @__PURE__ */new Map();
    optionsList.value.forEach(option => {
      res.set(option.value, option);
    });
    return res;
  });
  const optionsList = computed(() => {
    const res = [];
    const getOptionsList = options2 => {
      options2.forEach(option => {
        if (option.group) getOptionsList(option.children);else res.push(option);
      });
    };
    getOptionsList(options.value);
    return res;
  });
  getOptions();
  watch(() => props.options, () => {
    getOptions();
  });
  onBeforeUpdate(() => {
    getOptions();
  });
  return {
    options,
    optionsMap,
    optionsList
  };
}

export { useSelectOptions as default };
//# sourceMappingURL=use-select-options.js.map
