/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var get = require('lodash/get');
var isArray = require('lodash/isArray');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);

function useSelectOptions(props, instance, keys) {
  const options = VueCompositionAPI.ref([]);
  const getOptions = () => {
    let dynamicIndex = 0;
    const innerOptions = props.options?.map(option => {
      const getFormatOption = option2 => {
        const {
          value,
          label
        } = keys.value;
        const res = {
          ...option2,
          index: dynamicIndex,
          label: get__default["default"](option2, label),
          value: get__default["default"](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 => item.componentOptions?.tag === "l-option");
    const groupSlots = currentSlots.filter(item => item.componentOptions?.tag === "l-option-group");
    if (isArray__default["default"](groupSlots)) {
      groupSlots.forEach(group => {
        const groupOption = {
          group: group.componentOptions.propsData?.label,
          ...group.componentOptions.propsData,
          children: []
        };
        const res = group.componentOptions.children;
        if (isArray__default["default"](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__default["default"](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 = VueCompositionAPI.computed(() => {
    const res = /* @__PURE__ */new Map();
    optionsList.value.forEach(option => {
      res.set(option.value, option);
    });
    return res;
  });
  const optionsList = VueCompositionAPI.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();
  VueCompositionAPI.watch(() => props.options, () => {
    getOptions();
  });
  VueCompositionAPI.onBeforeUpdate(() => {
    getOptions();
  });
  return {
    options,
    optionsMap,
    optionsList
  };
}

exports["default"] = useSelectOptions;
//# sourceMappingURL=use-select-options.js.map
