/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
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 _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function useSelectOptions(props, instance, keys) {
  var options = VueCompositionAPI.ref([]);

  var getOptions = function getOptions() {
    var dynamicIndex = 0;
    var innerOptions = props.options.map(function (option) {
      var getFormatOption = function getFormatOption(option2) {
        var _keys$value = keys.value,
            value = _keys$value.value,
            label = _keys$value.label;

        var res = _objectSpread(_objectSpread({}, 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 _objectSpread(_objectSpread({}, option), {}, {
          children: option.children.map(function (child) {
            return getFormatOption(child);
          })
        });
      }

      return getFormatOption(option);
    });
    var currentSlots = instance.proxy.$slots["default"] || [];
    var optionsSlots = currentSlots.filter(function (item) {
      var _item$componentOption;

      return ((_item$componentOption = item.componentOptions) === null || _item$componentOption === void 0 ? void 0 : _item$componentOption.tag) === "t-option";
    });
    var groupSlots = currentSlots.filter(function (item) {
      var _item$componentOption2;

      return ((_item$componentOption2 = item.componentOptions) === null || _item$componentOption2 === void 0 ? void 0 : _item$componentOption2.tag) === "t-option-group";
    });

    if (isArray__default["default"](groupSlots)) {
      groupSlots.forEach(function (group) {
        var _group$componentOptio;

        var groupOption = _objectSpread(_objectSpread({
          group: (_group$componentOptio = group.componentOptions.propsData) === null || _group$componentOptio === void 0 ? void 0 : _group$componentOptio.label
        }, group.componentOptions.propsData), {}, {
          children: []
        });

        var res = group.componentOptions.children;

        if (isArray__default["default"](res)) {
          res.forEach(function (child) {
            groupOption.children.push(_objectSpread(_objectSpread({
              "class": child.data.staticClass,
              style: child.data.staticStyle
            }, child.componentOptions.propsData), {}, {
              slots: function slots() {
                return child.componentOptions.children;
              },
              index: dynamicIndex
            }));
            dynamicIndex += 1;
          });
        }

        innerOptions.push(groupOption);
      });
    }

    if (isArray__default["default"](optionsSlots)) {
      optionsSlots.forEach(function (child) {
        innerOptions.push(_objectSpread(_objectSpread({
          "class": child.data.staticClass,
          style: child.data.staticStyle
        }, child.componentOptions.propsData), {}, {
          slots: function slots() {
            return child.componentOptions.children;
          },
          index: dynamicIndex
        }));
        dynamicIndex += 1;
      });
    }

    options.value = innerOptions;
  };

  var optionsMap = VueCompositionAPI.computed(function () {
    var res = /* @__PURE__ */new Map();
    optionsList.value.forEach(function (option) {
      res.set(option.value, option);
    });
    return res;
  });
  var optionsList = VueCompositionAPI.computed(function () {
    var res = [];

    var getOptionsList = function getOptionsList(options2) {
      options2.forEach(function (option) {
        if (option.group) {
          getOptionsList(option.children);
        } else {
          res.push(option);
        }
      });
    };

    getOptionsList(options.value);
    return res;
  });
  getOptions();
  VueCompositionAPI.onBeforeUpdate(function () {
    getOptions();
  });
  return {
    options: options,
    optionsMap: optionsMap,
    optionsList: optionsList
  };
}

exports["default"] = useSelectOptions;
//# sourceMappingURL=useSelectOptions.js.map
