/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var isArray = require('lodash/isArray');
var get = require('lodash/get');
var hooks_slot = require('../../hooks/slot.js');

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

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

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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; }
var useSelectOptions = function useSelectOptions(props, keys) {
  var getChildComponentSlots = hooks_slot.useChildComponentSlots();
  var optionsCache = vue.ref([]);
  var options = vue.computed(function () {
    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++;
        return res;
      };
      if (option.group && option.children) {
        return _objectSpread(_objectSpread({}, option), {}, {
          children: option.children.map(function (child) {
            return getFormatOption(child);
          })
        });
      }
      return getFormatOption(option);
    })) || [];
    var optionsSlots = getChildComponentSlots("Option");
    var groupSlots = getChildComponentSlots("OptionGroup");
    if (isArray__default["default"](groupSlots)) {
      var _iterator = _createForOfIteratorHelper(groupSlots),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var _group$props, _res$, _res$2;
          var group = _step.value;
          var groupOption = _objectSpread(_objectSpread({
            group: (_group$props = group.props) === null || _group$props === void 0 ? void 0 : _group$props.label
          }, group.props), {}, {
            children: []
          });
          var res = group.children["default"]();
          if (!(isArray__default["default"](res) && !!((_res$ = res[0]) !== null && _res$ !== void 0 && _res$.children))) continue;
          var _iterator2 = _createForOfIteratorHelper(res === null || res === void 0 ? void 0 : (_res$2 = res[0]) === null || _res$2 === void 0 ? void 0 : _res$2.children),
            _step2;
          try {
            for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
              var child = _step2.value;
              groupOption.children.push(_objectSpread(_objectSpread({}, child.props), {}, {
                slots: child.children,
                index: dynamicIndex
              }));
              dynamicIndex++;
            }
          } catch (err) {
            _iterator2.e(err);
          } finally {
            _iterator2.f();
          }
          innerOptions.push(groupOption);
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    }
    if (isArray__default["default"](optionsSlots)) {
      var _iterator3 = _createForOfIteratorHelper(optionsSlots),
        _step3;
      try {
        for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
          var _child = _step3.value;
          innerOptions.push(_objectSpread(_objectSpread({}, _child.props), {}, {
            slots: _child.children,
            index: dynamicIndex
          }));
          dynamicIndex++;
        }
      } catch (err) {
        _iterator3.e(err);
      } finally {
        _iterator3.f();
      }
    }
    return innerOptions;
  });
  var optionsList = vue.computed(function () {
    var res = [];
    var getOptionsList = function getOptionsList(options2) {
      var _iterator4 = _createForOfIteratorHelper(options2),
        _step4;
      try {
        for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
          var option = _step4.value;
          if (option.group) getOptionsList(option.children);else res.push(option);
        }
      } catch (err) {
        _iterator4.e(err);
      } finally {
        _iterator4.f();
      }
    };
    getOptionsList(options.value);
    return res;
  });
  var optionsMap = vue.computed(function () {
    var res = /* @__PURE__ */new Map();
    optionsCache.value.concat(optionsList.value).forEach(function (option) {
      res.set(option.value, option);
    });
    return res;
  });
  return {
    options: options,
    optionsMap: optionsMap,
    optionsList: optionsList,
    optionsCache: optionsCache
  };
};

exports.useSelectOptions = useSelectOptions;
//# sourceMappingURL=useSelectOptions.js.map
