/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @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 config = require('../../config.js');

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);

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 innerSlotRecord = null;
  var getOptions = function getOptions() {
    var _props$options;
    var dynamicIndex = 0;
    var innerOptions = ((_props$options = props.options) === null || _props$options === void 0 ? void 0 : _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);
    })) || [];
    if (props.options === void 0) {
      innerSlotRecord = instance.proxy.$slots["default"];
      var currentSlots = instance.proxy.$slots["default"] || [];
      currentSlots.forEach(function (child) {
        var _child$componentOptio, _child$componentOptio2;
        if (((_child$componentOptio = child.componentOptions) === null || _child$componentOptio === void 0 ? void 0 : _child$componentOptio.tag) === "".concat(config.prefix, "-option")) {
          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;
        } else if (((_child$componentOptio2 = child.componentOptions) === null || _child$componentOptio2 === void 0 ? void 0 : _child$componentOptio2.tag) === "".concat(config.prefix, "-option-group")) {
          var _child$componentOptio3, _child$componentOptio4, _child$componentOptio5;
          var groupOption = _objectSpread(_objectSpread({
            group: (_child$componentOptio3 = child.componentOptions.propsData) === null || _child$componentOptio3 === void 0 ? void 0 : _child$componentOptio3.label
          }, child.componentOptions.propsData), {}, {
            children: []
          });
          (_child$componentOptio4 = child.componentOptions.children) === null || _child$componentOptio4 === void 0 ? void 0 : (_child$componentOptio5 = _child$componentOptio4.forEach) === null || _child$componentOptio5 === void 0 ? void 0 : _child$componentOptio5.call(_child$componentOptio4, function (groupChild) {
            groupOption.children.push(_objectSpread(_objectSpread({
              "class": groupChild.data.staticClass,
              style: groupChild.data.staticStyle
            }, groupChild.componentOptions.propsData), {}, {
              slots: function slots() {
                return groupChild.componentOptions.children;
              },
              index: dynamicIndex
            }));
            dynamicIndex += 1;
          });
          innerOptions.push(groupOption);
        }
      });
    }
    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.watch(function () {
    return props.options;
  }, function () {
    getOptions();
  });
  VueCompositionAPI.onBeforeUpdate(function () {
    if (props.options === void 0 && innerSlotRecord !== instance.proxy.$slots["default"]) getOptions();
  });
  return {
    options: options,
    optionsMap: optionsMap,
    optionsList: optionsList
  };
}

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